basics · beginner · ~15 min

Sum of array

Iterate over an array with a size parameter.

Challenge

Implement long sum_array(const int *a, size_t n) returning the sum of the first n ints in a. n may be zero.

Starter code

#include <stddef.h>

long sum_array(const int *a, size_t n) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.