Question: First Instead of initializing the structure with given data, you will write an input function that reads in the data from a file for one
First Instead of initializing the structure with given data, you will write an input function that
reads in the data from a file for one location.
Second a data file to be read is name InputWeatherData.txt
The data file contains data for multiple locations organized as follows:
location a string on one line, followed by lines of data for rainfall, high temp, low
temp and average temp for each of the months.
Hayward
etc.
Seattle
etc.
The program will contain an outer loop that processes until endoffile and that reads
the data, location by location. Inside this loop body you will call the various functions.
first.reads the numeric data from the file. second.calls the function that outputs the numeric data in a
formatted table, third.output the statistics, as was done before
fourth.Also write overloaded output functions that output the formatted data and
statistics to a text file.
Fifth mixing numeric input with the use of getline or getchar can be tricky because cin or
file input of numeric data leaves a newline in the input buffer. So you have to use
cin.ignore or fin.ignore to clear
#include
#include
#include
using namespace std;
struct weatherData
string month;
float rain;
float highTemp;
float lowTemp;
float avgTemp;
;
float FindTotalRainfallconst weatherData year;
float FindAvgRainfallfloat total;
int locateHighestTempconst weatherData year;
int locateLowestTempconst weatherData year;
float averageAnnualTempconst weatherData year;
void outputYearDataconst weatherData year;
int main
float totalRainfall;
float avgRainfall;
int locHighestTemp;
int locLowestTemp;
float averageMonthlyTemp;
weatherData year
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun
"Jul
"Aug
"Sep",
"Oct
"Nov",
"Dec",
;
outputYearDatayear;
totalRainfall FindTotalRainfallyear;
avgRainfall FindAvgRainfalltotalRainfall;
locHighestTemp locateHighestTempyear;
locLowestTemp locateLowestTempyear;
averageMonthlyTemp averageAnnualTempyear;
cout
Total rainfall for year std::fixed setprecision totalRainfall inches";
cout
Average rainfall per month std::fixed setprecision avgRainfall inches";
cout
The highest temperature of std::fixed setprecision yearlocHighestTemphighTemp degrees F occurred in yearlocHighestTempmonth;
cout
The lowest temperature of std::fixed setprecision yearlocLowestTemplowTemp degrees F occurred in yearlocLowestTempmonth;
cout
The average annual temperature for this locality is std::fixed setprecision averageMonthlyTemp degrees F;
return ;
void outputYearDataconst weatherData year
cout setprecision fixed showpoint;
cout left setw "Month" right setw "Rain" right
setwHi temp" setw "Low Temp" setw "Avg Temp" endl;
for int j ; j ; j
cout setw left yearjmonth;
cout setw right std::fixed std::setprecision yearjrain ;
cout setw right yearjhighTemp ;
cout setw yearjlowTemp ;
cout setw yearjavgTemp endl;
float FindTotalRainfallconst weatherData year
float sum ;
for int j ; j ; j
sum yearjrain;
return sum;
this method calculates the average rain fall by dividing the total by
float FindAvgRainfallfloat total
return total ;
this method finds the index of highest temperature in array year
int locateHighestTempconst weatherData year
int maxIndex ;
float max ;
for int j ; j ; j
if yearjhighTemp max
max yearjhighTemp;
maxIndex j;
return maxIndex;
this method finds the index of lowest temperature in array year
int locateLowestTempconst weatherData year
int minIndex ;
float min ;
for int j ; j ; j
if yearjlowTemp min
min yearjlowTemp;
minIndex j;
return minIndex;
this method calculates the averageAnnualTemp in array year
float averageAnnualTempconst weatherData year
float sumAvg ;
for int j ; j ; j
sumAvg yearjavgTemp;
return sumAvg ;
c
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
