Question #47506

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.
1

Expert's answer

2014-10-07T00:39:15-0400

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/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS