/*
Program for Searching array of n entered numbers for a Maximum number and return it's INDEXusing function.
*/
#include<iostream>
using namespace std;
int getMaxIndex(int[], int); //function Declaration/Prototype
int main()
{
int size, num[50], index;
//For Inputting amount of numbers
do
{
cout << "How many numbers u want to Enter (Max 50)?\t";
cin >> size;
if (size < 0 || size > 50)
cout << "Invalid Input.......\nEnter number again\n";
} while (size < 0 || size > 50);
//For inputting Numbers in Array
for (int i = 0; i < size; i++)
{
cout << "Enetr number at [" << i << "] index?\t";
cin >> num[i];
}
//for formatting output
for (int i = 0; i < 80; i++)
cout << "=";
index = getMaxIndex(num, size);
//displays result
cout << "\nMaximum Number is " << num[index] << " at INDEX [" << index << "].";
cout << endl;
return 0;
}
//function Definition
int getMaxIndex(int num[], int size)
{
int index=0, lar=num[0];
//for finding Index of Largest Number
for (int i = 1; i < size; i++)
{
if (lar < num[i])
{
lar = num[i];
index = i;
}
}
return index;
}
OUTPUT
No comments:
Post a Comment