Index of a Value
mediumQuestion 541Write 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, so print one after the last line.
Hints4 available
Sign in to use hintsTopics
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.