# Learning C. Need help on this program

• 01-25-2013
jwall
Learning C. Need help on this program
Hi:

I am learning C and I am having a little trouble with this program.

The task was to write a program that can determine the largest and smallest integers.

I am using a while loop to accumulate the numbers
the value 0 acts as a "sentinel" value that will stop the program.
The problem is when I enter 0 to stop the program, it will take 0 as the smallest number. This also happens when I do other values like -99 or others. Any help will be greatly appreciated.
Code:

```//Write a program that reads an integer and determines and prints whether it is od or even #include <stdio.h> #include <math.h> #include <limits.h> int main(void) {         int numbers;//numbers         int counter = 0;//counter of variables         int accumulator = 0;//adds all numbers up         int average;//computes average of all the numbers         int largest = 0;         int smallest = 0;         int counting = -99;//value that, when entered, will stop program.         while(numbers != 0)         {                 printf("Please enter a number. Enter 0 to end.\n");                 scanf("%d", &numbers);                 accumulator += numbers;                 if(numbers == 0)                         {                                 printf("Thank you.\n");;                                 if(numbers < smallest && numbers !=0 );                                 {                                         numbers = smallest;                                 }//end if                         }//end if                 else                         {                                 counter++;                         }//end if                 if(numbers % 2 == 0 && numbers != 0)                         {                                 printf("%d is an even number.\n", numbers);                         }//end if                 else if(numbers % 2 != 0 && numbers != 0)                         {                                 printf("%d is an odd number.\n", numbers);                         }//end else                         if(numbers < smallest && numbers != 0)                 {                         smallest = numbers;                 }//end if                 if(numbers > largest && numbers != 0)                 {                         largest = numbers;                 }//end if         }//end while         average = accumulator/counter;         if(counter > 2)                 {                         printf("User made %d entries\n", counter);                 }//end if         else                 {                         printf("User made 1 entry.\n");                 }//end else                         printf("Total of all entries: %d\n", accumulator);         printf("Average: %d\n", average);         printf("Largest number: %d\n ", largest);         printf("Smallest number: %d\n ", smallest); }//end main```
• 01-25-2013
std10093
There is an extra semicolon here, after the if
Code:

```if(numbers < smallest && numbers !=0 );                 {                     numbers = smallest;                 }//end if```
Remove it.
• 01-25-2013
Tclausex
You are reading in the number, processing it, then returning to the top of the while loop to test the input. So yes, 0 is being treated as an input.

The easiest solution at a beginning level would be to prompt and read the number before entering the while loop, and then again at the bottom of the loop.
Not an elegant approach since your duplicating code, but I'm betting you haven't been introduced to functions or the comma operator yet.

BTW. When you execute
Code:

`while(numbers != 0)`
numbers is uninitialized and therefore you have no idea what value it holds. If it happens to be zero, you would never enter the loop at all. So, either initialize it to your sentinel value when you declare it, or insure some assignment prior to testing it.
• 01-25-2013
jwall
Fixed it. Thanks for the help.
• 01-25-2013
jwall
but is there a way to use a non-integer as a sentinel value?
• 01-25-2013
std10093
You mean as an initializing value? Yes, there is.

Example. Ask the user to input a number until he inputs -5. (not so clever example, but ok).
Code:

```int init = -5; int input; do{   printf("Please input\n");   scanf("%d", %input); }while(input != init);```
• 01-25-2013
stahta01
Quote:

Originally Posted by jwall
but is there a way to use a non-integer as a sentinel value?

Lookup the meaning of the return value of scanf. You might be able to use it.

Tim S.
• 01-25-2013
Tclausex

By definition a sentinel is some value in the range of the datatype being used which does not represent a valid data value. So when you're determining if a sentinel is even the right approach, you have to look at what your valid data range is and can you accommodate that AND have values outside that range which would be sentinel values. E.g., if valid data is all integers from 0 to 32,000. Then a signed integer (assuming 4 bytes) would support your data range and any negative value would be a sentinel. If however, you require acceptance of negative values, you are more limited (you might use INT_MIN if you're glued to the idea of a sentinel).

If you mean 'is there some other data type?', then no as well. Integer and floating point are your two intrinsic types to use (at least I can't imagine using any derived type), and FP isn't any good for checking equality due to precision.

Another approach would be to read user input until they signal end-of-file [EOF](ctrl-z or similar depending on host environment). That would bypass the whole notion of sentinel.
• 01-25-2013
jwall
Quote:

Originally Posted by Tclausex

By definition a sentinel is some value in the range of the datatype being used which does not represent a valid data value. So when you're determining if a sentinel is even the right approach, you have to look at what your valid data range is and can you accommodate that AND have values outside that range which would be sentinel values. E.g., if valid data is all integers from 0 to 32,000. Then a signed integer (assuming 4 bytes) would support your data range and any negative value would be a sentinel. If however, you require acceptance of negative values, you are more limited (you might use INT_MIN if you're glued to the idea of a sentinel).

If you mean 'is there some other data type?', then no as well. Integer and floating point are your two intrinsic types to use (at least I can't imagine using any derived type), and FP isn't any good for checking equality due to precision.

Another approach would be to read user input until they signal end-of-file [EOF](ctrl-z or similar depending on host environment). That would bypass the whole notion of sentinel.

How would I do that here?
• 01-25-2013