fork download
  1. //Diego Martinez CSC5 Chapter 7, P.487, #2
  2. /*******************************************************************************
  3. * CALCULATE RAINFALL PATTERNS
  4. * ______________________________________________________________________________
  5. * This program manages rainfall data collected over a period of 12 months. It
  6. * determines overall patterns in the data by user inputs provided.
  7. *
  8. *
  9. * Computation is based on the Formula:
  10. * Total = R1 + R2 + R3 + ... + R12
  11. * Average = Total / 12
  12. *______________________________________________________________________________
  13. * INPUT
  14. * 12 Real Numbers (No negative Numbers allowed)
  15. *
  16. * OUTPUT
  17. * Total Rainfall for the year
  18. * Average monthly rainfall
  19. * Month with the highest rainfall
  20. * Month with the lowest rainfall
  21. *
  22. *******************************************************************************/
  23. #include <iostream>
  24. using namespace std;
  25.  
  26. // ===== FUNCTION PROTOTYPES =====
  27. void inputRainfall(double rain[], int size);
  28. void calculateStats(double rain[], int size, double &total, double &average, int &highest, int &lowest);
  29. void displayResults(double rain[], int size, double total, double average, int highest, int lowest);
  30.  
  31. // ===== MAIN FUNCTION =====
  32. int main()
  33. {
  34. // ===== DATA DICTIONARY =====
  35. const int SIZE = 12; // number of months
  36. double rainfall[SIZE]; // array to store rainfall values
  37. double totalRain; // total yearly rainfall
  38. double avgRain; // average monthly rainfall
  39. int highestMonth; // index of highest rainfall
  40. int lowestMonth; // index of lowest rainfall
  41.  
  42. // ===== FUNCTION CALLS =====
  43. inputRainfall(rainfall, SIZE);
  44. calculateStats(rainfall, SIZE, totalRain, avgRain, highestMonth, lowestMonth);
  45. displayResults(rainfall, SIZE, totalRain, avgRain, highestMonth, lowestMonth);
  46.  
  47. return 0;
  48. }
  49.  
  50. // ===== FUNCTION DEFINITIONS =====
  51.  
  52. // Function to input rainfall with validation
  53. void inputRainfall(double rain[], int size)
  54. {
  55. for (int i = 0; i < size; i++)
  56. {
  57. do
  58. {
  59. cout << "Enter rainfall for month " << i + 1 << ": ";
  60. cin >> rain[i];
  61.  
  62. if (rain[i] < 0)
  63. {
  64. cout << "Invalid input. Rainfall cannot be negative.\n";
  65. }
  66.  
  67. } while (rain[i] < 0);
  68. }
  69. }
  70.  
  71. // Function to calculate total, average, highest, and lowest
  72. void calculateStats(double rain[], int size, double &total, double &average, int &highest, int &lowest)
  73. {
  74. total = 0;
  75. highest = 0;
  76. lowest = 0;
  77.  
  78. for (int i = 0; i < size; i++)
  79. {
  80. total += rain[i];
  81.  
  82. if (rain[i] > rain[highest])
  83. {
  84. highest = i;
  85. }
  86.  
  87. if (rain[i] < rain[lowest])
  88. {
  89. lowest = i;
  90. }
  91. }
  92.  
  93. average = total / size;
  94. }
  95.  
  96. // Function to display results
  97. void displayResults(double rain[], int size, double total, double average, int highest, int lowest)
  98. {
  99. cout << "\nTotal rainfall for the year: " << total << endl;
  100. cout << "Average monthly rainfall: " << average << endl;
  101. cout << "Month with highest rainfall: Month " << highest + 1
  102. << " (" << rain[highest] << ")" << endl;
  103. cout << "Month with lowest rainfall: Month " << lowest + 1
  104. << " (" << rain[lowest] << ")" << endl;
  105. }
Success #stdin #stdout 0.01s 5284KB
stdin
2.5
3.0
1.8
4.2
5.1
3.3
2.0
6.0
4.8
3.7
2.9
1.5
stdout
Enter rainfall for month 1: Enter rainfall for month 2: Enter rainfall for month 3: Enter rainfall for month 4: Enter rainfall for month 5: Enter rainfall for month 6: Enter rainfall for month 7: Enter rainfall for month 8: Enter rainfall for month 9: Enter rainfall for month 10: Enter rainfall for month 11: Enter rainfall for month 12: 
Total rainfall for the year: 40.8
Average monthly rainfall: 3.4
Month with highest rainfall: Month 8 (6)
Month with lowest rainfall: Month 12 (1.5)