| | |
second largest num
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
C Syntax (Toggle Plain Text)
if ( num > max ) { max2 = max; max = num; }
"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
Erm,
C Syntax (Toggle Plain Text)
if (num > max) { max2 = max; max = num; } else if (num > max2) { max2 = num; }
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Solved Threads: 5
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:
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?
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?
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?
•
•
Join Date: Aug 2005
Posts: 14
Reputation:
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.
C Syntax (Toggle Plain Text)
else if (max == max2) { max2=num; }
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: memory allocation ptr to array? how?
- Next Thread: How to decrease the amount of memory that a process/executable consume?
| Thread Tools | Search this Thread |
Tag cloud for C
* adobe api append array arrays bash binarysearch centimeter char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest homework i/o ide include infiniteloop initialization interest intmain() iso keyboard kilometer lazy license linked linkedlist linux linuxsegmentationfault list match matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scheduling segmentationfault send shape single socketprogramming spoonfeeding stack standard strchr string strings suggestions system test testautomation unix urboc user whythiscodecausesegmentationfault win32api windows.h






