logo CodeStepByStep logo

secondIndexOf

Language/Type: C++ arrays array programming

Write a function named secondIndexOf that accepts an array of integers, its length, and a target value as parameters and returns the index of the second occurrence of the target value in the array. If the value does not appear in the array at least twice, return -1. For example, if an array called list stores the following values:

     // index  0  1   2   3  4   5   6  7   8  9  10
int list[] = {42, 7, -9, 14, 8, 39, 42, 8, 19, 0, 42};

Then the call secondIndexOf(list, 11, 42) should return 6 because the index of the second occurrence of value 42 in the array is at index 6. The call secondIndexOf(list, 11, 14) should return -1 because value 14 does not occur at least twice in the array.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.