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.

Type your C++ solution code here:


This is a function exercise. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve an exercise or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect tests, etc.), please

Is there a problem? Contact a site administrator.

©, all rights reserved.