fork download
  1. // Torrez, Elaine CS1A Chapter 7, P. 444, #2
  2. // *****************************************************************************************
  3. // RAINFALL STATISTICS
  4. // *--------------------------------------------------------------------------------------*
  5. // * This program asks the user to enter the total rainfall for each of 12 months. *
  6. // * The program stores the values in an array, then calculates and displays: *
  7. // * - The total rainfall for the year *
  8. // * - The average monthly rainfall *
  9. // * - The month with the highest rainfall *
  10. // * - The month with the lowest rainfall *
  11. // * *
  12. // * INPUT: *
  13. // * rainfall[12] : Rainfall amount for each month *
  14. // * *
  15. // * OUTPUT: *
  16. // * totalRainfall : Total rainfall for the year *
  17. // * avgRainfall : Average monthly rainfall *
  18. // * highestMonth : Month with the most rainfall *
  19. // * lowestMonth : Month with the least rainfall *
  20. // * *
  21. // * INPUT VALIDATION: *
  22. // * Do not accept negative numbers for rainfall amounts. *
  23. // *****************************************************************************************
  24.  
  25. #include <iostream>
  26. #include <iomanip>
  27. #include <string>
  28. using namespace std;
  29.  
  30. int main()
  31. {
  32. /***** CONSTANTS *****/
  33. const int MONTHS = 12;
  34.  
  35. /***** VARIABLE DECLARATIONS *****/
  36. double rainfall[MONTHS]; // Array to store rainfall for each month
  37. string monthNames[MONTHS] = { // Month names for display
  38. "January", "February", "March", "April", "May", "June",
  39. "July", "August", "September", "October", "November", "December"
  40. };
  41.  
  42. double total = 0.0; // Total rainfall
  43. double average; // Average rainfall
  44. double highest; // Highest rainfall
  45. double lowest; // Lowest rainfall
  46. int highMonth = 0; // Index of highest rainfall month
  47. int lowMonth = 0; // Index of lowest rainfall month
  48.  
  49. /***** INPUT SECTION *****/
  50. cout << "Enter the total rainfall (in inches) for each month:\n";
  51.  
  52. for (int i = 0; i < MONTHS; i++)
  53. {
  54. cout << monthNames[i] << ": ";
  55. cin >> rainfall[i];
  56.  
  57. // Input validation — no negative values allowed
  58. while (rainfall[i] < 0)
  59. {
  60. cout << "Invalid input. Rainfall cannot be negative. Re-enter: ";
  61. cin >> rainfall[i];
  62. }
  63.  
  64. total += rainfall[i]; // Add to total as we go
  65. }
  66.  
  67. /***** PROCESSING SECTION *****/
  68. average = total / MONTHS; // Calculate average
  69.  
  70. // Initialize highest and lowest
  71. highest = lowest = rainfall[0];
  72.  
  73. for (int i = 1; i < MONTHS; i++)
  74. {
  75. if (rainfall[i] > highest)
  76. {
  77. highest = rainfall[i];
  78. highMonth = i;
  79. }
  80. if (rainfall[i] < lowest)
  81. {
  82. lowest = rainfall[i];
  83. lowMonth = i;
  84. }
  85. }
  86.  
  87. /***** OUTPUT SECTION *****/
  88. cout << fixed << setprecision(2);
  89. cout << "\n=============================================\n";
  90. cout << " RAINFALL STATISTICS REPORT \n";
  91. cout << "=============================================\n";
  92. cout << "Total rainfall for the year : " << total << " inches\n";
  93. cout << "Average monthly rainfall : " << average << " inches\n";
  94. cout << "Highest rainfall month : " << monthNames[highMonth]
  95. << " (" << highest << " inches)\n";
  96. cout << "Lowest rainfall month : " << monthNames[lowMonth]
  97. << " (" << lowest << " inches)\n";
  98. cout << "=============================================\n";
  99.  
  100. return 0;
  101. }
  102.  
Success #stdin #stdout 0.01s 5324KB
stdin
2.5
3.1
1.2
2.8
4.0
3.5
5.1
4.8
2.2
3.0
2.9
1.6
stdout
Enter the total rainfall (in inches) for each month:
January: February: March: April: May: June: July: August: September: October: November: December: 
=============================================
           RAINFALL STATISTICS REPORT        
=============================================
Total rainfall for the year : 36.70 inches
Average monthly rainfall     : 3.06 inches
Highest rainfall month       : July (5.10 inches)
Lowest rainfall month        : March (1.20 inches)
=============================================