Question: Modify the code below so that the program now reads a location followed by 1 2 numbers that represent amount of rainfall for each month

Modify the code below so that the program now reads a location followed by 12 numbers that represent
amount of rainfall for each month in that location. program reads a data file that is named READRAINFALL.TXT
//reads a data file
void readRainfallForLocation( ifstream &fin, float rainfall[]);
two overloaded functions:
//output the contents of the parallel arrays of months and rainfall
void outputArrays( ofstream &fout, string months[12], float rainfall[12]);l
//output the statistics to a file
oid outputStatistics( ofstream &fout, string months[], float rainfall[],
float total, float average, int locGreatest);
Please update this code I built adding the things above.
#include
using namespace std;
// function to print the rainfall of each month
void rainfallStatistics(string month[], double rainfall[]){
double sum =0; // variable to store the total sum of rainfall
string maxMonth = month[0]; // variable to return month with maximum rainfall
double maxRainfall = INT_MIN; // variable to store the maximum rainfall
// running the loop till the last month
for(int i=0; i<12; i++){
// printing the month and rainfall
cout<< month[i]<<""<< fixed << setprecision(2)<< rainfall[i]<<" inches" << endl;
// storing the sum
sum = sum + rainfall[i];
// checking the month with highest rainfall
if(rainfall[i]> maxRainfall){
maxRainfall = rainfall[i];
maxMonth = month[i];
}
}
// calculating the average of the rainfall
double average = sum / double(12);
cout << endl;
cout << "Total for the year "<< sum <<" inches" << endl;
cout << "Average rainfall per month "<< average <<" inches" << endl;
cout << "Month with the greatest rainfall was "<< maxMonth <<" with rainfall of "<< maxRainfall <<" inches" << endl;
}
// driver program
int main()
{
// array to store the nonth name
string month[12]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
// array to store the rainfall
double rainfall1[12]={4.50,4.25,3.26,1.35,0.80,0.20,0.10,0.00,0.40,1.20,2.96,4.71};
double rainfall2[12]={5.24,4.09,3.92,2.75,2.03,1.55,0.93,1.16,1.61,0.00,5.67,6.06};
cout << "Hayward Statistics" << endl;
cout << endl;
cout<< "Month" <<""<< "Rainfall" << endl;
// calling the function for Hayward
rainfallStatistics(month, rainfall1);
cout << endl;
cout << endl;
cout << "Seattle Statistics" << endl;
cout << endl;
cout<< "Month" <<""<< "Rainfall" << endl;
// calling the function for Houston
rainfallStatistics(month, rainfall2);
return 0;
}
This is a shell for final code.
#include
#include
#include
#include
using namespace std;
//Prototypes
void readRainfallForLocation( ifstream &fin, float rainfall[]);
void findTotalandAverage(float monthlyRain[12], float &total, float &average);
int findGreatestRainLoc(float []);
void outputArrays( string months[12], float rainfall[12]);
void outputArrays( ofstream &fout, string months[12], float rainfall[12]);
void outputStatistics( string months[], float rainfall[], float total, float average, int locGreatest);
void outputStatistics( ofstream &fout, string months[], float rainfall[], float total, float average, int locGreatest);
int main()
{
//declare and initialize an array of strings containing month abbreviations
string months[12]={"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
float monthlyRainfall[12]; //Stores monthly rainfall for an entire year for one location
string location;
float total, average;
int locGreatest; //array index of greatest rainfall
ifstream fin;
ofstream fout;
fin.open("READRAINFALL.TXT");
//fixme! Open both input and output files and check that they opened successfully
//Master loop
getline(fin, location); //priming input gets first location
while (!fin.eof())
{
readRainfallForLocation (fin, monthlyRainfall); //input the twelve monthly rain amounts
//fixme! Call the other functions as done in the zyLab
//add two overloaded output to file functions as shown in protoypes
fin.ignore(); //skip over the line feed left in the buffer after last number was read
getline(fin, location); //get the next location until End of File
}
return 0;
}
//All functions are omitted for now.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!