Yes, min and max start at -1 but that gets overriden to what ever the first value entered is.
1 2
|
if( num == SENTINEL )
//meens if this is the first run
|
However think about how you would find a minimum...
If I said "tell me the lowest number, the numbers are: 5, 2, 10".
What do you do?
Well I say "ok 5" that's the lowest so far...
min = num; // num is 5
Then move to the next number, Wait! 2 is less then 5...
if( num < min ) //it is (true)
so now the lowest so far is 2...
min = num //num is 2
Then keep moving. 10 is
not less then 2, so do nothing to the current min and forget 10.
Finally try to keep moving, but we are out of numbers so 2 is the lowest.
This is know as the "high water mark algorithum"
http://en.wikipedia.org/wiki/Ordinary_high_water_mark (or I guess low water mark in this case.)
(The maximum works the same way, but check to see if num is
greater then the current max)