# rgeC++ - Help with finding 2 last number of 10 numbers

This topic is 3748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

NOTICE: I typed my topic wrong, I'm trying to find the 2 highest numbers of 10 numbers. Hello, I'm doing an exercise in a book and I'm currently trying to make a program using a while loop to find the two highest numbers of 10 numbers. I can make it find the highest number, but I don't know how to make it find the second highest. Also, in the book it has a hint that says "You must input each number only once."
int main()
{
int counter; // control loop
int number; // number of items sold
int largest; // largest number
int secondLargest; // second largest number

// pre-processing stage
counter = 1;
largest = 0;
secondLargest = 0;

// processing stage
while ( counter <= 10 )
{
cout << "Enter quantity sold: ";
cin >> number;

if ( number > largest )
largest = number;

// This is where i'm stuck
if ( largest != number )
secondLargest = number;

counter++; // increment counter by 1
}

// termination phase
cout << "The largest number is " << largest << endl;
cout << "The second largest number is " << secondLargest
<< endl;

system( "pause" );
return 0;
}


The part where I'm stuck is the fact that I have no Idea how to make it check to see if it is not greater than the largest but also greater than the second largest.

##### Share on other sites
Well think about it. If you keep track of the largest and second largest numbers you've seen so far, what happens when you process a new number? There are three situations: 1) the number is bigger than both the largest and the second largest you've seen so far, 2) the number is in between those two numbers and 3) the number is less than both numbers. What should you do in each of those cases?

##### Share on other sites
Quote:
 Original post by SiCraneWell think about it. If you keep track of the largest and second largest numbers you've seen so far, what happens when you process a new number? There are three situations: 1) the number is bigger than both the largest and the second largest you've seen so far, 2) the number is in between those two numbers and 3) the number is less than both numbers. What should you do in each of those cases?

well, for 1) The number would be set to largest, for 2) the number would be set to secondLargest, and 3) nothing, could set number to itself. I think....

But, for something like 2), could you use an in between statement in C++ or any programming language ( 1 < X < 10 )????

I tryed this...
if ( number > largest )    largest = number;else if ( secondLargest < number < largest )    secondLargest = number;else     number = number;

and it did not work, the highest number worked but again for the second highest it gave me the last number inputted that did not equal the highest.

##### Share on other sites
For 1) you also need to think about what happens to the second largest number.

In C++ you need to do something like (secondLargest < number) && (number < largest). However, in Python, secondLargest < number < largest would work like you expect.

##### Share on other sites
Quote:
 Original post by SiCraneFor 1) you also need to think about what happens to the second largest number.In C++ you need to do something like (secondLargest < number) && (number < largest). However, in Python, secondLargest < number < largest would work like you expect.

I got it!!!!! The solution was
if ( number > largest ){	    largest = number;    secondLargest = secondLargest;}else if ( (secondLargest < number) && (number < largest) )    secondLargest = number;else     number = number;

Thank you very much! One more thing, I don't really care but, I don't think that was the way the book wanted me to solve it because the book has not taught the && thing yet, but this probably makes it work better anyways.

##### Share on other sites
The answer you posted isn't correct. Maybe just a typo, but:
if ( number >= largest ) // notice I used greater-or-equal here{	    // this is wrong:    // largest = number;    // secondLargest = secondLargest;         secondLargest = largest;    largest = number;}else if ( (secondLargest < number) && (number < largest) )    secondLargest = number;else     number = number; // this is not wrong per se, but doesn't do anything

At the first comment, I used greater-or-equal, this is needed for the case that the largest and second largest numbers are the same number. When you are checking for ranges, never forget the border values!

At the second comment, your code would find the largest number, but you don't set the secondLargest correctly. It has to become the former largest number.

At the third comment, that code is meaningless. Why assign 'number' to 'number', you can just leave it be and do nothing.

##### Share on other sites
Quote:
 Original post by MorrandirThe answer you posted isn't correct. Maybe just a typo, but:*** Source Snippet Removed ***At the first comment, I used greater-or-equal, this is needed for the case that the largest and second largest numbers are the same number. When you are checking for ranges, never forget the border values!At the second comment, your code would find the largest number, but you don't set the secondLargest correctly. It has to become the former largest number.At the third comment, that code is meaningless. Why assign 'number' to 'number', you can just leave it be and do nothing.

ahh... I understand. Thank you very much.

##### Share on other sites
if ( number >= largest ) {  // if code is executed here then number is greater than or equal to largest} else {  // so if code is executed here then the number must be less than largest  // this means all you need is:  if (secondLargest < number) {    // so then we have code executing for case 2 here without    // needing to use two comparisons, since we already performed    // one of them  } else {    // and of course case 3 doesn't need any additional comparisons either  }}

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633416
• Total Posts
3011780
• ### Who's Online (See full list)

There are no registered users currently online

×