For my assignment I am to write a program that reads a series of integers from the keyboard. I am also required to use a sentinel number (a number that causes the loops to halt and display the results). Once the user has entered a series of integers ranging from 1 to 100, the user will then enter a sentinel number to stop the loops. The resulting output should show the average of the integers as well as the maximum and minimum integers.
I know how to code a loop with a sentinel number that halts it, but I am unsure of how to make the program display the maximum and minimum integers in the user given numbers. We have not learned arrays yet so I am now allowed to use them. In my notes, I have written down that the teacher said to use climits int_min and int_max I think. I am just unsure how to code these as my book does now show this.
Please note I am not going to show very much C++ as this would effectively do your assignment for you - which as you know from my instructions to questioners I do not do.
The C++ library header called climits (as used in a #include directive:
) is the C++ name for the C library limits.h header. It contains various preprocessor macros that define the sizes of the various C/C++ built in integer types of which INT_MIN and INT_MAX (note: in UPPER CASE) are two and represent the value of the minimum and maximum values an int (or signed int) can hold. For a typical compiler having 32-bit twos complement int values these will be -(2^31) and +(2^31-1), or -2147483648 and +2147483647.
Now your program will in outline look something like:
define and initialise local variables used for results
read int value from keyboard
if value is in valid range
else if value is not the sentinel value
display message to user about the value being invalid
while value not the sentinel value
NOTE: I have left out checking that the input stream (std::cin) read from is in a valid state - I shall leave as an exercise for you to add such checking.
The values you state you need to calculate are:
maximum entered value
minimum entered value
average of values entered
Maintaining the maximum and minimum values in the Do Calculations part is easy for each valid value read:
if this value is bigger than the current maximum value then set maximum equal to value
if this value is smaller than the current minimum value then set minimum equal to value
The only wrinkle is what to set the maximum and minimum values to initially - it is here that the INT_MIN and INT_MAX values could be useful:
- they are outside the valid range
- any valid value will be greater than INT_MIN, so is useful as an initial value for maximum
- any valid value will be less than INT_MAX, so is useful as an initial value for minimum
- if maximum equals INT_MIN after the loop terminates no valid values were entered
- if minimum equals INT_MAX after the loop terminates no valid values were entered
The average (arithmetic mean) is defined as the sum of all values divided by the number of values. Hence you will require variables to store both the sum of valid values entered and the count of valid values entered. Here we can see that initially there will have been no values entered (i.e. count should initially be 0) and initially there is of course a zero sum, so the sum of values entered should also be initialised to 0. Then to maintain the sum and count in the Do Calculations part of the loop:
Add this value to sum
Increment count (i.e. add one to count)
Which in C and C++ we can do using the add-assignment operator += and the increment operator ++
sum += value; // Equivalent to sum = sum + value
++count; // Equivalent to count = count + 1
Then when the loop terminates the average can be calculated as:
average equals the sum divided by the count
BUT take note that if sum and count are both integer values then the result of the division will also be an integer - all fractional parts will be lost (e.g. if sum is 999 and count is 10 then the integer result of sum / count would be 99). You can get around this by making one of the values a floating point type - in this case sum would be the best candidate and double would be a good choice of floating point type. Or you could express the result as the integer result of an integer division and the remainder using the C/C++ remainder or modulus operator, %:
remainder = sum % count;
This could be used to display the average in the form of the whole part and the remainder over the count, which using the previous example values of 999 for sum, 10 for count and 99 for the whole part of the average might look like so:
Hope this helps. And note that it seems that most of what you needed to do was sit back and think - none of the above is rocket science - the only tricky bits really are the initial minimum and maximum value and the loosing of fractional parts in the average calculation.