//Cameron Pham CS1A Chapter 5, p. 295, #10
//
/******************************************************************************
* CALCULATE AVERAGE RAINFALL
* ______________________________________________________________________________
* This program accepts an integer value for years, and it accepts a value for
* Inches of rain. It will calculate the total inches of rainfall over the years
* And the average rainfall.
* ______________________________________________________________________________
* INPUT
* Years // Number of years in desired time period
* Rainfall // Inches of rainfall of particular month
*
* OUTPUT
* Months // Total number of months
* Rainfall total // Total inches of rainfall
* Rainfall average // Average rainfall per month for the entire period
*
* INPUT VALIDATION
* Years >= 1
*******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// Initialize Variables
int years; // Number of years
float rainfall; // Rainfall in inches for a particular month
int months; // Total number of months in given years
float rainfallTotal = 0; // Total inches of rainfall in time period
float rainfallAverage; // Average rainfall per month for time period
// Ask the user for the number of years desired for calculating rainfall.
cout << "I will calculate the average amount of rainfall in inches ";
cout << "based off of the number of years and amount of rainfall you ";
cout << "provide me." << endl;
cout << "How many years of rainfall would you like me to calculate?";
cin >> years;
// Validate the number of years.
while (years < 1)
{
cout << "Please enter a number of years that is greater than 0./n";
cin >> years;
}
cout << "You have entered " << years << " year(s)" << endl;
// Outer loop runs once for each year.
for (int year = 1; year <= years; year++)
{
// Inner loop runs 12 times for each month in a year.
for (int month = 1; month <= 12; month++)
{
cout << "Enter the rainfall in inches for month " << month;
cin >> rainfall;
cout << endl;
// Validate input for rainfall.
while (rainfall < 0)
{
cout << "Enter a positive value for rainfall.";
cin >> rainfall;
}
// Add rainfall per month to total rainfall.
rainfallTotal += rainfall;
}
}
// Calculate the total number of months and average rainfall per month.
months = years * 12; // Total number of months
rainfallAverage = rainfallTotal / months;
// Display the results.
cout << fixed << setprecision(2);
cout << "The total number of months in " << years << " year(s) is ";
cout << months << " months." << endl;
cout << "The total inches of rainfall in this period is ";
cout << rainfallTotal << " inches" << endl;
cout << "The average rainfall per month for the entire period is ";
cout << rainfallAverage << " inches." << endl;
return 0;
}