C++/C++ help


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:

   #include <climits>

) 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
         Do Calculations
       else if value is not the sentinel value
         display message to user about the value being invalid
   while value not the sentinel value
   output results

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:

   99 9/10

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.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2016 About.com. All rights reserved.