fork download
  1. //Maxwell Brewer CS1A Chapter 8, p. 488, #5
  2. //
  3. /*******************************************************************************
  4.  * RAINFALL STATISTICS MODIFICATION
  5.  * _____________________________________________________________________________
  6.  * This program collects monthly rainfall data, calculates total and average
  7.  * rainfall, identifies the months with the highest and lowest rainfall,
  8.  * and sorts the months by rainfall amount.
  9.  * _____________________________________________________________________________
  10.  * INPUT
  11.  * Rainfall data for each month.
  12.  *
  13.  * OUTPUT
  14.  * Total rainfall, average monthly rainfall, highest and lowest rainfall months,
  15.  * and sorted rainfall data in descending order.
  16.  *******************************************************************************/
  17.  
  18. #include <iostream>
  19. using namespace std;
  20.  
  21. const unsigned MONTHS = 12;
  22.  
  23. void sortMonths(unsigned[], double[], const unsigned);
  24. void calculateStatistics(const double[], unsigned&, unsigned&, double&, double&);
  25.  
  26. int main() {
  27. double rainfall[MONTHS];
  28. unsigned month[MONTHS];
  29.  
  30. // Collect rainfall data with input validation
  31. for(unsigned i = 0; i < MONTHS; i++) {
  32. cout << "\nEnter the total rainfall for month [" << (i + 1) << "]: ";
  33. cin >> rainfall[i];
  34.  
  35. while (rainfall[i] < 0) { // Validate input for non-negative values
  36. cout << "The value should not be negative. Please enter again: ";
  37. cin >> rainfall[i];
  38. }
  39.  
  40. month[i] = i + 1; // Store the month number
  41. }
  42.  
  43. // Calculate statistics
  44. unsigned highestMonth, lowestMonth;
  45. double totalRainfall, averageRainfall;
  46.  
  47. calculateStatistics(rainfall, highestMonth, lowestMonth, totalRainfall, averageRainfall);
  48.  
  49. // Display statistics
  50. cout << "\n\nRainfall statistics for the year:\n";
  51. cout << " Total rainfall: " << totalRainfall << " units\n";
  52. cout << "Average monthly rainfall: " << averageRainfall << " units\n";
  53. cout << "Month with highest rainfall: " << (highestMonth + 1) << "\n";
  54. cout << "Month with lowest rainfall: " << (lowestMonth + 1) << "\n";
  55.  
  56. // Sort and display rainfall data by descending order
  57. sortMonths(month, rainfall, MONTHS);
  58. cout << "\nSorted rainfall data (highest to lowest):\n";
  59. for(unsigned i = 0; i < MONTHS; i++) {
  60. cout << "Month [" << month[i] << "] - Rainfall: " << rainfall[i] << " units\n";
  61. }
  62.  
  63. return 0;
  64. }
  65.  
  66. void calculateStatistics(const double rainfall[], unsigned& highestMonth, unsigned& lowestMonth, double& totalRainfall, double& averageRainfall) {
  67. totalRainfall = 0.0;
  68. highestMonth = lowestMonth = 0;
  69.  
  70. for(unsigned i = 0; i < MONTHS; i++) {
  71. totalRainfall += rainfall[i];
  72.  
  73. if (rainfall[i] > rainfall[highestMonth]) {
  74. highestMonth = i;
  75. }
  76.  
  77. if (rainfall[i] < rainfall[lowestMonth]) {
  78. lowestMonth = i;
  79. }
  80. }
  81.  
  82. averageRainfall = totalRainfall / MONTHS;
  83. }
  84.  
  85. void sortMonths(unsigned months[], double rainfall[], const unsigned SIZE) {
  86. // Selection sort to sort rainfall and corresponding months in descending order
  87. for(unsigned i = 0; i < SIZE - 1; i++) {
  88. unsigned largest = i;
  89.  
  90. for(unsigned j = i + 1; j < SIZE; j++) {
  91. if (rainfall[j] > rainfall[largest]) {
  92. largest = j;
  93. }
  94. }
  95.  
  96. // Swap rainfall values
  97. double tempRain = rainfall[largest];
  98. rainfall[largest] = rainfall[i];
  99. rainfall[i] = tempRain;
  100.  
  101. // Swap month numbers accordingly
  102. unsigned tempMonth = months[largest];
  103. months[largest] = months[i];
  104. months[i] = tempMonth;
  105. }
  106. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Enter the total rainfall for month [1]: 
Enter the total rainfall for month [2]: 
Enter the total rainfall for month [3]: 
Enter the total rainfall for month [4]: 
Enter the total rainfall for month [5]: 
Enter the total rainfall for month [6]: 
Enter the total rainfall for month [7]: 
Enter the total rainfall for month [8]: 
Enter the total rainfall for month [9]: 
Enter the total rainfall for month [10]: 
Enter the total rainfall for month [11]: 
Enter the total rainfall for month [12]: 

Rainfall statistics for the year:
   Total rainfall: 1.71853e-309 units
Average monthly rainfall: 1.43211e-310 units
Month with highest rainfall: 10
Month with lowest rainfall: 11

Sorted rainfall data (highest to lowest):
Month [10] - Rainfall: 4.68616e-310 units
Month [9] - Rainfall: 4.68616e-310 units
Month [3] - Rainfall: 1.11614e-310 units
Month [1] - Rainfall: 1.11614e-310 units
Month [2] - Rainfall: 1.11614e-310 units
Month [7] - Rainfall: 1.11614e-310 units
Month [4] - Rainfall: 1.11614e-310 units
Month [6] - Rainfall: 1.11614e-310 units
Month [8] - Rainfall: 1.11614e-310 units
Month [5] - Rainfall: 3.59201e-319 units
Month [12] - Rainfall: 9.88131e-324 units
Month [11] - Rainfall: 4.94066e-324 units