fork download
  1. // Torrez, Elaine CS1A
  2. // ----------------------------------------------
  3. // WEATHER STATISTICS
  4. // ----------------------------------------------
  5. //
  6. // This program stores weather data for 12 months using
  7. // a structure. For each month, the user enters the total
  8. // rainfall, the high temperature, and the low temperature.
  9. // The program calculates the average monthly rainfall,
  10. // total yearly rainfall, highest temperature and the month
  11. // it occurred, lowest temperature and the month it occurred,
  12. // and the average temperature for the entire year.
  13. //
  14. // ----------------------------------------------
  15. // INPUT
  16. // For each of the 12 months:
  17. // total rainfall
  18. // high temperature
  19. // low temperature
  20. //
  21. // PROCESSING
  22. // Validate input (rain >= 0, temp between -100 and 140)
  23. // Compute total rainfall
  24. // Compute average monthly rainfall
  25. // Identify highest and lowest temperature + month
  26. // Compute average of (high + low)/2 for each month
  27. //
  28. // OUTPUT
  29. // Total rainfall for the year
  30. // Average rainfall
  31. // Highest temperature and month
  32. // Lowest temperature and month
  33. // Average yearly temperature
  34. //
  35. // ----------------------------------------------
  36.  
  37. #include <iostream>
  38. #include <iomanip>
  39. #include <string>
  40. using namespace std;
  41.  
  42. // Structure to store weather data
  43. struct Weather {
  44. double rainfall; // Total rainfall for the month
  45. double highTemp; // Highest temperature
  46. double lowTemp; // Lowest temperature
  47. double avgTemp; // Average monthly temperature
  48. };
  49.  
  50. int main()
  51. {
  52. const int MONTHS = 12;
  53. string monthNames[MONTHS] =
  54. { "January", "February", "March", "April", "May", "June",
  55. "July", "August", "September", "October", "November", "December" };
  56.  
  57. Weather data[MONTHS];
  58.  
  59. double totalRain = 0;
  60. double totalAvgTemp = 0;
  61.  
  62. double highest = -101;
  63. double lowest = 141;
  64. int highMonth = 0;
  65. int lowMonth = 0;
  66.  
  67. cout << fixed << setprecision(2);
  68.  
  69. // ----------------------------------------------------
  70. // INPUT SECTION
  71. // ----------------------------------------------------
  72. for (int i = 0; i < MONTHS; i++)
  73. {
  74. cout << "\nEnter data for " << monthNames[i] << ":\n";
  75.  
  76. // Rainfall validation
  77. do {
  78. cout << " Total rainfall: ";
  79. cin >> data[i].rainfall;
  80. if (data[i].rainfall < 0)
  81. cout << " Error: Rainfall cannot be negative.\n";
  82. } while (data[i].rainfall < 0);
  83.  
  84. // High temperature validation
  85. do {
  86. cout << " High temperature: ";
  87. cin >> data[i].highTemp;
  88. if (data[i].highTemp < -100 || data[i].highTemp > 140)
  89. cout << " Error: Temp must be between -100 and 140.\n";
  90. } while (data[i].highTemp < -100 || data[i].highTemp > 140);
  91.  
  92. // Low temperature validation
  93. do {
  94. cout << " Low temperature: ";
  95. cin >> data[i].lowTemp;
  96. if (data[i].lowTemp < -100 || data[i].lowTemp > 140)
  97. cout << " Error: Temp must be between -100 and 140.\n";
  98. } while (data[i].lowTemp < -100 || data[i].lowTemp > 140);
  99.  
  100. // Average temperature for this month
  101. data[i].avgTemp = (data[i].highTemp + data[i].lowTemp) / 2.0;
  102.  
  103. // Add to totals
  104. totalRain += data[i].rainfall;
  105. totalAvgTemp += data[i].avgTemp;
  106.  
  107. // Track highest temp
  108. if (data[i].highTemp > highest) {
  109. highest = data[i].highTemp;
  110. highMonth = i;
  111. }
  112.  
  113. // Track lowest temp
  114. if (data[i].lowTemp < lowest) {
  115. lowest = data[i].lowTemp;
  116. lowMonth = i;
  117. }
  118. }
  119.  
  120. // ----------------------------------------------------
  121. // OUTPUT SECTION
  122. // ----------------------------------------------------
  123. cout << "\n----------------------------------------------\n";
  124. cout << "WEATHER STATISTICS REPORT\n";
  125. cout << "----------------------------------------------\n";
  126.  
  127. cout << "Total Rainfall: " << totalRain << " inches\n";
  128. cout << "Average Monthly Rainfall: " << totalRain / MONTHS << " inches\n";
  129.  
  130. cout << "Highest Temperature: " << highest
  131. << " ( " << monthNames[highMonth] << " )\n";
  132.  
  133. cout << "Lowest Temperature: " << lowest
  134. << " ( " << monthNames[lowMonth] << " )\n";
  135.  
  136. cout << "Average Temperature for the Year: "
  137. << totalAvgTemp / MONTHS << " degrees\n";
  138.  
  139. return 0;
  140. }
  141.  
Success #stdin #stdout 0.01s 5308KB
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