second largest num

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

second largest num

 
0
  #1
Aug 30th, 2005
Hi, I can find the largest number. But I can not find the second largest number. I dont know where to start.
Attached Files
File Type: cpp Cpp1.cpp (1.1 KB, 29 views)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,433
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: second largest num

 
0
  #2
Aug 30th, 2005
  1. if ( num > max )
  2. {
  3. max2 = max;
  4. max = num;
  5. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: second largest num

 
0
  #3
Aug 30th, 2005
Erm,

  1. if (num > max) {
  2. max2 = max;
  3. max = num;
  4. }
  5. else if (num > max2) {
  6. max2 = num;
  7. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: second largest num

 
0
  #4
Aug 30th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

Re: second largest num

 
0
  #5
Aug 31st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

Re: second largest num

 
0
  #6
Aug 31st, 2005
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.
Attached Files
File Type: cpp Cpp1.cpp (1.7 KB, 11 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,052
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: second largest num

 
0
  #7
Aug 31st, 2005
Okay. Look at your algorithm and think about what it does.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

Re: second largest num

 
0
  #8
Aug 31st, 2005
let me see, i'm thinking I need two inputs. Let me look at it closer
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

Re: second largest num

 
0
  #9
Aug 31st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 14
Reputation: m7r23 is an unknown quantity at this point 
Solved Threads: 0
m7r23 m7r23 is offline Offline
Newbie Poster

Re: second largest num

 
0
  #10
Aug 31st, 2005
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.

  1. else if (max == max2)
  2. {
  3. max2=num;
  4.  
  5. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC