pointers-memory · beginner · ~6 min

All zero?

Verify a buffer is zeroed.

Challenge

Check whether every element of an array is zero.

Task

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.

Input

a — pointer to an int array. n — the number of elements to inspect (may be 0).

Output

Returns 1 if every element is 0 (or n == 0), else 0.

Example

all_zero({0,0,0}, 3)   ->   1
all_zero({0,1,0}, 3)   ->   0
all_zero(a, 0)         ->   1   (vacuously true)

Edge cases

  • n == 0: returns 1 (no element violates the condition).

Why this matters

Drill: practice one contract before combining it with other skills.

Input format

a — pointer to an int array. n — the number of elements to inspect (may be 0).

Output format

Returns 1 if every element is 0 (or n == 0), else 0.

Constraints

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.

Starter code

int all_zero(const int *a, int n) {
    /* TODO */
    return 0;
}

Common mistakes

Checking only the first element or treating only positive numbers as nonzero.

Edge cases to handle

Empty range is true; nonzero first, middle, or last element; negative values are nonzero too.

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.