fork download
  1. //Cameron Pham CS1A Chapter 7, p. 444, #2
  2. //
  3. /******************************************************************************
  4. * CALCULATE RAINFALL STATISTICS
  5. * ______________________________________________________________________________
  6. * This program accepts a double value for the total rainfall for each of 12
  7. * months and stores the value in an array of doubles. It calculates and displays
  8. * the total rainfall, average monthly rainfall, and months with the highest
  9. * and lowest rainfall.
  10. * ______________________________________________________________________________
  11. * INPUT
  12. * Rainfall // Rainfall amount for each month
  13. *
  14. * OUTPUT
  15. * Total rainfall // Total rainfall for the year
  16. * Average rainfall // Average monthly rainfall
  17. * Highest month // Month with the highest rainfall
  18. * Lowest month // Month with the lowest rainfall
  19. *******************************************************************************/
  20. #include <iostream>
  21. #include <iomanip>
  22. #include <string>
  23. using namespace std;
  24.  
  25. int main()
  26. {
  27. // Initialize variables
  28. double rainfall[12]; // Stores rainfall for 12 months
  29. double totalRainfall = 0.0; // Total rainfall for the year
  30. double averageRainfall; // Average rainfall per month
  31. double highestRainfall; // Highest monthly rainfall
  32. double lowestRainfall; // Lowest monthly rainfall
  33. int highestMonth = 0; // Index of highest rainfall
  34. int lowestMonth = 0; // Index of lowest rainfall
  35.  
  36. // Store the names of the 12 months
  37. string months[12] =
  38. {
  39. "January", "February", "March", "April",
  40. "May", "June", "July", "August",
  41. "September", "October", "November", "December"
  42. };
  43.  
  44. // Ask the user to enter rainfall for each month
  45. for (int i = 0; i < 12; i++)
  46. {
  47. cout << "Enter the rainfall for " << months[i] << ": ";
  48. cin >> rainfall[i];
  49.  
  50. // Validate the input
  51. while (rainfall[i] < 0)
  52. {
  53. cout << "Error: Rainfall cannot be negative. ";
  54. cout << "Enter the rainfall for " << months[i] << " again: ";
  55. cin >> rainfall[i];
  56. }
  57.  
  58. // Add the monthly rainfall to the annual total
  59. totalRainfall += rainfall[i];
  60. }
  61.  
  62. // Initialize the highest and lowest rainfall values
  63. highestRainfall = rainfall[0];
  64. lowestRainfall = rainfall[0];
  65.  
  66. // Find the months with the highest and lowest rainfall
  67. for (int i = 1; i < 12; i++)
  68. {
  69. if (rainfall[i] > highestRainfall)
  70. {
  71. highestRainfall = rainfall[i];
  72. highestMonth = i;
  73. }
  74.  
  75. if (rainfall[i] < lowestRainfall)
  76. {
  77. lowestRainfall = rainfall[i];
  78. lowestMonth = i;
  79. }
  80. }
  81.  
  82. // Calculate the average monthly rainfall
  83. averageRainfall = totalRainfall / 12.0;
  84.  
  85. // Display the rainfall statistics
  86. cout << fixed << setprecision(2);
  87.  
  88. cout << endl << "Rainfall Statistics for the Year" << endl;
  89. cout << "--------------------------------" << endl;
  90. cout << "Total rainfall: " << totalRainfall << endl;
  91. cout << "Average monthly rainfall: " << averageRainfall << endl;
  92. cout << "Month with the highest rainfall: "
  93. << months[highestMonth] << " (" << highestRainfall << ")" << endl;
  94. cout << "Month with the lowest rainfall: "
  95. << months[lowestMonth] << " (" << lowestRainfall << ")" << endl;
  96.  
  97. return 0;
  98. }
Success #stdin #stdout 0.01s 5288KB
stdin
6 0 3 4 5 8 12 15 9 10 18 15
stdout
Enter the rainfall for January: Enter the rainfall for February: Enter the rainfall for March: Enter the rainfall for April: Enter the rainfall for May: Enter the rainfall for June: Enter the rainfall for July: Enter the rainfall for August: Enter the rainfall for September: Enter the rainfall for October: Enter the rainfall for November: Enter the rainfall for December: 
Rainfall Statistics for the Year
--------------------------------
Total rainfall: 105.00
Average monthly rainfall: 8.75
Month with the highest rainfall: November (18.00)
Month with the lowest rainfall: February (0.00)