Source of meandif3.cpp


  1: // Filename: MEANDIF3.CPP
  2: // Purpose:  This program reads in temperatures for each day of
  3: //           the week, computes and prints out their average
  4: //           (i.e. their "mean"), and then finally prints out
  5: //           the difference of each individual temperature from
  6: //           the average.

  8: #include <iostream>
  9: #include <iomanip>
 10: using namespace std;

 12: int main()
 13: {
 14:     const int NUMBER_OF_DAYS = 7;
 15:     enum DayType {SUN, MON, TUE, WED, THU, FRI, SAT}; // For array indices
 16:     typedef int TemperatureType[NUMBER_OF_DAYS]; // Define an array data type.

 18:     TemperatureType temp; // Use type definition to declare array variable.
 19:     double averageTemp;
 20:     double sum;
 21:     DayType day;

 23:     cout.setf(ios::fixed, ios::floatfield);
 24:     cout.setf(ios::showpoint);
 25:     cout << setprecision(1);

 27:     cout << endl;
 28:     cout << "This program asks for "
 29:          << NUMBER_OF_DAYS << " daily "
 30:          << "temperatures, then prints out their average " << endl;
 31:     cout << "and their differences from that average. "    << endl;
 32:     cout << endl;

 34:     cout << "Enter the " << NUMBER_OF_DAYS
 35:          << " daily temperatures "
 36:          << "as integers, then press ENTER: "           << endl;
 37:     for (day = SUN; day <= SAT; day = DayType(day + 1)) // Use enumerated values
 38:         cin >> temp[day];                               // as array indices.

 40:     sum = 0;
 41:     for (day = SUN; day <= SAT; day = DayType(day + 1))
 42:         sum = sum + temp[day];
 43:     averageTemp = sum/NUMBER_OF_DAYS;
 44:     cout << endl;
 45:     cout << "The average temperature was "
 46:          << averageTemp << "."                    << endl;
 47:     cout << endl;

 49:     cout << "The daily temperatures and their "
 50:          << "differences from the average are: "  << endl;
 51:     cout << endl;
 52:     for (day = SUN; day <= SAT; day = DayType(day + 1))
 53:         cout << setw(4) << temp[day]
 54:              << setw(7) << temp[day]-averageTemp  << endl;
 55:     cout << endl;

 57:     return 0;
 58: }