CJCoding With Joseph
15free / daySign inCreate account

Index of a Value

Write a function int indexOf(const array<int, 6>& a, int x) that returns the index (0 to 5) of the first element equal to x, or -1 if x does not appear in the array. If x appears more than once, return the smallest index.

For {4, 8, 15, 16, 23, 42} and x = 15 the function returns 2. Each test prints the returned indexes on their own lines, as shown in Expected Output.

Do NOT write a main function.

Expected Output:

2
-1
1
-1

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
STL Containersstd::array
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.