Learning C. Need help on this program

This is a discussion on Learning C. Need help on this program within the C Programming forums, part of the General Programming Boards category; Hi:
I am learning C and I am having a little trouble with this program.
The task was to write ...

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.

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.

"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

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.

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.