fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <limits>
  5. using namespace std;
  6. // OrinTill Qimid CS1A Chapter 4, P. 294, 10
  7. //
  8. // /**************************************************************
  9. // * Calculate Total Rainfall
  10. // *
  11. // * ____________________________________________________________
  12. // * This program will calculate the average rainfall and the temperature.
  13. // * ____________________________________________________________
  14. // * INPUT
  15. // * Total Rainfall
  16. // * OUTPUT
  17. // * Average montly temperatures.
  18. // **************************************************************/
  19.  
  20. // Define the MonthlyBudget structure
  21. struct WeatherData {
  22. double totalRainfall;
  23. double highTemperature;
  24. double lowTemperature;
  25. double averageTemperature;
  26. };
  27.  
  28. int main() {
  29. const int MONTHS = 12;
  30. vector<WeatherData> year(MONTHS);
  31. double totalRainfall = 0.0;
  32. double totalAverageTemperature = 0.0;
  33. double highestTemperature = numeric_limits<double>::lowest();
  34. double lowestTemperature = numeric_limits<double>::max();
  35. int highestTempMonth = -1;
  36. int lowestTempMonth = -1;
  37. string monthNames[MONTHS] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  38.  
  39. // Input weather data for each month
  40. for (int i = 0; i < MONTHS; ++i) {
  41. cout << "Enter data for " << monthNames[i] << ":\n";
  42. cout << "Total Rainfall (in inches): ";
  43. cin >> year[i].totalRainfall;
  44. cout << "High Temperature (in Fahrenheit): ";
  45. cin >> year[i].highTemperature;
  46. cout << "Low Temperature (in Fahrenheit): ";
  47. cin >> year[i].lowTemperature;
  48.  
  49. // Calculate average temperature
  50. year[i].averageTemperature = (year[i].highTemperature + year[i].lowTemperature) / 2.0;
  51. totalRainfall += year[i].totalRainfall;
  52. totalAverageTemperature += year[i].averageTemperature;
  53.  
  54. // Determine highest and lowest temperatures
  55. if (year[i].highTemperature > highestTemperature) {
  56. highestTemperature = year[i].highTemperature;
  57. highestTempMonth = i;
  58. }
  59. if (year[i].lowTemperature < lowestTemperature) {
  60. lowestTemperature = year[i].lowTemperature;
  61. lowestTempMonth = i;
  62. }
  63.  
  64. cout << "\n";
  65. }
  66.  
  67. // Calculate average monthly rainfall
  68. double averageMonthlyRainfall = totalRainfall / MONTHS;
  69. double averageOfMonthlyAverages = totalAverageTemperature / MONTHS;
  70.  
  71. // Display results
  72. cout << "\nYearly Weather Summary:\n";
  73. cout << "Total Rainfall for the year: " << totalRainfall << " inches\n";
  74. cout << "Average Monthly Rainfall: " << averageMonthlyRainfall << " inches\n";
  75. cout << "Highest Temperature: " << highestTemperature << " F (" << monthNames[highestTempMonth] << ")\n";
  76. cout << "Lowest Temperature: " << lowestTemperature << " F (" << monthNames[lowestTempMonth] << ")\n";
  77. cout << "Average of Monthly Average Temperatures: " << averageOfMonthlyAverages << " F\n";
  78.  
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Enter data for January:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 
Enter data for February:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 
Enter data for March:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 
Enter data for April:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 
Enter data for May:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 
Enter data for June:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 
Enter data for July:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 
Enter data for August:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 
Enter data for September:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 
Enter data for October:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 
Enter data for November:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 
Enter data for December:
Total Rainfall (in inches): High Temperature (in Fahrenheit): Low Temperature (in Fahrenheit): 

Yearly Weather Summary:
Total Rainfall for the year: 0 inches
Average Monthly Rainfall: 0 inches
Highest Temperature: 0 F (January)
Lowest Temperature: 0 F (January)
Average of Monthly Average Temperatures: 0 F