You've almost got it right.
Note that it should be int main()
and pausing at line 35 achieves nothing.
Here's your code with some corrections.
#include <iostream>
using namespace std;
int lastLargestIndex(int [], int);
int main()
{
int arr[15] = {5,198,76,9,4,2,15,8,21,34,99,3,198,13,61,};
int location = lastLargestIndex(arr, 15);
if (location != -1)
cout << "The last largest index is: " << location << endl;
// pause to read the output
cout << "\npress Enter to exit...";
cin.get();
return 0;
}
int lastLargestIndex(int arr[], int size)
{
if (size == 0)
return -1;
if (size == 1)
return 0;
// set variables to the first index
int lastLargestIndex = 0;
int tem = arr[0];
// start the loop from the 2nd index (1)
for (int i = 1; i < size; ++i)
{
if (arr[i] >= tem) // note >= so we get the last occurrence
{
lastLargestIndex = i;
tem = arr[i];
}
}
return lastLargestIndex;
}