954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

second largest num

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

Attachments Cpp1.cpp (1.15KB)
m7r23
Newbie Poster
14 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 
if ( num > max )
{
   max2 = max;
   max  = num;
}
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Erm,

if (num > max) {
    max2 = max;
    max = num;
}
else if (num > max2) {
    max2 = num;
}
Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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?

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
Solved Threads: 5
 

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.

m7r23
Newbie Poster
14 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

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 Cpp1.cpp (1.74KB)
m7r23
Newbie Poster
14 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Okay. Look at your algorithm and think about what it does.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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

m7r23
Newbie Poster
14 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

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.

m7r23
Newbie Poster
14 posts since Aug 2005
Reputation Points: 10
Solved Threads: 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;
			
	}
m7r23
Newbie Poster
14 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

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?

jeevsmyd
Junior Poster
142 posts since Oct 2008
Reputation Points: 8
Solved Threads: 0
 

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.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

no , this logic works perfectly ! Thanks a ton

jeevsmyd
Junior Poster
142 posts since Oct 2008
Reputation Points: 8
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You