pointers-memory · beginner · ~6 min
Verify a buffer is zeroed.
Check whether every element of an array is zero.
Implement int all_zero(const int *a, int n) that returns 1 if all of the first n elements are 0, otherwise 0. No main — the grader calls it.
a — pointer to an int array. n — the number of elements to inspect (may be 0).
Returns 1 if every element is 0 (or n == 0), else 0.
all_zero({0,0,0}, 3) -> 1
all_zero({0,1,0}, 3) -> 0
all_zero(a, 0) -> 1 (vacuously true)
n == 0: returns 1 (no element violates the condition).Drill: practice one contract before combining it with other skills.
a — pointer to an int array. n — the number of elements to inspect (may be 0).
Returns 1 if every element is 0 (or n == 0), else 0.
n is nonnegative; a points to at least n readable ints (a may be NULL only when n=0). Read only. Return exactly 0 or 1.
int all_zero(const int *a, int n) {
/* TODO */
return 0;
}
Checking only the first element or treating only positive numbers as nonzero.
Empty range is true; nonzero first, middle, or last element; negative values are nonzero too.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.