pointers-memory · beginner · ~15 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).

Input format

a — int array; n — count to inspect (may be 0).

Output format

1 if all n elements are 0 (or n==0), else 0.

Starter code

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

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