Answer on Question #47506 - Programming, C
Write a function, lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. Also, write a program to test your function.
Solution.
//Connecting library
#include <vector>
#include <iostream>
using namespace std;
int lastLargestIndex(int* arr, int size);
int main()
{
int arr[] = {1,58,54,5,8,3,58};
int index = lastLargestIndex(arr, sizeof(arr) / sizeof(arr[0]));
cout << "return lastLargestIndex: " << index << endl;
cout << "The last occurrence of the largest element in the array: " << endl;
cout << arr[index];
//system("pause");
}
int lastLargestIndex(int* arr, int size)
{
int buf = 0, index = 0;
for (int i = 0; i < size; i++)
{
if (buf <= arr[i]) { buf = arr[i]; index = i; }
}
return index;
}http://www.AssignmentExpert.com/
Comments