Printing highest number in array.

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;
        }
    }
}

Its because you have a loop inside a loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <iostream>
using namespace 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;
}
55
Damn, much easier. Thank you very much.
Your welcome :)
Topic archived. No new replies allowed.