Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- Write a program that uses one struct to store the following weather informatio

ID: 3766125 • Letter: #

Question

-   Write a program that uses one struct to store the following weather information for one month:

                        Total rainfall

                        High temperature

                        Low temperature

                        Average temperature (Of High/Low temperature)

Assume the values are floats. The program should declare an array that holds 12 structs. The array can then hold weather information for up to an entire year. When the program runs, it should read the information for the year from a file called weather.dat. Each line of the file (0 to 12 lines are possible) will contain one month’s data for Total rainfall, High temperature, and Low temperature. You can assume all three values will be on the line. A typical line might be as follows:

3.2 88.1 59.3

The average temperature for the month should be calculated by the program and stored in the array with the other monthly values as the data is input. The values in the input file will be separated by blanks.

Once the data is read for all the months, the program should prompt the user to select a month (1 = Jan, 2 = Feb, etc.), and then display the Total rainfall, High temperature, Low temperature, and Average temperature for that month. If the user selects 0, the program should calculate and display the total rainfall for the year along with the average rainfall for the year. If the user selects an invalid month, the program should display a helpful error message. The program should allow the user to repeat the month selection until they are ready to exit. Remember to allow a sentinel menu option to exit. Label all output clearly and format it neatly. Use functions to modularize your program. A good programmer will check to be sure the program handles an empty file and the last record in the input file correctly, and calculates yearly values correctly when the number of months input is not 12.

Explanation / Answer

Answer:

/*** C++ PROGRAM FOR FINDING TOTAL RAINFALL ,AVERAGE RAINFALL, DISPLAYING THE WEATHER INFORMATION ******/

#include<iostream>

#include<stdlib.h>

#include<fstream>

using namespace std;

//STRUCTURE TO HOLD WEATHER INFORMATION FOR 12MONTHS

struct weather

{

float totalRain;

float highTemp;

float lowTemp;

float avgTemp;

};

//ARRAY OF STRUCT TO WEATHER INFROMATION FOR 12 MONTHS

struct weather wArr[12];

//READ FROM FILE

void ReadTemp(struct weather wArr[12])

{

ifstream tempFile("weather.txt");

if(tempFile.is_open())

{

int k1=0;

//READ WEATHER INFORMATION FROM FILE

while(tempFile>>wArr[k1].totalRain>>wArr[k1].highTemp>>wArr[k1].lowTemp)

{

k1++;

}

}

else

cout<<" FILE CANNOT BE LOADED"<<endl;

}

//CALCULATE AVERAGE TEMPERATURE FOR THE WHOLE MONTH

void calAvgTemp(struct weather wArr[12])

{

int k1=0;

for(k1=0;k1<12;k1++)

{

wArr[k1].avgTemp=(wArr[k1].highTemp+wArr[k1].lowTemp)/2;

}

}

//DISPLAY THE WEATHER INFORMATION FOR THE SELECTEDMONTH

void displayTemp(struct weather wArr[12],int k1)

{

cout<<"TOTAL RAINFALL:"<<wArr[k1-1].totalRain<<endl;

cout<<"HIGH TEMPERATURE:"<<wArr[k1-1].highTemp<<endl;

cout<<"LOW TEMPERATURE:"<<wArr[k1-1].lowTemp<<endl;

cout<<"AVERAGE TEMPERATURE:"<<wArr[k1-1].avgTemp<<endl;

}

void calculateAvgTotal(struct weather * wArr)

{

float tRain=0;

float aRain;

//FIND THE TOTALRAINFALL FOR THE YEAR

for(int k1=0;k1<12;k1++)

{

tRain+=wArr[k1].totalRain;

}

//DISPLAY THE TOTAL AND AVERAGE RAINFALL FOR THE YEAR

cout<<"TOTAL RAINFALL FOR THE YEAR :"<<tRain<<endl;

cout<<"AVERAGE RAINFALL FOR THE YEAR :"<<tRain/12<<endl;

}

//MAIN METHOD

int main()

{

int selectMonth;

int userChoice1;

//READ TEMPERATURE VALUES FROM FILE

ReadTemp( wArr);

//CALCULATE AVERAGE TEMPERATURE FOREACH MONTH

calAvgTemp(wArr);

do

{

cout<<"select a month(1=Jan,2=Feb,3=Mar,4=Apr,....12=Dec)"<<endl;

cin>>selectMonth;

//DISPLY THE TOTALRAINFALL, HIGH LOW AVERAGE //TEMPERATURE FOR THE SELECT MONTH

if(selectMonth>0&&selectMonth<=12)

displayTemp(wArr,selectMonth);

//DISPLAY THE TOAL RAINFALL AND AVERAGE RAINFALL

else if(selectMonth==0)

calculateAvgTotal(wArr);

else

cout<<"INVALID CHOICE"<<endl;

cout<<"DO U WANT TO CONTINUE (1 for yes / 2 for NO)"<<endl;

cin>>userChoice1;

}while(userChoice1==1);

return 0;

}//END MAIN