in c++ Write a modular program that analyzes a year\'s worth of rainfall data. I
ID: 3780041 • Letter: I
Question
in c++ Write a modular program that analyzes a year's worth of rainfall data. In addition to main, the program should have a getData function that accepts the total rainfall for each of the 12 months from the user, and stores it in a double array. It should also have four value-returning functions that compute and return to main the totalRainfall, averageRainfall, driestMonth, and wettestMonth. These last two functions return the number of month with the lowest and highest rainfall amounts, not the amount of rain that fell those months. Notice that this months number can be used to obtain the amount of rain that fell those months. This information should be used either by main or by a displayReport function called by main to print a summary rainfall report similar to the following.
2015 Rain Report for Neversnows County
Total Rainfall: 23.19 inches
Average Monthly Rainfall: 1.93 inches
The least rain fell in January with 0.24 inches.
The most rain fell in April with 4.29 inches.
Explanation / Answer
Here is the code for you:
#include <iostream>
using namespace std;
void getData(double Rainfalls[12])
{
for(int i = 1; i <= 12; i++) //Reading Rainfalls.
{
cout<<"Enter the rainfall of month "<<i<<": ";
cin>>Rainfalls[i-1];
}
}
double totalRainfall(double Rainfalls[12])
{
double sum = 0;
for(int i = 0; i < 12; i++)
sum += Rainfalls[i];
return sum;
}
double averageRainfall(double Rainfalls[12])
{
double sum = 0;
for(int i = 0; i < 12; i++)
sum += Rainfalls[i];
return sum/12.0;
}
int driestMonth(double Rainfalls[12])
{
int low = 0;
for(int i = 0; i < 12; i++)
if(Rainfalls[i] < Rainfalls[low])
low = i;
return low;
}
int wettestMonth(double Rainfalls[12])
{
int high = 0;
for(int i = 0; i < 12; i++)
if(Rainfalls[i] > Rainfalls[high])
high = i;
return high;
}
int main()
{
double Rainfalls[12];
string month[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
cout<<"Reading Rainfalls..."<<endl;
getData(Rainfalls);
double totalRF = totalRainfall(Rainfalls);
double avgRF = averageRainfall(Rainfalls);
int dryMonthIndex = driestMonth(Rainfalls);
int wetMonthIndex = wettestMonth(Rainfalls);
cout<<"2015 Rain Report for Neversnows County"<<endl;
cout<<"Total Rainfall: "<<totalRF<<" inches"<<endl;
cout<<"Average Monthly Rainfall: "<<avgRF<<" inches"<<endl;
cout<<"The least rain fell in "<<month[dryMonthIndex]<<" with "<<Rainfalls[dryMonthIndex]<<" inches."<<endl;
cout<<"The most rain fell in "<<month[wetMonthIndex]<<" with "<<Rainfalls[wetMonthIndex]<<" inches."<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.