if ( num > max )
{
max2 = max;
max = num;
}
Dave Sinkula
long time no c
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
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
Okay. Look at your algorithm and think about what it does.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
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