Average of readings
Combine a loop with integer division. An easy embedded C problem (Arrays and text), run and graded in the browser.
easy · Arrays and text · arrays · division
Return the average of the first n elements of values, with the fraction dropped. n is at least 1.
Starter code
int average(const int values[], int n) {
return 0;
}Example tests
Three readings
int v[] = { 10, 20, 30 };
printf("%d\n", average(v, 3));Expected output:
20The fraction is dropped
int v[] = { 1, 2 };
printf("%d\n", average(v, 2));Expected output:
1Learn 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.
- Fill an array · Write to the elements of an array.
- Find a value · Search an array and report where a value is.
- Length of a string · Find where a piece of text ends.
- Count a character · Loop over text and count matches.