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