Write a complete C++ program that inputs rainfall data collected at Chicago Midw
ID: 3588159 • Letter: W
Question
Write a complete C++ program that inputs rainfall data collected at Chicago Midway airport, and outputs the average rainfall for each year. The format of the input data is as follows. There are N>0 lines, each containing 13 values: a year followed by 12 real numbers denoting the amount of rainfall for the 12 months of January, February, March, ..., December. The last line of the input consists of a single value -1. Here's one possible input sequence:
For this input, your program should produce the following output, which is the average rainfall for each year:
Like the previous exercise, the provided code is actually spread across 2 C++ source files: "main.cpp" and "analysis.cpp". Start by reviewing the code for "main.cpp" which is visible in the editor pane --- note that this code is read-only (you cannot modify it). In particular, focus on the code within the while loop. Once the year is input, you'll see that the InputYearOfDataAndReturnAverage() function is called to input the remaining 12 values on that line, then compute and return the average. The average is stored, output, and then the next year is input and the process repeated --- until -1 is input.
Now let's review the code for the user-defined function. Above the editor pane you'll see the text "Current file: main.cpp", with a little drop-down arrow to the right. Click the drop-down and select "analysis.cpp". Then, immediately click the link "Load default template..." --- this avoids a bug that appears in some web browsers (which fail to properly load the file). You only need to do this once, the first time you view the file.
The editor pane should now be displaying the code for the user-defined function InputYearOfDataAndReturnAverage. Your job is to modify the body of this function so that it inputs exactly 12 real numbers and returns the average as a real number. Note that we are breaking the rule of "functions do not input nor output" --- in this case your function should use cin to input the necessary rainfall values for the current year. However, your function should not use cout, but must instead return the average. When the function is correctly implemented, you will pass all 3 test cases.
Current file.main.cpp File is marked as read only 1 /*main.cpp*/ 3 #include¢ostream» 4 #include 6 using namespace std; 9 // declarations of user-defined functions: 11 double InputYearOfDataAndReturnAverage(); 12 13 14 int main() 15 16 int 17 18 19 cin year; 28 year; double avg; 21 I 22 1 process each year of data and output the average: 23 / 24 while (year- -1) 25 26 27 28 29 30 31 32 avg = InputYearOfDataAndReturnAverage(); cin >> year; return e; 34 35Explanation / Answer
This program will work according to your assignment problem. Please go threw code.
InputYearOfDataAndReturnAverage function will take 12 entry from user calculate average of rainfall and return that real value to main function.
#include <iostream>
#include <string>
using namespace std;
//prototype of average calculate function.
double InputYearOfDataAndReturnAverage();
int main(void)
{
int year;
double avg;
cin >> year; //get the year number from user
while(year != -1)
{
avg = InputYearOfDataAndReturnAverage(); //function call
cout<<year<<": "<<avg<<endl; // disply average with year
cin>>year; // get the year number from user
}
return 0;
}
/*analysis.cpp*/
#include <iostream>
#include <string>
using namespace std;
//
//InputYearOfDataAndReturnAverage()
//
//Inputs 12 rainfall values --- one per month --- and returns the average.//
double InputYearOfDataAndReturnAverage(void)
{
int months=0; //month counter
double data = 0.0,total=0.0,average= 0.0; //take data , total rainfall , average variable
while(months != 12)
{
cin>>data; // take data from user
total += data; // add data to total
months++; // increment months counter
}
return (total/12); // return average to rainfall
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.