//Devin Scheu CS1A Chapter 7, P. 444, #2
//
/**************************************************************
*
* CALCULATE RAINFALL STATISTICS
* ____________________________________________________________
* This program determines the total rainfall, average monthly
* rainfall, and the months with the highest and lowest rainfall
* for a year.
* ____________________________________________________________
* INPUT
* monthlyRainfall : The rainfall amounts for each of the 12 months
*
* OUTPUT
* totalRainfall : The total rainfall for the year
* averageRainfall : The average monthly rainfall
* highestMonth : The month with the highest rainfall
* lowestMonth : The month with the lowest rainfall
*
**************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main () {
//Variable Declarations
const int MONTHS = 12; //OUTPUT - Number of months in a year
double monthlyRainfall[MONTHS]; //INPUT - The rainfall amounts for each of the 12 months
double totalRainfall; //OUTPUT - The total rainfall for the year
double averageRainfall; //OUTPUT - The average monthly rainfall
int highestMonth; //OUTPUT - The month with the highest rainfall
int lowestMonth; //OUTPUT - The month with the lowest rainfall
string monthNames[MONTHS] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
//Prompt for Input
for (int i = 0; i < MONTHS; i++) {
cout << "Enter rainfall for " << monthNames[i] << " (in inches): ";
cin >> monthlyRainfall[i];
while (monthlyRainfall[i] < 0) {
cout << "\nError: Please enter a non-negative number: ";
cin >> monthlyRainfall[i];
}
cout << monthlyRainfall[i] << " inches" << endl;
}
//Calculate Total and Average
totalRainfall = 0;
for (int i = 0; i < MONTHS; i++) {
totalRainfall += monthlyRainfall[i];
}
averageRainfall = totalRainfall / MONTHS;
//Find Highest and Lowest Rainfall
highestMonth = 0;
lowestMonth = 0;
for (int i = 1; i < MONTHS; i++) {
if (monthlyRainfall[i] > monthlyRainfall[highestMonth]) {
highestMonth = i;
}
if (monthlyRainfall[i] < monthlyRainfall[lowestMonth]) {
lowestMonth = i;
}
}
//Separator and Output Section
cout << "-------------------------------------------------------" << endl;
cout << "OUTPUT:" << endl;
//Output Result
cout << fixed << setprecision(2);
cout << left << setw(25) << "Total Rainfall:" << right << setw(15) << totalRainfall << " inches" << endl;
cout << left << setw(25) << "Average Rainfall:" << right << setw(15) << averageRainfall << " inches" << endl;
cout << left << setw(25) << "Highest Rainfall:" << right << setw(15) << monthNames[highestMonth] << " (" << monthlyRainfall[highestMonth] << " inches)" << endl;
cout << left << setw(25) << "Lowest Rainfall:" << right << setw(15) << monthNames[lowestMonth] << " (" << monthlyRainfall[lowestMonth] << " inches)" << endl;
} //end of main()