Find a value
Search an array and report where a value is. An easy embedded C problem (Arrays and text), run and graded in the browser.
easy · Arrays and text · arrays · search
Return the position of the first element among the first n elements of values that equals target. If there is none, return -1.
Starter code
int index_of(const int values[], int n, int target) {
return -1;
}Example tests
Found
int v[] = { 4, 8, 15, 16 };
printf("%d\n", index_of(v, 4, 15));Expected output:
2Not found
int v[] = { 4, 8, 15, 16 };
printf("%d\n", index_of(v, 4, 23));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.
- 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.