How can I find the largest number in loops?

Pages: 123
Aug 2, 2012 at 3:13am
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the
numbers. */
#include<iostream>

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 Aug 2, 2012 at 3:14am
Aug 2, 2012 at 3:31am

1
2
3
4
if(number > largest)
{           
 largest = number;         
}


And don't forget about the counter variable.
Aug 2, 2012 at 3:33am
*Bang my head against the wall*
Last edited on Aug 2, 2012 at 3:33am
Aug 2, 2012 at 3:38am
Still doesn't work I did this.

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
/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the
numbers. */
#include<iostream>

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;
}
Aug 2, 2012 at 5:01am
put lines 22 and 23 after the while loop
Aug 2, 2012 at 5:08am
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.

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
/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the
numbers. */
#include<iostream>

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;
}
Aug 2, 2012 at 8:32am
You haven't initialized your variable.

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

int largest = 0;
Last edited on Aug 2, 2012 at 8:32am
Aug 3, 2012 at 1:57am
Wow dude, thanks! It worked. :)
Aug 3, 2012 at 3:12am
How do you find 2 largest vales?
Aug 3, 2012 at 3:18am
Well, can you think about how to do that?

Hint - you need another variable.
Aug 3, 2012 at 3:53am
Hmm thanks Ideaman......
Aug 3, 2012 at 4:06am
Nope, I can't figure it out. :\
Aug 3, 2012 at 4:09am
What have you tried? Post your new code. You must have done something.
Aug 3, 2012 at 4:14am
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
31
32
33
34
35
36
/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the
numbers. */
#include<iostream>

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;
}
Aug 3, 2012 at 4:22am
OK same problem as before - you need to initialise your variables.

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

1
2
3
4
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?
Aug 3, 2012 at 4:31am
The old largest number will be the new largest number?
Aug 3, 2012 at 4:52am
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?
Aug 3, 2012 at 5:26am
Ummmm it will be replaced?
Aug 3, 2012 at 5:33am
[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.

Aug 3, 2012 at 5:36am
oh.... it goes into 2....
Last edited on Aug 3, 2012 at 7:12am
Pages: 123