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 12 lines of data for rainfall, high temp, low
temp and average temp for each of the 12 months.
Hayward
3.85584351
2.47604653 etc.
Seattle
5.20483743
3.90513844 etc.
The program will contain an outer loop (that processes until end-of-file 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 FindTotalRainfall(const weatherData year[]);
float FindAvgRainfall(float total);
int locateHighestTemp(const weatherData year[]);
int locateLowestTemp(const weatherData year[]);
float averageAnnualTemp(const weatherData year[]);
void outputYearData(const weatherData year[]);
int main(){
float totalRainfall;
float avgRainfall;
int locHighestTemp;
int locLowestTemp;
float averageMonthlyTemp;
weatherData year[12]={
{
"Jan",4.50,62,28.70,45
},
{
"Feb", 3.50,68,22,47.50
},
{
"Mar",2.24,70,34,50
},
{
"Apr",1.98.72.30,38,55.25},
{
"May", 0.65,77,40,58},
{
"Jun,0,82.30,55.60,65.34},
{
"Jul,0,98.9,60.1,67.5},
{
"Aug,0,102.5,75.6,80.},
{
"Sep", 2.45,87.5,48.2,65.4},
{
"Oct,1.10,84,40,55},
{
"Nov",2.25,67.5,36.2,38.9},
{
"Dec", 4.09,53.4,20.5,50.0}
};
outputYearData(year);
totalRainfall = FindTotalRainfall(year);
avgRainfall = FindAvgRainfall(totalRainfall);
locHighestTemp = locateHighestTemp(year);
locLowestTemp = locateLowestTemp(year);
averageMonthlyTemp = averageAnnualTemp(year);
cout <<"
Total rainfall for year "<< std::fixed << setprecision(2)<< totalRainfall <<" inches";
cout <<"
Average rainfall per month "<< std::fixed << setprecision(2)<< avgRainfall <<" inches";
cout <<"
The highest temperature of "<< std::fixed << setprecision(2)<< year[locHighestTemp].highTemp <<" degrees F occurred in "<< year[locHighestTemp].month;
cout <<"
The lowest temperature of "<< std::fixed << setprecision(2)<< year[locLowestTemp].lowTemp <<" degrees F occurred in "<< year[locLowestTemp].month;
cout <<"
The average annual temperature for this locality is "<< std::fixed << setprecision(2)<< averageMonthlyTemp <<" degrees F";
return 0;
}
void outputYearData(const weatherData year[])
{
cout << setprecision(1)<< fixed << showpoint;
cout << left << setw(6)<< "Month" << right << setw(12)<< "Rain" << right <<
setw(18)<<"Hi temp" << setw(18)<< "Low Temp" << setw(18)<< "Avg Temp" << endl;
for (int j =0; j <12; j++){
cout << setw(6)<< left << year[j].month;
cout << setw(10)<< right << std::fixed << std::setprecision(2)<< year[j].rain <<"";
cout << setw(16)<< right << year[j].highTemp <<"";
cout << setw(18)<< year[j].lowTemp <<"";
cout << setw(15)<< year[j].avgTemp <<""<< endl;
}
}
float FindTotalRainfall(const weatherData year[]){
float sum =0.0;
for (int j =0; j <12; j++)
sum += year[j].rain;
return sum;
}
//this method calculates the average rain fall by dividing the total by 12
float FindAvgRainfall(float total){
return total /12;
}
//this method finds the index of highest temperature in array year
int locateHighestTemp(const weatherData year[]){
int maxIndex =-1;
float max =0;
for (int j =0; j <12; j++){
if (year[j].highTemp > max){
max = year[j].highTemp;
maxIndex = j;
}
}
return maxIndex;
}
//this method finds the index of lowest temperature in array year
int locateLowestTemp(const weatherData year[]){
int minIndex =-1;
float min =150;
for (int j =0; j <12; j++){
if (year[j].lowTemp < min){
min = year[j].lowTemp;
minIndex = j;
}
}
return minIndex;
}
//this method calculates the averageAnnualTemp in array year
float averageAnnualTemp(const weatherData year[]){
float sumAvg =0.0;
for (int j =0; j <12; j++)
sumAvg += year[j].avgTemp;
return sumAvg /12;
} c++

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 Databases Questions!