basics · beginner · ~15 min
Iterate an array by index.
Add up the elements of an integer array.
Implement long array_sum(const int *a, int n) that returns the sum of the first n elements of a. Accumulate into a long so large totals don't overflow. No main — the grader calls it.
A pointer a to at least n ints, and the count int n (n >= 0).
Returns the sum of the n elements as a long.
array_sum({1,2,3,4,5}, 5) -> 15
array_sum({-3,3}, 2) -> 0
array_sum({7}, 1) -> 7
n == 0 returns 0 (empty range).A pointer a to n ints, and the count int n (n >= 0).
The sum of the n elements, as a long.
n may be 0 (returns 0).
long array_sum(const int *a, int n) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.