fork download
  1. // Torrez, Elaine CS1A
  2. // ----------------------------------------------
  3. // WEATHER STATISTICS MODIFICATION
  4. // ----------------------------------------------
  5. //
  6. // This program is a modification of the Weather Statistics
  7. // program. It stores weather data for 12 months using a
  8. // structure and an enumerated data type for the months.
  9. // For each month, the user enters the total rainfall, the
  10. // high temperature, and the low temperature. The program
  11. // calculates the average monthly rainfall, total yearly
  12. // rainfall, highest temperature and the month it occurred,
  13. // lowest temperature and the month it occurred, and the
  14. // average temperature for the entire year.
  15. //
  16. // This version defines an enumerated type with enumerators
  17. // for the months and uses it to step through the array.
  18. //
  19. // ----------------------------------------------
  20. // INPUT
  21. // For each of the 12 months:
  22. // total rainfall
  23. // high temperature
  24. // low temperature
  25. //
  26. // PROCESSING
  27. // Use enum Month {JANUARY, ..., DECEMBER}
  28. // Validate input (rain >= 0, temp between -100 and 140)
  29. // Compute total rainfall
  30. // Compute average monthly rainfall
  31. // Identify highest and lowest temp + month
  32. // Compute average temperature for the year
  33. //
  34. // OUTPUT
  35. // Total rainfall for the year
  36. // Average rainfall
  37. // Highest temperature and month
  38. // Lowest temperature and month
  39. // Average yearly temperature
  40. //
  41. // ----------------------------------------------
  42.  
  43. #include <iostream>
  44. #include <iomanip>
  45. #include <string>
  46. using namespace std;
  47.  
  48. // ----------------------------------------------
  49. // Enumerated type for the months
  50. // ----------------------------------------------
  51. enum Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
  52. JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
  53.  
  54. // ----------------------------------------------
  55. // Structure to store weather data
  56. // ----------------------------------------------
  57. struct Weather {
  58. double rainfall; // Total rainfall for the month
  59. double highTemp; // Highest temperature
  60. double lowTemp; // Lowest temperature
  61. double avgTemp; // Average monthly temperature
  62. };
  63.  
  64. int main()
  65. {
  66. const int MONTHS = 12;
  67.  
  68. string monthNames[MONTHS] =
  69. { "January", "February", "March", "April", "May", "June",
  70. "July", "August", "September", "October", "November", "December" };
  71.  
  72. Weather data[MONTHS];
  73.  
  74. double totalRain = 0;
  75. double totalAvgTemp = 0;
  76.  
  77. double highest = -101;
  78. double lowest = 141;
  79. Month highMonth = JANUARY;
  80. Month lowMonth = JANUARY;
  81.  
  82. cout << fixed << setprecision(2);
  83.  
  84. // ----------------------------------------------------
  85. // INPUT SECTION (step through array using enum)
  86. // ----------------------------------------------------
  87. for (Month m = JANUARY; m <= DECEMBER; m = static_cast<Month>(m + 1))
  88. {
  89. int i = static_cast<int>(m); // use enum as index
  90.  
  91. cout << "\nEnter data for " << monthNames[i] << ":\n";
  92.  
  93. // Rainfall validation
  94. do {
  95. cout << " Total rainfall: ";
  96. cin >> data[i].rainfall;
  97. if (data[i].rainfall < 0)
  98. cout << " Error: Rainfall cannot be negative.\n";
  99. } while (data[i].rainfall < 0);
  100.  
  101. // High temperature validation
  102. do {
  103. cout << " High temperature: ";
  104. cin >> data[i].highTemp;
  105. if (data[i].highTemp < -100 || data[i].highTemp > 140)
  106. cout << " Error: Temp must be between -100 and 140.\n";
  107. } while (data[i].highTemp < -100 || data[i].highTemp > 140);
  108.  
  109. // Low temperature validation
  110. do {
  111. cout << " Low temperature: ";
  112. cin >> data[i].lowTemp;
  113. if (data[i].lowTemp < -100 || data[i].lowTemp > 140)
  114. cout << " Error: Temp must be between -100 and 140.\n";
  115. } while (data[i].lowTemp < -100 || data[i].lowTemp > 140);
  116.  
  117. // Average temperature for this month
  118. data[i].avgTemp = (data[i].highTemp + data[i].lowTemp) / 2.0;
  119.  
  120. // Add to totals
  121. totalRain += data[i].rainfall;
  122. totalAvgTemp += data[i].avgTemp;
  123.  
  124. // Track highest temp
  125. if (data[i].highTemp > highest) {
  126. highest = data[i].highTemp;
  127. highMonth = m;
  128. }
  129.  
  130. // Track lowest temp
  131. if (data[i].lowTemp < lowest) {
  132. lowest = data[i].lowTemp;
  133. lowMonth = m;
  134. }
  135. }
  136.  
  137. // ----------------------------------------------------
  138. // OUTPUT SECTION
  139. // ----------------------------------------------------
  140. cout << "\n----------------------------------------------\n";
  141. cout << "WEATHER STATISTICS REPORT\n";
  142. cout << "----------------------------------------------\n";
  143.  
  144. cout << "Total Rainfall: " << totalRain << " inches\n";
  145. cout << "Average Monthly Rainfall: " << totalRain / MONTHS << " inches\n";
  146.  
  147. cout << "Highest Temperature: " << highest
  148. << " ( " << monthNames[static_cast<int>(highMonth)] << " )\n";
  149.  
  150. cout << "Lowest Temperature: " << lowest
  151. << " ( " << monthNames[static_cast<int>(lowMonth)] << " )\n";
  152.  
  153. cout << "Average Temperature for the Year: "
  154. << totalAvgTemp / MONTHS << " degrees\n";
  155.  
  156. return 0;
  157. }
  158.  
Success #stdin #stdout 0.01s 5288KB
stdin
3.2
72
45
2.8
68
40
4.1
75
48
1.9
70
42
0.7
80
55
0.3
85
60
0.1
92
65
0.0
95
70
0.2
90
66
1.0
78
50
2.5
72
46
3.0
68
44
stdout
Enter data for January:
  Total rainfall:   High temperature:   Low temperature: 
Enter data for February:
  Total rainfall:   High temperature:   Low temperature: 
Enter data for March:
  Total rainfall:   High temperature:   Low temperature: 
Enter data for April:
  Total rainfall:   High temperature:   Low temperature: 
Enter data for May:
  Total rainfall:   High temperature:   Low temperature: 
Enter data for June:
  Total rainfall:   High temperature:   Low temperature: 
Enter data for July:
  Total rainfall:   High temperature:   Low temperature: 
Enter data for August:
  Total rainfall:   High temperature:   Low temperature: 
Enter data for September:
  Total rainfall:   High temperature:   Low temperature: 
Enter data for October:
  Total rainfall:   High temperature:   Low temperature: 
Enter data for November:
  Total rainfall:   High temperature:   Low temperature: 
Enter data for December:
  Total rainfall:   High temperature:   Low temperature: 
----------------------------------------------
WEATHER STATISTICS REPORT
----------------------------------------------
Total Rainfall: 19.80 inches
Average Monthly Rainfall: 1.65 inches
Highest Temperature: 95.00 ( August )
Lowest Temperature: 40.00 ( February )
Average Temperature for the Year: 65.67 degrees