data-structures · beginner · ~15 min
Sum an array by shrinking its length.
Implement:
long long array_sum(const int *a, int n);
Return the sum of a[0..n-1], computed recursively.
Array a, length n.
Sum of elements.
Reduce n by 1 each call.
#include <stddef.h>
/* Sum of a[0..n-1], computed recursively. */
long long array_sum(const int *a,int n){ (void)a;(void)n; return 0; }
Off-by-one indexing (use a[n-1] with the n-1 subproblem).
n==0 returns 0.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.