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