basics · intermediate · ~15 min
Spot an initialisation bug that only shows on certain inputs.
The starter initialises the running max to 0, which is wrong for all-negative arrays. Fix int find_max(const int *a, int n) (n >= 1) to return the true maximum.
int find_max(const int *a, int n) {
int m = 0; /* BUG: wrong for all-negative arrays */
for (int i = 0; i < n; i++)
if (a[i] > m) m = a[i];
return m;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.