//Amari Mosley CSC5 Chapter 7, P. 444, #2
/***************************************************************
*
* RAINFALL STATISTICS
* ____________________________________________________________
* This program tracks the total rainfall for each of 12 months
* and displays total, average, highest, and lowest months.
*
* Computation is based on the formula:
* TotalRainfall += rainfall[]
* AverageRainfall = TotalRainfall / Num_months
* ___________________________________________________________
* INPUT
* rainfall: Monthly rainfall figures inputted by user
*
* OUTPUT
* TotalRainfall: Total rainfall for the year
* AverageRainfall: Average monthly rainfall
* HighestMonth: Month with highest rainfall
* LowestMonth: Month with lowest rainfall
*
**************************************************************/
#include <iostream>
#include <string>
using namespace std;
int main() {
const int NUM_MONTHS = 12;
string MonthNames[NUM_MONTHS] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
double rainfall[NUM_MONTHS];
double TotalRainfall = 0.0;
int HighIndex = 0;
int LowIndex = 0;
cout << "Enter the total rainfall for each of the 12 months:\n";
for (int count = 0; count < NUM_MONTHS; count++) {
do {
cout << MonthNames[count] << ": ";
cin >> rainfall[count];
if (rainfall[count] < 0) {
cout << "Error: Rainfall cannot be negative. Please try again.\n";
}
} while (rainfall[count] < 0);
TotalRainfall += rainfall[count];
}
for (int count = 1; count < NUM_MONTHS; count++) {
if (rainfall[count] > rainfall[HighIndex]) {
HighIndex = count;
}
if (rainfall[count] < rainfall[LowIndex]) {
LowIndex = count;
}
}
double AverageRainfall = TotalRainfall / NUM_MONTHS;
cout << endl;
cout << " \n RAINFALL STATISTICS REPORT \n ";
cout << "Total Annual Rainfall: " << TotalRainfall << " inches \n ";
cout << "Average Monthly Rainfall: " << AverageRainfall << " inches \n ";
cout << "Highest Month: " << MonthNames[HighIndex] << " (" << rainfall[HighIndex] << " inches) \n ";
cout << "Lowest Month: " << MonthNames[LowIndex] << " (" << rainfall[LowIndex] << " inches) \n ";
return 0;
}