6 Replies - 1867 Views - Last Post: 07 September 2009 - 06:49 PM Rate Topic: -----

#1 brich744  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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++)
		{
		cout<<"Please enter in a number:"<<endl;

		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  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 brich744  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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++)
		{
		cout<<"Please enter in a number:"<<endl;

		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;
}




Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • 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.
Was This Post Helpful? 1
  • +
  • -

#5 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • 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.
Was This Post Helpful? 1
  • +
  • -

#6 brich744  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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++)
		{
		cout<<"Please enter in a number:"<<endl;

		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;
}

Was This Post Helpful? 0
  • +
  • -

#7 AJOHNZ  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1