Hi, I can find the largest number. But I can not find the second largest number. I dont know where to start.

Attachments
//Problem Description:
//Write an C++ Program (namely PQ1.cpp) to
//input several (zero or more, depending on users) integers, 
//and output their sum and maximum and second largest value.

//Michael Roland
//August 29, 2005
//PG2.cpp
//Selection and Repetion control structures  

#include <iostream>
using namespace std;

const int SENTINEL = -100;

int main()
{
	int num; 	  
	int sum = 0;  	
	int counter = 0; 	
	int max;
	int max2;

	
	cout<<"Enter the number of users "<<endl;
	cout<<"If you're enter the amount of users then enter"<<SENTINEL<<endl;
		
	
	cin>>num;
	max=num;
	max2=num;

	while(num != SENTINEL)					
	{
		sum = sum + num; 					
		counter++;    							
		cin>>num;
			   	
		

		if (num>max)
		
			max=num;
		
//		else if (max2<max && max2 > num)
	//		max2=max;

		
	}

	cout<<"The sum of the "<<counter<<" users is "<<sum<<endl;			
	cout<<"The max number is "<<max<<endl;
	cout<<"The second largest number is "<<max2<<endl;

return 0;
 }


/*
cin>>n1>>n2
if(n1>=n2)

  [
  max=n1
  max=n2
  ]

  else
  [
  max-n2
  max=n1
  ]

  cin>>n;
  while (n 1=-999)
 
  cin>>n

*/

Though the question has been answered, 'where to start' is important.

Before you start coding, always think out what you want the program to do on paper.

For this particular problem, what you need to think about is:

If I can find the largest number, how can I use that information or process to find the second largest number?

Assuming you compare the current maximum to another number, you then update the variable with the largest number.
But that means that the number just discarded is (by definition) the second largest number yet encountered.

But you're not done yet. As Rashakil Fol pointed out, there are additional cases, where the new number may be smaller than the maxiumum but smaller than the second, so you have to account for that.

And, what if you have equal numbers for your maximum or second largest?

Ok, thanks it work. But for some odd reason I thought that I tried it just like that but it did not work for me. I will have to study this way to see where I went wrong.

ok, i tried many cases. Then only case that I found not to work is descending order. When I put the numbers in descendin order it always put the first number as it max and second max.

Attachments
//Problem Description:
//Write an C++ Program (namely PQ1.cpp) to
//input several (zero or more, depending on users) integers, 
//and output their sum and maximum and second largest value.

//Michael Roland
//August 29, 2005
//PG2.cpp
//Selection and Repetion control structures  

#include <iostream>
using namespace std;

const int SENTINEL = -100;

int main()
{
	int num; 	  
	int sum = 0;  	
	int counter = 0; 	
	int max;
	int max2;

	
	cout<<"Enter the number of users "<<endl;
	cout<<"If you're enter the amount of users then enter"<<SENTINEL<<endl;
		
	
	cin>>num;
	max=num;
	max2=num;

	while(num != SENTINEL)					
	{
		sum = sum + num; 					
		counter++;    							
		cin>>num;
			   	
		

	if (num > max) 
	{
    max2 = max;
    max = num;
	}

	else if (num > max2) 
	{
    max2 = num;
	}
		
	}

	cout<<"The sum of the "<<counter<<" users is "<<sum<<endl;			
	cout<<"The max number is "<<max<<endl;
	cout<<"The second largest number is "<<max2<<endl;

return 0;
 }


/*
ascending order
Enter the number of users
If you're enter the amount of users then enter-100
1
2
3
4
5
-100
The sum of the 5 users is 15
The max number is 5
The second largest number is 4
Press any key to continue

descending order


negative numbers
Enter the number of users
If you're enter the amount of users then enter-100
5
4
3
2
1
-100
The sum of the 5 users is 15
The max number is 5
The second largest number is 5
Press any key to continue


zero, negative, and positive
Enter the number of users
If you're enter the amount of users then enter-100
4
-2
33
-44
0
-100
The sum of the 5 users is -9
The max number is 33
The second largest number is 4
Press any key to continue





*/

let me see, i'm thinking I need two inputs. Let me look at it closer

Ok, I look at it carefully and think I found the problem. I think it was me declaring the variable max wrong. I initialized it to num and now I changed it to 0.

ok, i'm trying to have a case if the numbers are equal. I can do three numbers. But the fourth number I cant get to work.

else if (max == max2)
	{
		max2=num;
			
	}

but what if the 1st number in the array is the largest? so max should be assigned to 0 , right? if max is intialised as the first element of the array, then if the 1st element is the largest then 2nd largest will also be that number only, right?
but is max is assigned to 0 then the program wont work with negetive integers ! any possible solutions?

Assign the largest number initially, to the value of the first element of the array. Assign the 2nd largest number initially to INT_MIN (your compilers macro for the least possible value for an integer.

That makes the "mouth" for trapping the numbered values you want in the array, suitable.

(Your compiler may call it MIN_INT or something different, but you can check in the header file, limits.h, and you should include it).

If you get "stuck" in a really sticky logic situation in this problem, you can always just sort the values in the array, say descending, and then count down by one, for every new value in the array. That way duplicates don't goof it up.

This article has been dead for over six months. Start a new discussion instead.