# Not a clue

• 12-27-2003
cyberCLoWn
Not a clue
This is what I have, but I'm struggling to achieve a goal which I'll explain shortly. Here is the code

Code:

```// Practise Exercise #include <iostream> using namespace std; int main() {    int counter = 1;   int number;   int largest = 0;   int next_largest = 0;     while ( counter <= 10 )   {       cout << counter << ". Enter a number: ";       cin >> number;             // works out largest number entered by user            if ( largest < number )         largest = number;         ++counter;   }     cout << "Largest number entered:\t" << largest << '\n'         << "Second lagest number entered:\t" << next_largest << endl;   system( "pause" );   return 0; }```
I can easily work out the largest number entered by the user, but now I'm stuck on working out the second largest. I've tried many things, but I cannot get my mind around the maths to get to the second largest. Any help would be appreciated.
• 12-27-2003
erikj
Code:

```if ( largest < number ) {         next_largest=largest;         largest = number; } else if(next_largest < number)         next_largest=number;```
• 12-27-2003
Omnius
I guess the biggest problem is that you've written your code already, but you haven't worked out yet what you want the code to do ;)

Even for a program this small, it's worth getting the steps right in your mind before you code.
• 12-27-2003
cyberCLoWn
I appreciate the good tip Omnibus. See, I wrote this program to correspond with the requirements of an exercise I was given. Then about 2 exercises later it returned to the one I had written and asked for it to produce the second largest number along with the largest. Normally I would have written out the steps, but this time I had to add to the original program.

erikj I am highly impressed! I sat for a while trying to figure it out. How may I ask do you figure problems like this out, or have you done it before?
• 12-27-2003
erikj
The problem isn't really that hard and i haven't done it before. You will solve problems like these with ease in a couple of months, trust me.

For me it helps to try to visualize the problem on paper before coding it. When you have a solution or two that might work, try to code it.
• 12-28-2003
ripper079
What happends if the user desides to only enter negative numbers???
• 12-28-2003
erikj
it will still work, why shouldn't it?

but you probably want to initialize largest and next_largest with MIN_INT or equivalent
• 12-28-2003
Omnius
Quote:

Originally posted by erikj
but you probably want to initialize largest and next_largest with MIN_INT or equivalent
Or INT_MIN ;)
• 12-28-2003
ripper079
Quote:

What happends if the user desides to only enter negative numbers???
Quote:

it will still work, why shouldn't it?
No, because variable number will never be greater than variable largest(0).

Quote:

but you probably want to initialize largest and next_largest with MIN_INT or equivalent
Where do you see that in his/hers code (before you said it)???

Another solution(which I find to be simpler) is to store all elements(ints in this case) in a vector and sort them.
• 12-28-2003
erikj
Quote:

Where do you see that in his/hers code (before you said it)???
I didn't. Thats why a mentioned it.

Quote:

What happends if the user desides to only enter negative numbers???
• 12-28-2003
ripper079
Quote:

Where do you see that in his/hers code (before you said it)???
Quote:

I didn't. Thats why a mentioned it.