home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / taverage.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-31  |  2.0 KB  |  47 lines

  1. // taverage.cpp
  2. //
  3. // Example program uses the array template.  The program reads in a set
  4. // of test scores with values between 0 and 100, stuffs each one into an
  5. // array, and then gets the average score which is then printed to standard
  6. // output.  This example is the template version of "average.cxx".  Using
  7. // a template really gets rid of the need for the integer class.
  8. //
  9. #include <iostream.h>
  10. #include <cm/include/cmtarray.h>
  11.  
  12. const int MIN = 0;                               // Define minimum and
  13. const int MAX = 100;                             // maximum score constants.
  14.  
  15. int main()                                       // Start main.
  16. {
  17.   CmTArray<int> array;                           // Declare array.
  18.   int           howMany;                         // Number of scores.
  19.   int           input;                           // Input score.
  20.  
  21.   cout << "How many scores will you enter: " << flush;
  22.   cin  >> howMany;                               // Read in number of scores.
  23.   array.resize(howMany);                         // Set array size.
  24.  
  25.   int ii = 0;                                    // Loop counter.
  26.   while (ii < howMany)                           // While still in range,
  27.   {
  28.     cout << "Score " << ++ii << ": " << flush;
  29.     cin  >> input;                               // read score.
  30.     if (input < MIN || input > MAX)              // Check score range.
  31.     {
  32.       cout << "Score must be between " << MIN << // Bad range.
  33.               "and " << MAX << endl; ii--;
  34.     }
  35.     else
  36.       array.add(input);                          // Copy score into array.
  37.   }
  38.  
  39.   CmTArrayIterator<int> iterator(array);         // Create an iterator.
  40.   int                   average = 0;             // Start total at zero.
  41.   while (iterator)                               // While still iterating,
  42.     average += iterator++;                       // add score to total.
  43.                                                  // Print average.
  44.   cout << "\nAverage score is " << average / howMany << ".\n";
  45.   return 0;
  46. }
  47.