944,137 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7468
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 30th, 2005
0

second largest num

Expand Post »
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, 309 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m7r23 is offline Offline
14 posts
since Aug 2005
Aug 30th, 2005
0

Re: second largest num

  1. if ( num > max )
  2. {
  3. max2 = max;
  4. max = num;
  5. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 30th, 2005
0

Re: second largest num

Erm,

  1. if (num > max) {
  2. max2 = max;
  3. max = num;
  4. }
  5. else if (num > max2) {
  6. max2 = num;
  7. }
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Aug 30th, 2005
0

Re: second largest num

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:
Quote ...
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?
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Aug 31st, 2005
0

Re: second largest num

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m7r23 is offline Offline
14 posts
since Aug 2005
Aug 31st, 2005
0

Re: second largest num

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, 76 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m7r23 is offline Offline
14 posts
since Aug 2005
Aug 31st, 2005
0

Re: second largest num

Okay. Look at your algorithm and think about what it does.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Aug 31st, 2005
0

Re: second largest num

let me see, i'm thinking I need two inputs. Let me look at it closer
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m7r23 is offline Offline
14 posts
since Aug 2005
Aug 31st, 2005
0

Re: second largest num

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
m7r23 is offline Offline
14 posts
since Aug 2005
Aug 31st, 2005
0

Re: second largest num

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

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to convert a GIF image into binary using C programming.
Next Thread in C Forum Timeline: C program help!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC