fork download
  1. //Devin Scheu CS1A Chapter 11, P. 646, #5
  2. //
  3. /**************************************************************
  4. *
  5. * CALCULATE AND DISPLAY WEATHER STATISTICS WITH ENUMERATED MONTHS
  6. * ____________________________________________________________
  7. * This program determines weather statistics for a year,
  8. * including average monthly rainfall, total rainfall, highest
  9. * and lowest temperatures, and average temperature, using
  10. * an enumerated type for months.
  11. * ____________________________________________________________
  12. * INPUT
  13. * weatherData : Weather data for each of the 12 months
  14. *
  15. * OUTPUT
  16. * weatherSummary : The formatted display of weather statistics
  17. *
  18. **************************************************************/
  19.  
  20. #include <iostream>
  21. #include <iomanip>
  22. #include <string>
  23.  
  24. using namespace std;
  25.  
  26. enum Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
  27. JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
  28.  
  29. struct WeatherData {
  30. double totalRainfall;
  31. double highTemperature;
  32. double lowTemperature;
  33. double averageTemperature;
  34. };
  35.  
  36. int main () {
  37.  
  38. //Variable Declarations
  39. const int MONTHS = 12; //OUTPUT - Number of months in a year
  40. WeatherData weatherData[MONTHS]; //INPUT - Weather data for each of the 12 months
  41. Month monthIndex; //OUTPUT - Enumerated index for months
  42. string monthNames[MONTHS] = {"January", "February", "March", "April", "May", "June",
  43. "July", "August", "September", "October", "November", "December"};
  44. double totalRainfallYear = 0; //OUTPUT - The total rainfall for the year
  45. double averageRainfallYear; //OUTPUT - The average monthly rainfall
  46. double highestTemp = -1000; //OUTPUT - The highest temperature for the year
  47. int highestTempMonth = 0; //OUTPUT - The month index of the highest temperature
  48. double lowestTemp = 1000; //OUTPUT - The lowest temperature for the year
  49. int lowestTempMonth = 0; //OUTPUT - The month index of the lowest temperature
  50. double totalAvgTemp = 0; //OUTPUT - The total of monthly average temperatures
  51. double avgOfAvgTemps; //OUTPUT - The average of all monthly average temperatures
  52. string weatherSummary; //OUTPUT - The formatted display of weather statistics
  53.  
  54. //Prompt for Input with Loop using enumerated type
  55. for (monthIndex = JANUARY; monthIndex <= DECEMBER; monthIndex = static_cast<Month>(monthIndex + 1)) {
  56. int i = static_cast<int>(monthIndex);
  57. weatherData[i].averageTemperature = 0; // Initialize to avoid uninitialized use
  58. cout << "Enter total rainfall for " << monthNames[i] << " (in inches): ";
  59. cin >> weatherData[i].totalRainfall;
  60. while (weatherData[i].totalRainfall < 0) {
  61. cout << "\nError: Please enter a non-negative amount: ";
  62. cin >> weatherData[i].totalRainfall;
  63. }
  64. cout << weatherData[i].totalRainfall << " inches" << endl;
  65.  
  66. cout << "Enter high temperature for " << monthNames[i] << " (in °F): ";
  67. cin >> weatherData[i].highTemperature;
  68. while (weatherData[i].highTemperature < -100 || weatherData[i].highTemperature > 140) {
  69. cout << "\nError: Please enter a temperature between -100 and 140°F: ";
  70. cin >> weatherData[i].highTemperature;
  71. }
  72. cout << weatherData[i].highTemperature << "°F" << endl;
  73.  
  74. cout << "Enter low temperature for " << monthNames[i] << " (in °F): ";
  75. cin >> weatherData[i].lowTemperature;
  76. while (weatherData[i].lowTemperature < -100 || weatherData[i].lowTemperature > 140) {
  77. cout << "\nError: Please enter a temperature between -100 and 140°F: ";
  78. cin >> weatherData[i].lowTemperature;
  79. }
  80. cout << weatherData[i].lowTemperature << "°F" << endl;
  81.  
  82. //Calculate average temperature
  83. weatherData[i].averageTemperature = (weatherData[i].highTemperature + weatherData[i].lowTemperature) / 2.0;
  84.  
  85. //Update yearly statistics
  86. totalRainfallYear += weatherData[i].totalRainfall;
  87. totalAvgTemp += weatherData[i].averageTemperature;
  88. if (weatherData[i].highTemperature > highestTemp) {
  89. highestTemp = weatherData[i].highTemperature;
  90. highestTempMonth = i;
  91. }
  92. if (weatherData[i].lowTemperature < lowestTemp) {
  93. lowestTemp = weatherData[i].lowTemperature;
  94. lowestTempMonth = i;
  95. }
  96. }
  97.  
  98. //Calculate Yearly Averages
  99. averageRainfallYear = totalRainfallYear / MONTHS;
  100. avgOfAvgTemps = totalAvgTemp / MONTHS;
  101.  
  102. //Separator and Output Section
  103. cout << "-------------------------------------------------------" << endl;
  104. cout << "OUTPUT:" << endl;
  105.  
  106. //Output Result
  107. cout << fixed << setprecision(2);
  108. cout << left << setw(25) << "Total Rainfall:" << right << setw(15) << totalRainfallYear << " inches" << endl;
  109. cout << left << setw(25) << "Average Monthly Rainfall:" << right << setw(15) << averageRainfallYear << " inches" << endl;
  110. cout << left << setw(25) << "Highest Temperature:" << right << setw(15) << highestTemp << "°F in " << monthNames[highestTempMonth] << endl;
  111. cout << left << setw(25) << "Lowest Temperature:" << right << setw(15) << lowestTemp << "°F in " << monthNames[lowestTempMonth] << endl;
  112. cout << left << setw(25) << "Average of Monthly Averages:" << right << setw(15) << avgOfAvgTemps << "°F" << endl;
  113.  
  114. } //end of main()
Success #stdin #stdout 0s 5320KB
stdin
6
73
34
4
76
36
5
78
40
8
76
42
6
73
34
4
76
36
5
78
40
8
76
42
6
73
34
4
76
36
5
78
40
8
76
42
6
73
34
4
76
36
5
78
40
8
76
42
stdout
Enter total rainfall for January (in inches): 6 inches
Enter high temperature for January (in °F): 73°F
Enter low temperature for January (in °F): 34°F
Enter total rainfall for February (in inches): 4 inches
Enter high temperature for February (in °F): 76°F
Enter low temperature for February (in °F): 36°F
Enter total rainfall for March (in inches): 5 inches
Enter high temperature for March (in °F): 78°F
Enter low temperature for March (in °F): 40°F
Enter total rainfall for April (in inches): 8 inches
Enter high temperature for April (in °F): 76°F
Enter low temperature for April (in °F): 42°F
Enter total rainfall for May (in inches): 6 inches
Enter high temperature for May (in °F): 73°F
Enter low temperature for May (in °F): 34°F
Enter total rainfall for June (in inches): 4 inches
Enter high temperature for June (in °F): 76°F
Enter low temperature for June (in °F): 36°F
Enter total rainfall for July (in inches): 5 inches
Enter high temperature for July (in °F): 78°F
Enter low temperature for July (in °F): 40°F
Enter total rainfall for August (in inches): 8 inches
Enter high temperature for August (in °F): 76°F
Enter low temperature for August (in °F): 42°F
Enter total rainfall for September (in inches): 6 inches
Enter high temperature for September (in °F): 73°F
Enter low temperature for September (in °F): 34°F
Enter total rainfall for October (in inches): 4 inches
Enter high temperature for October (in °F): 76°F
Enter low temperature for October (in °F): 36°F
Enter total rainfall for November (in inches): 5 inches
Enter high temperature for November (in °F): 78°F
Enter low temperature for November (in °F): 40°F
Enter total rainfall for December (in inches): 8 inches
Enter high temperature for December (in °F): 76°F
Enter low temperature for December (in °F): 42°F
-------------------------------------------------------
OUTPUT:
Total Rainfall:                    69.00 inches
Average Monthly Rainfall:           5.75 inches
Highest Temperature:               78.00°F in March
Lowest Temperature:                34.00°F in January
Average of Monthly Averages:          56.88°F