In: Computer Science
IN C++
Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month.
After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.
Add additional loops to validate the input:
Test Run
This program will calculate average rainfall over a
period of years. How many years do you wish to average? 0
Years must be at least one. Please re-enter: -2
Years must be at least one. Please re-enter: 2
Year 1
Number of inches of rain for month 1? 1.555
Number of inches of rain for month 2? 3.5
Number of inches of rain for month 3? 2
Number of inches of rain for month 4? 4
Number of inches of rain for month 5? 2
Number of inches of rain for month 6? 4
Number of inches of rain for month 7? 2
Number of inches of rain for month 8? 4
Number of inches of rain for month 9? 2
Number of inches of rain for month 10? 4
Number of inches of rain for month 11? 2
Number of inches of rain for month 12? 4
The rainfall for year 1 is 35.055 and the average rainfall for the year is 2.921
Year 2
Number of inches of rain for month 1? -2
Rainfall must be zero or greater.
Number of inches of rain for month 1? 3
Number of inches of rain for month 2? 4
Number of inches of rain for month 3? 3
Number of inches of rain for month 4? 4
Number of inches of rain for month 5? 3
Number of inches of rain for month 6? 4
Number of inches of rain for month 7? 3
Number of inches of rain for month 8? 4
Number of inches of rain for month 9? 3
Number of inches of rain for month 10? 4
Number of inches of rain for month 11? 2.665
Number of inches of rain for month 12? 3.3
The rainfall for year 2 is 40.965 and the average rainfall for the year is 3.414
Over a period of 24 months, 76.020 inches of rain fell.
Average monthly rainfall for the period is 3.168 inches.
Source Code in text format (See below images of code for indentation):
#include <iostream>
#include <iomanip>
using namespace std;
/*main function*/
int main()
{
/*variables*/
double years,rainfall,total=0,sum=0;
int i,j,months=0;
/*set precision*/
cout<<fixed<<setprecision(3);
/*prompt the user to enter number of year*/
cout<<"This program will calculate average rainfall over
a"<<endl;
cout<<"period of years. How many years do you
wish to average? ";
cin>>years;
/*input validation for years*/
while(years<=0)
{
/*read years again*/
cout<<"Years must be at least one. Please
re-enter: ";
cin>>years;
}
/*outer for loop iterates for number of years*/
for(i=0;i<years;i++)
{
cout<<"Year "<<i+1<<endl;
/*inner for loop iterates for 12 months*/
for(j=1;j<=12;j++)
{
/*prompt the user to read rainfall*/
cout<<"Number of inches of rain for month
"<<j<<"? ";
cin>>rainfall;
/*input validation for rainfall*/
while(rainfall<0)
{
/*read again if invalid*/
cout<<"Rainfall must be zero or
greater."<<endl;
cout<<"Number of inches of rain for month
"<<j<<"? ";
cin>>rainfall;
}
/*calculate total,sum and increment months*/
total+=rainfall;
sum+=rainfall;
months++;
}
/*print total and average for every year*/
cout<<"The rainfall for year
"<<i<<" is "<<sum<<" and the average
rainfall for the year is "<<(double)sum/12<<"\n";
sum=0;
}
/*print number of months, total and average rainfall*/
cout<<"Over a period of "<<months<<" months,
"<<total<<" inches of rain fell."<<endl;
cout<<"Average monthly rainfall for the period is
"<<(double)total/months<<" inches.";
return 0;
}
Source Code:
Output: