//Charlotte Davies-Kiernan CS1A Chapter 7 P.444 #2
//
/******************************************************************************
*
* Compute Rainfall Statistics
* ____________________________________________________________________________
* This program will calculate and display the total rainfall for the year,
* the average monthly rainfall, and the months with the highest and lowest
* amounts.
*
* Formula Used:
* average = totalRainfall / MONTHS
* ____________________________________________________________________________
* Input
* MONTHS :Number of months in a year
* monthNames :Array that stores the names of the months
* rainfall :Array that stores the total rainfall for each month
* Output
* totalRainfall :Sum of rainfall for all 12 months
* highest :Highest monthly rainfall amount entered
* lowest :Lowest monthly rainfall amount entered
* highestMonth :Index of the month with the highest rainfall
* lowestMonth :Index of the month with the lowest rainfall
* average :Average monthly rainfall
*****************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
//Data Dictionary
const int MONTHS = 12; //INPUT - Number of months in a year
string monthNames[MONTHS] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
//INPUT - stores the name of the months
float rainfall[MONTHS]; //INPUT - Array that stores the total rainfall for each month
float totalRainfall = 0.0; //OUTPUT - Sum of rainfall for all 12 months
float highest = 0.0; //OUTPUT - Highest monthly rainfall amount entered
float lowest = 0.0; //OUTPUT - Lowest monthly rainfall amount entered
int highestMonth = 0; //OUTPUT - Index of the month with the highest rainfall
int lowestMonth = 0; //OUTPUT - Index of the month with the lowest rainfall
float average; //OUTPUT - Average monthly rainfall
//User Input
cout << "Enter the total rainfall (in inches) for each month: " << endl;
for(int i = 0; i < MONTHS; i++){
do{
cout << monthNames[i] << ": " << endl;
cin >> rainfall [i];
if(rainfall[i] < 0){
cout << "Error: Rainfall cannot be negative. Please re-enter." << endl;
}
}while(rainfall[i] < 0);
totalRainfall += rainfall[i];
}
//Calculations
highest = lowest = rainfall[0];
for(int i = 1; i < MONTHS; i++){
if(rainfall[i] > highest){
highest = rainfall[i];
highestMonth = i;
}
if(rainfall[i] < lowest){
lowest = rainfall[i];
lowestMonth = i;
}
}
average = totalRainfall / MONTHS;
//Display Results
cout << "RAINFALL STATISTICS: " << endl;
cout << "Total rainfall for the year: " << totalRainfall << " inches" << endl;
cout << "Average monthly rainfall: " << average << " inches" << endl;
cout << "Month with highest rainfall: " << monthNames[highestMonth] << " (";
cout << highest << " inches)" << endl;
cout << "Month with lowest rainfall: " << monthNames[lowestMonth] << " (";
cout << lowest << " inches)" << endl;
return 0;
}