# Thread: Not a clue

1. ## 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.

2. Code:
```if ( largest < number )
{
next_largest=largest;
largest = number;
}
else if(next_largest < number)
next_largest=number;```

3. 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.

4. 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?

5. 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.

6. What happends if the user desides to only enter negative numbers???

7. it will still work, why shouldn't it?

but you probably want to initialize largest and next_largest with MIN_INT or equivalent

8. Originally posted by erikj
but you probably want to initialize largest and next_largest with MIN_INT or equivalent
Or INT_MIN

9. What happends if the user desides to only enter negative numbers???
it will still work, why shouldn't it?
No, because variable number will never be greater than variable largest(0).

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.

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

What happends if the user desides to only enter negative numbers???
If you knew the answer why did you ask?

11. Where do you see that in his/hers code (before you said it)???
I didn't. Thats why a mentioned it.
And in your reply to me you said
it will still work, why shouldn't it?
Iīm kinda getting double messages that it works and at the same time it doesnīt.

If you knew the answer why did you ask?
At the time I replied for the first time I saw a bug which none of the previous answer had noticied. Insteed of giving the whole code for him/her I wanna give him/her a chance to find and solve the problem and if he/she canīt Iīll help.

12. ok. point taken =)

Popular pages Recent additions