// Andres Guzman CSC5 Chapter 7, P. 444, #2
/******************************************************************************
*
* DISPLAY RAINFALL STATISTICS
* ____________________________________________________________________________
* This program will calculate and display the total, average, and highest and
* lowest months of rainfall.
*
* Computation:
*
* total / 12
* ____________________________________________________________________________
*
* INPUT
*
* [SIZE] = Array Size
* rainfall[SIZE] = Holds the inputed values of rainfall for each month
*
* OUTPUT
*
* total = Displays the total accumulated rainfall
* months[highestmonth] = Displays highest month of rainfall
* months[lowestmonth] = Display lowest month of rainfall
*
******************************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
const int SIZE = 12; //Array size
string months[SIZE] = {"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December"}; //Initialization List for month
double rainfall[SIZE]; //Subscript to input values for rainfall
double total = 0; //Accumulator for rainfall
int i; //Cycles through subscript in for loops
//
//Initializing For Loop
for(i = 0; i < SIZE; i++)
{
cout << months[i] << " Rainfall: ";
cin >> rainfall[i];
cout << endl;
while(rainfall[i] < 0) //Input Validiation
{
cout << "Invalid Rainfall Amount, Please Try Again." << endl;
cout << months[i] << " Rainfall: ";
cin >> rainfall[i];
cout << endl;
}
total += rainfall[i]; //Adds rainfall input to total
}
double highest = rainfall[0]; //Assigns beginning of subscript to highest
double lowest = rainfall [0]; //Assigns beginning of subscript to lowest
int highestmonth = 0; //Assigns variable to subscript as a name
int lowestmonth = 0; //Assigns variable to subscript as a name
//
//Initializing For Loop
for(i = 0; i < SIZE; i++)
{
if(rainfall[i] > highest) //Finds which subscript is bigger than [0]
{
highest = rainfall[i]; //Assigns found subscript to highest
highestmonth = i; //Assigns highestmonth to subscript [0]
}
if(rainfall[i] < lowest) //Finds which subscirpt is lower than [0]
{
lowest = rainfall[i]; //Assigns found subscript to lower
lowestmonth = i; //Assigns lowestmonth to subscript [0]
}
}
cout << "\nTotal Rainfall: " << total << endl;
cout << fixed << setprecision(2); //Allows 2 decimal points for average
cout << "Average Monthly Rainfall: " << (total / 12) << endl;
cout << "Highest Month of Rainfall: " << months[highestmonth] << endl;
cout << "Lowest Month of Rainfall: " << months[lowestmonth] << endl;
return 0;
}