C: Min & Max via a Sentinel

I'm trying to create a C program which keeps taking in integer values until the number 0 is entered (the sentinel value). The "taking in" part is easy, but confused about evaluating the extrema. Once I can figure out how to do one way, then the other way is easy - just reversed. So, let's say, for now, that we want to calculate the minimum of all the integers entered. I understand the algorithm of how I might want to go about this. Basically, just keep updating a variable named min if the number entered is lower than the stored value of min. But, all this starts somewhere. How do I start it? There has to be a "stored value of min" to updated the "stored value of min". For example, let's say the user enters:
1
2
3
4
5
6
1
3
7
5
6
0

Then, the answer should be outputted as 1, since it is the minimum of all the non-sentinel values entered. Now, how do we store the first numbered, and the first number only, as the min and then keep updating. I might kick myself when I get the response, but I just seem stuck here. Any slight bit of help - even a hint - would be greatly appreciated! Thanks in advance. :)
Initialize min to a value greater than anything you would consider valid input.
You can introduce a boolean variable that will indicate that minimal value is set.

for example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
_Bool empty = 1;
int value;
int min;

while ( ( scnaf( "%d", &value ), value ) )
{
   if ( empty )
   {
      min = value;
      empty = 0;
   }
   else if ( value < min )
   {
      min = value;
   }
}
Last edited on
Topic archived. No new replies allowed.