data-structures · beginner · ~15 min
Find the maximum by comparing against the rest.
Implement:
int array_max(const int *a, int n);
Return the maximum of a[0..n-1] (n >= 1), computed recursively.
Array a, length n>=1.
Maximum element.
Handle negative values.
#include <stddef.h>
/* Maximum of a[0..n-1] (n>=1), computed recursively. */
int array_max(const int *a,int n){ (void)a;(void)n; return 0; }
Base case at n==0 (there is no max of nothing) instead of n==1.
Single element is its own max.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.