Hi guys,
I'm writing a function that is suppose to print out the biggest number in the array.
I can get it to print it out, but then it prints at every loop. I only want it to print one time.
Where to put it in?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void printMostEaten(int anArray[], int nSize)
{
for(int nStartIndex=0; nStartIndex < nSize; nStartIndex++)
{
// Highest index meet so far.
int nHighestIndex = nStartIndex;
// Search through every element starting from nStartIndex + 1.
for(int nCurrentIndex = nStartIndex+1; nCurrentIndex < nSize; nCurrentIndex++)
{
if(anArray[nCurrentIndex] > anArray[nHighestIndex])
nHighestIndex = nCurrentIndex;
}
}
}
#include <iostream>
usingnamespace std;
int printMostEaten(int anArray[], int nSize);
int main()
{
int result;
int array[10] = { 1, 9, 12, 44, 21, 10, 11, 33, 55 };
result = printMostEaten(array, 10);
cout << result;
return 0;
}
int printMostEaten(int anArray[], int nSize)
{
int num = 0;
for (int i = 0; i < nSize;i++)
if (anArray[i] > num)
num = anArray[i];
return num;
}