I have to break it up into at least 6 smaller functions and use arrays to store
ID: 3533140 • Letter: I
Question
I have to break it up into at least 6 smaller functions and use arrays to store rainfall value (i need to use pass by value, pass by reference, passing arrays) How do i go about it?
The program must do the following:
Calculates the average rainfall for 12 months. The program should ask the user for their name and the name of a file that has the name of a city on the first line and amount of rain (in inches) that fell each month of the year. Use a loop to process the data. (NOTE THAT THE CODE BELOW IS WORKING; I JUST NEED TO BREAK IT DOWN INTO 6 FUNCTIONS)
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
//Declaring/Initializing Variables
string YOURNAME, CITYNAME, LOWMONTH, HIGHMONTH;
int counter=0;
int highmonth, lowmonth, month;
float rainfall, maxrainfall, minrainfall, sum, average, asteriskcounter ;
string month1="January", month2="February", month3="March", month4="April", month5="May", month6="June", month7="July", month8="August", month9="September", month10="October", month11="November", month12="December";
ifstream inputFile;
string filename;
int rainfallList[11];
string Months[11] = {"February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
//Input
cout<<left;
cout<<setw(40)<<"Hi, please enter your name: ";
getline(cin,YOURNAME);
cout<<endl;
cout<<setw(40)<<"Please enter the filename containing the name of a city on the first line and amount of rain (in inches) that fell each month of the year: ";
cout<<endl;
cin>>filename;
inputFile.open(filename.c_str());
if (inputFile)
{
getline(inputFile, CITYNAME);
inputFile>>rainfall;
if (rainfall<0||rainfall>20)
{
cout<<endl<<endl;
cout<<"Out of range. Error!"<<endl;
cout<<endl<<endl;
return 0;
}
//Process
sum=rainfall;
minrainfall=rainfall;
maxrainfall=rainfall;
month=1;
highmonth=month;
lowmonth=month;
asteriskcounter=rainfall;
cout<<"The rainfall report for "<<CITYNAME<<" prepared by "<<YOURNAME<<": ";
cout<<endl;
cout<<endl;
cout<<"______________________________________________________________";
cout<<endl;
cout<<endl;
cout<<"January |";
for(int i=0; i<rainfall; i++)
{cout<<setw(2)<<" *";}
cout<<endl;
int j = 0;
while (counter<11)
{
inputFile>>rainfall; rainfallList[j] = rainfall; j++;
if (rainfall<0||rainfall>20)
{
cout<<endl<<endl;
cout<<"Out of range. Error!"<<endl;
cout<<endl<<endl;
return 0;
}
sum+=rainfall;
counter++;
month++;
if (rainfall>maxrainfall)
{
maxrainfall=rainfall;
highmonth=month;
}
if (rainfall<minrainfall)
{
minrainfall=rainfall;
lowmonth=month;
}
}
for (int j = 0; j<11; j++)
{
cout<<setw(10)<<Months[j]<<" | ";
for (int x = 0; x < rainfallList[j]; x++)
{
cout<<setw(2)<<"*";
}
cout<<" ";
}
cout<<"Each * represent 1 inch of rain and the rainfall amount is rounded to the nearest whole number.";
cout<<endl;
cout<<setw(30)<<"-------------1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20";
cout<<endl;
if (lowmonth==1)
LOWMONTH=month1;
if (lowmonth==2)
LOWMONTH=month2;
if (lowmonth==3)
LOWMONTH=month3;
if (lowmonth==4)
LOWMONTH=month4;
if (lowmonth==5)
LOWMONTH=month5;
if (lowmonth==6)
LOWMONTH=month6;
if (lowmonth==7)
LOWMONTH=month7;
if (lowmonth==8)
LOWMONTH=month8;
if (lowmonth==9)
LOWMONTH=month9;
if (lowmonth==10)
LOWMONTH=month10;
if (lowmonth==11)
LOWMONTH=month11;
if (lowmonth==12)
LOWMONTH=month12;
if (highmonth==1)
HIGHMONTH=month1;
if (highmonth==2)
HIGHMONTH=month2;
if (highmonth==3)
HIGHMONTH=month3;
if (highmonth==4)
HIGHMONTH=month4;
if (highmonth==5)
HIGHMONTH=month5;
if (highmonth==6)
HIGHMONTH=month6;
if (highmonth==7)
HIGHMONTH=month7;
if (highmonth==8)
HIGHMONTH=month8;
if (highmonth==9)
HIGHMONTH=month9;
if (highmonth==10)
HIGHMONTH=month10;
if (highmonth==11)
HIGHMONTH=month11;
if (highmonth==12)
HIGHMONTH=month12;
average=(sum/12);
cout<<endl;
cout<<setprecision(2)<<fixed<<setw(30)<<"Average Rainfall"<<setw(10)<<average<<"Inches";
cout<<endl;
cout<<endl;
cout <<setw(30)<<"Month with Lowest Rainfall"<<setw(10)<<LOWMONTH<<setw(10)<<"Rainfall: "<<setw(10)<<minrainfall<<"Inches";
cout<<endl;
cout<<endl;
cout <<setw(30)<<"Month with Highest Rainfall"<<setw(10)<<HIGHMONTH<<setw(10)<<"Rainfall:"<<setw(10)<<maxrainfall<<"Inches";
inputFile.close();
}
else
{
cout<<"Error opening the file.";
cout<<endl;
}
return 0;
}
Explanation / Answer
please rate - thanks
you didn't specify what the 6 functions should be,
so hope this is good
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
void intro(string&,string&);
string getlow(int);
string gethigh(int);
void heading(string,string);
void output(int[],int,int,float,float,float);
int process(ifstream& ,int[],float&,int&,float&,int&,float&);
int main()
{
//Declaring/Initializing Variables
string YOURNAME, CITYNAME;
int highmonth, month,lowmonth;
float rainfall, asteriskcounter,maxrainfall, minrainfall,sum ;
ifstream inputFile;
string filename;
int rainfallList[11];
//Input
intro(YOURNAME,filename);
inputFile.open(filename.c_str());
if (inputFile)
{
getline(inputFile, CITYNAME);
inputFile>>rainfall;
if (rainfall<0||rainfall>20)
{
cout<<endl<<endl;
cout<<"Out of range. Error!"<<endl;
cout<<endl<<endl;
return 0;
}
//Process
sum=rainfall;
minrainfall=rainfall;
maxrainfall=rainfall;
highmonth=month;
lowmonth=month;
asteriskcounter=rainfall;
heading(CITYNAME,YOURNAME);
for(int i=0; i<rainfall; i++)
{cout<<setw(2)<<" *";}
cout<<endl;
if(process(inputFile,rainfallList,maxrainfall,highmonth,minrainfall,lowmonth,sum)==0)
output(rainfallList,lowmonth,highmonth,sum,maxrainfall, minrainfall);
inputFile.close();
}
else
{
cout<<"Error opening the file.";
cout<<endl;
}
system("pause");
return 0;
}
void output(int rainfallList[],int lowmonth,int highmonth,float sum,float maxrainfall, float minrainfall)
{string Months[11] = {"February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
string LOWMONTH, HIGHMONTH;
float average;
for (int j = 0; j<11; j++)
{
cout<<setw(10)<<Months[j]<<" | ";
for (int x = 0; x < rainfallList[j]; x++)
{
cout<<setw(2)<<"*";
}
cout<<" ";
}
cout<<"Each * represent 1 inch of rain and the rainfall amount is rounded to the nearest whole number.";
cout<<endl;
cout<<setw(30)<<"-------------1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20";
cout<<endl;
LOWMONTH=getlow(lowmonth);
HIGHMONTH=gethigh(highmonth);
average=(sum/12);
cout<<endl;
cout<<setprecision(2)<<fixed<<setw(30)<<"Average Rainfall"<<setw(10)<<average<<"Inches";
cout<<endl;
cout<<endl;
cout <<setw(30)<<"Month with Lowest Rainfall"<<setw(10)<<LOWMONTH<<setw(10)<<"Rainfall: "<<setw(10)<<minrainfall<<"Inches";
cout<<endl;
cout<<endl;
cout <<setw(30)<<"Month with Highest Rainfall"<<setw(10)<<HIGHMONTH<<setw(10)<<"Rainfall:"<<setw(10)<<maxrainfall<<"Inches";
}
void intro(string& YOURNAME,string& filename)
{cout<<left;
cout<<setw(40)<<"Hi, please enter your name: ";
getline(cin,YOURNAME);
cout<<endl;
cout<<setw(40)<<"Please enter the filename containing the name of a city on the first line and amount of rain (in inches) that fell each month of the year: ";
cout<<endl;
cin>>filename;
}
string gethigh(int highmonth)
{string month1="January", month2="February", month3="March", month4="April", month5="May", month6="June", month7="July", month8="August", month9="September", month10="October", month11="November", month12="December";
string HIGHMONTH;
if (highmonth==1)
HIGHMONTH=month1;
if (highmonth==2)
HIGHMONTH=month2;
if (highmonth==3)
HIGHMONTH=month3;
if (highmonth==4)
HIGHMONTH=month4;
if (highmonth==5)
HIGHMONTH=month5;
if (highmonth==6)
HIGHMONTH=month6;
if (highmonth==7)
HIGHMONTH=month7;
if (highmonth==8)
HIGHMONTH=month8;
if (highmonth==9)
HIGHMONTH=month9;
if (highmonth==10)
HIGHMONTH=month10;
if (highmonth==11)
HIGHMONTH=month11;
if (highmonth==12)
HIGHMONTH=month12;
return HIGHMONTH;
}
string getlow(int lowmonth)
{string month1="January", month2="February", month3="March", month4="April", month5="May", month6="June", month7="July", month8="August", month9="September", month10="October", month11="November", month12="December";
string LOWMONTH;
if (lowmonth==1)
LOWMONTH=month1;
if (lowmonth==2)
LOWMONTH=month2;
if (lowmonth==3)
LOWMONTH=month3;
if (lowmonth==4)
LOWMONTH=month4;
if (lowmonth==5)
LOWMONTH=month5;
if (lowmonth==6)
LOWMONTH=month6;
if (lowmonth==7)
LOWMONTH=month7;
if (lowmonth==8)
LOWMONTH=month8;
if (lowmonth==9)
LOWMONTH=month9;
if (lowmonth==10)
LOWMONTH=month10;
if (lowmonth==11)
LOWMONTH=month11;
if (lowmonth==12)
LOWMONTH=month12;
return LOWMONTH;
}
int process(ifstream& inputFile,int rainfallList[],float& maxrainfall,
int& highmonth,float& minrainfall,int& lowmonth,float& sum)
{int j = 0;
int counter=0;
int month=1;
float rainfall;
while (counter<11)
{
inputFile>>rainfall; rainfallList[j] = rainfall; j++;
if (rainfall<0||rainfall>20)
{
cout<<endl<<endl;
cout<<"Out of range. Error!"<<endl;
cout<<endl<<endl;
return -1;
}
sum+=rainfall;
counter++;
month++;
if (rainfall>maxrainfall)
{
maxrainfall=rainfall;
highmonth=month;
}
if (rainfall<minrainfall)
{
minrainfall=rainfall;
lowmonth=month;
}
}
return 0;
}
void heading(string CITYNAME,string YOURNAME)
{cout<<"The rainfall report for "<<CITYNAME<<" prepared by "<<YOURNAME<<": ";
cout<<endl;
cout<<endl;
cout<<"______________________________________________________________";
cout<<endl;
cout<<endl;
cout<<"January |";
}