Get the Index of the First Maximum Number in an Array

23:26 0 Comments A+ a-

Problem:
Write a program that contains at least two functions.
a) inputArray(…) // It takes input for the array.
b) getMaxIndex(…) // It finds maximum number and returns the index of the first occurrence of the maximum.

Your program takes input from user and displays the position of the maximum number
Sample Run 1:
Enter 10 Numbers:
10 20 30 87 40 87 86 23 23 78
First occurrence of the Maximum is found at index:3.
Solution:
# include <iostream>
using namespace std;
void inputArray(int array[], const int size)
{
cout << "Enter " << size << " Numbers: " << endl;
for (int i=0;i < size;i++)
{
cin >> array[i];
}
}
void getMaxIndex(int array[],int size)
{
int max=array[size-1];
int maxIndex=size-1;
int i=size-2;
while(i>=0)
{
if(array[i]>=max)
{
max=array[i];
maxIndex=i;
}
i--;
}
cout<<"The index of 1st maximum number is : "<<maxIndex<<endl;

}
int main()
{
const int size = 10;
int array[size];
inputArray(array, size);
getMaxIndex(array,size);

return 0;

}