Practice ยท 4 of 4
Reduce, the C Way
Implement folds over int arrays:
``c
/* left fold: acc = fn(acc, arr[i]) for i in [0, n), starting from init */
int fold_i(const int *arr, size_t n, int init, int (*fn)(int acc, int v));
/* convenience: sum via fold_i */
int sum_i(const int *arr, size_t n);
/* max via fold_i (n == 0 returns INT_MIN) */
int max_i(const int *arr, size_t n);
``
The boilerplate provides fn_add, fn_max as fold functions.
Difficulty: intermediate
Press Submit to check your solution.
Back to lesson: Practice: Comparator Gym