How can I find the largest number in loops?

Pages: 123
And the old largest value?
It goes to the old value variable aka large2!?
Last edited on
Ok show us the new code. I have been pretty clear, I am now tired of explaining that sky is blue on a fine day.
I still don't get it.... Its the same code. Here:

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
37
38
/* 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;
    number = 0;
    large2 = 0;

    while ( counter <= 5 )
        {
            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;
}
what if you enter these numbers?

1 2 3

3 is the largest, so it is stored in the variable largest

then you enter 4.

4 is the largest

before you store 4 into largest, you need to do something with the 3 that is in largest now. Which variable do you put it in?

Ok I'm no expert but the way I look at it is:
1
2
3
4
if ( number > largest )
{
        largest = number;
}

Ok you have tested the input to see if it is larger than the largest number - good.

So test to see what number (less than largest) is the large2.
First test - number must be less than largest.
Second test - is number greater than large2, if number is larger than large2 then large2 must be equil to number.

That should solve it.
Oh just one other thing. If you want to input 10 numbers then you need to change this or else you will get 11 numbers
while (counter < 10)

NB. zero is the first number in C++
There is only a need for one test. The rest is just assignment. The incredibly tricky thing seems to be which variable to assign the value of the second largest value to. We don't have a lot of variables, it shouldn't be that hard to work out which one .........
Troll
Troll

I thought about that, and went back and looked some previous posts. Some of them not as bad as this one, so maybe a serial troll..........
I'm a troll? No, I'm just a newbie D:
how can you see more than the 5 posts they have on their profile?

1
2
3
4
if (number > largest)
    int temp = largest; //store it so we can use it
    largest = number; //if we hadn't stored it in tem, we wouldn't know what the number of the int largest was
    number = temp; //and without knowing that, we wouldn't be able to set number to what largest was, inother words we are swaping these two numbers 


there was an article about how there are 2 groups of programming studnts, those that failed to graps theconcept of how variables worked would rarely pass, whereas the opposite was true for those that could.

sorry about spelling, not at my pc, and pretend those lines of code are inside the if statement block,even without curly brackets
Last edited on
I still don't get it....
A better example is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int largest1 = 0, largest2 = 0;
// Do some lines here
// Loop to find the largest 2 values
for (int i = 0; i < arraySize; i ++)
   // Number was larger than largest1
   if (myArray[i] > largest1) {
      // largest2 should get the old largest value
      largest2 = largest1;
      // largest1 should now get the new largest value
      largest1 = myArray[i];
   }
   // Number was equal to largest1 or less than
   // But greater than largest 2
   else if (myArray[i] > largest2)
      largest2 = myArray[i];


largest1 will contain the largest value while largest2 will contain the second largest value.
:[ my example was shit

/cry
lol don't cry
1
2
3
cookie *Cookie = new cookie;
person *Person = &you;
Cookie.Give(Person);


Better?
@Volatile what's up with the "myArray", you know I'm not at the array part yet :|
Someone needs to make a header file that will allow that to work. I'm a n00b, so sadly the task falls to someone else, but...
Oh nvm, volatile. Ignore me. I found it.
Array Version:
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
#include <iostream>

int main() {
   int largest1 = 0, largest2 = 0;
   // Create a constant max size for the array
   const int arraySize = 20;
   // Create our array
   int myArray[arraySize];
   // Loop to find the largest 2 values
   for (int i = 0; i < arraySize; i ++) {
      // Prompt user
      std::cout << "Please enter number " << (i + 1) << ": ";
      // Wait for input
      std::cin >> myArray[i];
      // Number was larger than largest1
      if (myArray[i] > largest1) {
         // largest2 should get the old largest value
         largest2 = largest1;
         // largest1 should now get the new largest value
         largest1 = myArray[i];
      }
      // Number was equal to largest1 or less than
      // But greater than largest 2
      else if (myArray[i] > largest2)
         largest2 = myArray[i];
   }
   return 0;
}


Regular number version:
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
#include <iostream>

int main() {
   int largest1 = 0, largest2 = 0;
   int number, maxNum;
   std::cout << "How many numbers would you like to enter? ";
   // Assume the user is going to enter an integer greater than 0
   std::cin >> maxNum;
   // Loop to find the largest 2 values
   for (int i = 0; i < maxNum; i ++) {
      // Prompt user
      std::cout << "Please enter number " << (i + 1) << ": ";
      // Wait for input
      std::cin >> number;
      // Number was larger than largest1
      if (number > largest1) {
         // largest2 should get the old largest value
         largest2 = largest1;
         // largest1 should now get the new largest value
         largest1 = number;
      }
      // Number was equal to largest1 or less than
      // But greater than largest 2
      else if (number > largest2)
         largest2 = number;
   }
   return 0;
}


There is two completely working examples, one using an array, and the other using a basic variable.
@Zephilinox

how can you see more than the 5 posts they have on their profile?


Go backwards through the pages on the beginners forum until you find a thread written by the person concerned.

If you read the responses , you will see that there is a lot of vague requests for more info, but with almost no effort provided by the OP to help themselves. Look at this thread, and read all the hopeless replies.

This poster is a SERIAL TROLL, and I have reported it to admin as such. I have learnt my lesson, don't waste your effort on these oxygen thieves. There you go, I have given him what he wants (assuming male gender)

The trouble is, they just create a new user name - I have suspicions about 5 or so other users.

Maybe admin can go troll hunting and find all the users with same email address, although they just get another email - it would be a start
Pages: 123