//Maxwell Brewer CS1A Chapter 8, p. 488, #5
//
/*******************************************************************************
* RAINFALL STATISTICS MODIFICATION
* _____________________________________________________________________________
* This program collects monthly rainfall data, calculates total and average
* rainfall, identifies the months with the highest and lowest rainfall,
* and sorts the months by rainfall amount.
* _____________________________________________________________________________
* INPUT
* Rainfall data for each month.
*
* OUTPUT
* Total rainfall, average monthly rainfall, highest and lowest rainfall months,
* and sorted rainfall data in descending order.
*******************************************************************************/
#include <iostream>
using namespace std;
const unsigned MONTHS = 12;
void sortMonths(unsigned[], double[], const unsigned);
void calculateStatistics(const double[], unsigned&, unsigned&, double&, double&);
int main() {
double rainfall[MONTHS];
unsigned month[MONTHS];
// Collect rainfall data with input validation
for(unsigned i = 0; i < MONTHS; i++) {
cout << "\nEnter the total rainfall for month [" << (i + 1) << "]: ";
cin >> rainfall[i];
while (rainfall[i] < 0) { // Validate input for non-negative values
cout << "The value should not be negative. Please enter again: ";
cin >> rainfall[i];
}
month[i] = i + 1; // Store the month number
}
// Calculate statistics
unsigned highestMonth, lowestMonth;
double totalRainfall, averageRainfall;
calculateStatistics(rainfall, highestMonth, lowestMonth, totalRainfall, averageRainfall);
// Display statistics
cout << "\n\nRainfall statistics for the year:\n";
cout << " Total rainfall: " << totalRainfall << " units\n";
cout << "Average monthly rainfall: " << averageRainfall << " units\n";
cout << "Month with highest rainfall: " << (highestMonth + 1) << "\n";
cout << "Month with lowest rainfall: " << (lowestMonth + 1) << "\n";
// Sort and display rainfall data by descending order
sortMonths(month, rainfall, MONTHS);
cout << "\nSorted rainfall data (highest to lowest):\n";
for(unsigned i = 0; i < MONTHS; i++) {
cout << "Month [" << month[i] << "] - Rainfall: " << rainfall[i] << " units\n";
}
return 0;
}
void calculateStatistics(const double rainfall[], unsigned& highestMonth, unsigned& lowestMonth, double& totalRainfall, double& averageRainfall) {
totalRainfall = 0.0;
highestMonth = lowestMonth = 0;
for(unsigned i = 0; i < MONTHS; i++) {
totalRainfall += rainfall[i];
if (rainfall[i] > rainfall[highestMonth]) {
highestMonth = i;
}
if (rainfall[i] < rainfall[lowestMonth]) {
lowestMonth = i;
}
}
averageRainfall = totalRainfall / MONTHS;
}
void sortMonths(unsigned months[], double rainfall[], const unsigned SIZE) {
// Selection sort to sort rainfall and corresponding months in descending order
for(unsigned i = 0; i < SIZE - 1; i++) {
unsigned largest = i;
for(unsigned j = i + 1; j < SIZE; j++) {
if (rainfall[j] > rainfall[largest]) {
largest = j;
}
}
// Swap rainfall values
double tempRain = rainfall[largest];
rainfall[largest] = rainfall[i];
rainfall[i] = tempRain;
// Swap month numbers accordingly
unsigned tempMonth = months[largest];
months[largest] = months[i];
months[i] = tempMonth;
}
}