Fill an array
Write to the elements of an array. An easy embedded C problem (Arrays and text), run and graded in the browser.
easy · Arrays and text · arrays · writing
Set each of the first n elements of values to value.
The function changes the caller's array directly. An array is not copied when it is passed to a function.
Starter code
void fill(int values[], int n, int value) {
}Example tests
Fill everything
int v[4] = { 1, 2, 3, 4 };
fill(v, 4, 7);
printf("%d %d %d %d\n", v[0], v[1], v[2], v[3]);Expected output:
7 7 7 7Fill only the first two
int v[4] = { 1, 2, 3, 4 };
fill(v, 2, 0);
printf("%d %d %d %d\n", v[0], v[1], v[2], v[3]);Expected output:
0 0 3 4Learn it first
- Arrays and text · Many values under one name, looping over them safely, and strings.
More Arrays and text problems
- Sum an array · Visit every element with a loop.
- Largest element · Track the best value seen so far.
- Count readings above a threshold · Count the elements that pass a test.
- Find a value · Search an array and report where a value is.
- Average of readings · Combine a loop with integer division.
- Length of a string · Find where a piece of text ends.
- Count a character · Loop over text and count matches.