How to output the highest number of an array

This is now sorted whooo
Last edited on
OK, the simplest (kinda) way to determine the greatest number in a container is to iterate through the container and compare each element to the previous "highest number".

You have an array with 4 elements, and let's say you have a variable called highScore. You can set highScore to contain the score of the first team, then compare it to each of the other 3 teams. If you find a team with a higher score than what is contained in highScore, you assign that value to highScore. At the end of those 3 comparisons, the greatest number will be contained in highScore
Ahh sounds like a good idea, don't suppose you could give a small example? of perhaps tell me what this comparing would be called? so I could look it up in my great little C++ book?

I'm still very new.

Thanks again for your time.
1
2
3
4
5
6
7
8
9
10
// declare highScore, set it to be the first team's points by default
int highScore = points[0];
// cycle through each of the other team's points
for( int i = 1; i < 4; ++i ) {
   // if another team has higher points than the previous highest,
   // set the highScore to equal that team's points (this will be the new highest)
   if( points[i] > highScore ) {
      highScore = points[i];
   }
}


As far as I know, this doesn't really have a special name. Notice that i is initialized to have a value of 1 instead of 0, this is because we want to start comparing from the second element, because highScore already contains the value of the first element.
Last edited on
Excellent, thanks for that! I have now implemented something similar and thank god it works LOL. However would their be away to also bring the team name along with this? meaning could i get the code to output the highest score as well as the relivent name to which it belongs to?
Sure theres a way, but maybe it'd be better for you if you figured that one out yourself.
Topic archived. No new replies allowed.