pointers-memory · beginner · ~15 min

Fill an array

Write through an array pointer.

Challenge

Set every element of an array to the same value, writing through the array pointer.

Task

Implement void fill(int *a, int n, int v) that sets the first n elements of a to v. No main — the grader calls it.

Input

a — pointer to an int array. n — number of elements to set (may be 0). v — the value to write.

Output

No return value. The first n elements of a become v.

Example

int a[4] = {1,2,3,4}; fill(a, 4, 9)   ->   a == {9,9,9,9}
int b[3] = {5,5,5};   fill(b, 0, 0)   ->   b unchanged

Edge cases

  • n == 0: writes nothing.

Input format

a — int array; n — count to fill (may be 0); v — value.

Output format

No return value; the first n elements are set to v.

Starter code

void fill(int *a, int n, int v) {
    /* TODO */
}

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