### How can I find the largest number in loops?

Pages: 123
I'm making a program that determines the largest number from the 10 loops. How can I make it determine the largest from the 10 loops. Here's the program I'm doing. *NOTE* I mean loops as in after you build and run it, it will make you enter an number 10 times.

 ``12345678910111213141516171819`` ``````/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the numbers. */ #include using namespace std; int main() { int counter,number,largest; counter = 0; while ( counter <= 10 ) { cout << "Enter a number: "; cin >> number; } return 0; }``````
Last edited on

 ``1234`` ``````if(number > largest) { largest = number; }``````

And don't forget about the counter variable.
*Bang my head against the wall*
Last edited on
Still doesn't work I did this.

 ``1234567891011121314151617181920212223242526272829`` ``````/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the numbers. */ #include using namespace std; int main() { int counter,number,largest; counter = 0; while ( counter < 10 ) { cout << "Enter a number: "; cin >> number; if ( number > largest ) { largest = number; cout << endl; cout << "The largest number is: " << largest; } counter++; } return 0; }``````
put lines 22 and 23 after the while loop
Umm It did work but the results. Ehhhh, the largest number is like 2242313. I entered it all the numbers 1,2,3. No other numbers than 1,2,3.

 ``1234567891011121314151617181920212223242526272829`` ``````/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the numbers. */ #include using namespace std; int main() { int counter,number,largest; counter = 0; while ( counter <= 10 ) { cout << "Enter a number: "; cin >> number; if ( number > largest ) { largest = number; } counter++; } cout << endl; cout << "The largest number is: " << largest; return 0; }``````

Now when you compare "number" to "largest", "largest" will have some crap value.

`int largest = 0;`
Last edited on
Wow dude, thanks! It worked. :)
How do you find 2 largest vales?
Well, can you think about how to do that?

Hint - you need another variable.
Hmm thanks Ideaman......
Nope, I can't figure it out. :\
What have you tried? Post your new code. You must have done something.
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the numbers. */ #include using namespace std; int main() { int counter,number,largest,large2; counter = 0; largest = 0; while ( counter <= 10 ) { cout << "Enter a number: "; cin >> number; if ( number > largest ) { largest = number; } if ( number > large2 ) { large2 = number; } counter++; } cout << endl; cout << "The largest numbers is: " << largest << " and " << large2; return 0; }``````
OK same problem as before - you need to initialise your variables.

I like to declare, initialise and comment all on 1 line like this:

 ``1234`` ``````int counter = 0; //count the numbers int number = 0; // A particular number int largest = 0; //The largest number int large2 = 0 //The second largest number; ``````

It makes it much easier for someone else to understand what all your variables mean.

Now think about how to do this. If you find a new largest number, what happens to the old largest number?
The old largest number will be the new largest number?
Well, no.

Say you have found a number that is bigger than the existing largest number. This will be the new largest number. The old largest number go into which variable?
Ummmm it will be replaced?
[Sigh] Come on, it's pretty simple.

It's a bit like this:

You have two buckets numbered 1 and 2. Object A goes into bucket 1, where does Object B go?

I apologise if I sound rude, but it really is very simple.

oh.... it goes into 2....
Last edited on
Pages: 123