# Finding the two largest numbers

Page 1 of 1

## 6 Replies - 2048 Views - Last Post: 07 September 2009 - 06:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=124436&amp;s=b3503baee21a402e34f7e330a1ee4f64&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 brich744

Reputation: 0
• Posts: 18
• Joined: 07-September 09

# Finding the two largest numbers

Posted 07 September 2009 - 09:17 AM

Hello everyone,

I am writing a program that the objective is to obtain the two largest value numbers from the ten that are input into the program. I am using a 'for' loop nested with 'if' and 'else if' statements. When I finish enter the values in the largest and second largest is still equal to the initail value that I set it to. How can I fix this? (Thanks in advance)

```

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

using namespace std;

int main()
{

int largest;
int secondlargest;
int num;

for(int counter =0;counter <= 10; counter++)
{

cin>>num;

largest = 1;
secondlargest = 0;

if(num > secondlargest)
num = secondlargest;

else if (num > largest)
num = largest;

else if(num < secondlargest)
{
largest = largest;
secondlargest = secondlargest;
}

//cout<<"The largest number is" <<"   "<<largest<<endl;
//cout<<"The second largest number is"<<"   "<<secondlargest<<endl;

}

cout<<"The largest number is" <<largest<<endl;
cout<<"The second largest number is"<<secondlargest<<endl;

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Finding the two largest numbers

### #2 KYA

• g++ jameson.cpp -o beverage

Reputation: 3151
• Posts: 19,194
• Joined: 14-September 07

## Re: Finding the two largest numbers

Posted 07 September 2009 - 09:25 AM

That's because you reset the values each iteration to 1 and 0. Move it outside of the for loop.

An example:

```#include <iostream>
using namespace std;

int main() {
int largest, secondlargest, input;
largest = 1;
secondlargest = 0;
for(int counter = 0; counter < 10; counter++){
//set inital values

//the issue you had with your code was not necessarily the idea
//but the manner in which you teasted the entered values
//it was an execution problem
cout << "\nEnter a num:";
cin >> input;

//logcial checking in as few steps as possible is the goal
if (input > largest) {
largest = input;
}
else if ((input < largest) && (input > secondlargest)){
secondlargest = input;
}
}

cout << "Final values, largest: " << largest << " secondlargest: " << secondlargest << endl;
}
```

This post has been edited by KYA: 07 September 2009 - 09:29 AM

### #3 brich744

Reputation: 0
• Posts: 18
• Joined: 07-September 09

## Re: Finding the two largest numbers

Posted 07 September 2009 - 05:26 PM

Ok so I have moved the initialization to the outside of the 'for' loop. But, after the first run the same problem happen again.

```#include <iostream>
using std::cout;
using std::cin;
using std::endl;

using namespace std;

int main()
{

int largest;
int secondlargest;
int num;

largest = 1;
secondlargest = 0;

for(int counter =0;counter < 10; counter++)
{

cin>>num;

if(num > largest)

num = largest;

else if ((num < largest) && (num > secondlargest))

num = secondlargest;

}

cout<<"The largest number is" <<largest<<endl;
cout<<"The second largest number is"<<secondlargest<<endl;

return 0;
}

```

### #4 KYA

• g++ jameson.cpp -o beverage

Reputation: 3151
• Posts: 19,194
• Joined: 14-September 07

## Re: Finding the two largest numbers

Posted 07 September 2009 - 05:36 PM

Your assignment is backwards. If the input (num) is greater, you want to assign its value to largest or secondlargest.

### #5 ericr2427

• D.I.C Regular

Reputation: 39
• Posts: 378
• Joined: 01-December 08

## Re: Finding the two largest numbers

Posted 07 September 2009 - 05:39 PM

You just need to change num = largest; to largest = num; and num = secondlargest; to secondlargest = num;. You were assigning them backwards. Also, I would initialize largest to 0 instead of 1.

### #6 brich744

Reputation: 0
• Posts: 18
• Joined: 07-September 09

## Re: Finding the two largest numbers

Posted 07 September 2009 - 06:35 PM

Ok, so the new dilemma is that when the largest updates to a new largest value and the old largest value is still larger than the secondlargest; the secondlargest does not update.

```#include <iostream>
using std::cout;
using std::cin;
using std::endl;

using namespace std;

int main()
{

int largest;
int secondlargest;
int num;

largest = 1;
secondlargest = 0;

for(int counter =0;counter < 10; counter++)
{

cin>>num;

if(num > largest)

largest = num;

else if ((num < largest) && (secondlargest < num))

secondlargest = num;

else if ((num < largest) && (secondlargest < largest))

secondlargest = largest;

cout<<"The largest number is" <<largest<<endl;
cout<<"The second largest number is"<<secondlargest<<endl;

}

return 0;
}
```

### #7 AJOHNZ

Reputation: 2
• Posts: 82
• Joined: 10-May 09

## Re: Finding the two largest numbers

Posted 07 September 2009 - 06:49 PM

inside your first if statement try assigning largest to secondlargest before you assign num to largest.

```if (num > largest)
{
secondlargest = largest;
largest = num;
}

```

also your second else if statement should not be there.

This post has been edited by AJOHNZ: 07 September 2009 - 06:48 PM