**This program is for C++** Write a program that defines a struct WeatherType to
ID: 3804206 • Letter: #
Question
**This program is for C++**
Write a program that defines a struct WeatherType to store the following weather data for a
particular month:
Month Name
Rainfall
High Temperature
Low Temperature
Average Temperature
The month name is a string. All other data are floating-point values. The program should have an
array of struct of 12 elements to hold weather data for 12 months of a year. When the program runs,
it should read the rainfall, high temperature, and low temperature of each month from a text file
“hw3data.txt”(information down below) and store the data in an array of struct. The average temperature of each month
should be calculated from the high temperature and low temperature of that month.
Your program should perform the following tasks:
A. Display the month name, rainfall, high temperature, low temperature, and average temperature of
each month in a neat format on the screen.
The program should also calculate and display the following information:
B. The total rainfall of the year
C. The average monthly rainfall for the year
D. The highest temperature of the year and the corresponding month name
E. The lowest temperature of the year and the corresponding month name
F. The average annual temperature, i.e., the average of the monthly average temperatures.
You should write five functions to perform task A, B, D, E, and F, but not task C (since the average
monthly rainfall can be easily calculated from the total rainfall). You have freedom to decide what
parameters to be passed into the functions and what value(s) to be returned from the functions. But
the output statements must be in the main function except the function for task A.
The input data in the text file is arranged like so:
Explanation / Answer
//Please create input file hw3data.txt in current directory and copy past following data
//========================== rainfall.cpp =============================//
#include <iostream>
#include <fstream>
using namespace std;
struct WeatherType
{
string month;
double rainFall;
double heighTemp;
double lowTemp;
double avgTemp;
};
void printWeather(WeatherType Weather[] ,int size);
double highestTemperature(WeatherType Weather[] ,int size);
double lowestTemperature(WeatherType Weather[] ,int size);
double avgTemperature(WeatherType Weather[] ,int size);
int main(){
std::ofstream fout;
fout.open("abc.txt",fstream::out);
fout << "tesing";
fout.close();
WeatherType Weather[12];
string str;
std::ifstream fin;
fin.open("hw3data.txt",fstream::in);
if(!fin){
cout <<"Not able to open file, Please check the path of file"<<endl;
return 1;
}
cout << "words :"<<endl;
int i =0;
while (fin >> str)
{
double val;
Weather[i].month = str;
fin >> val;
Weather[i].rainFall = val;
fin >> val;
Weather[i].heighTemp = val;
fin >> val;
Weather[i].lowTemp = val;
val = (Weather[i].heighTemp + Weather[i].lowTemp)/2.0;
Weather[i].avgTemp = val;
cout << str<<endl;
}
fin.close();
cout << "Weather :"<<endl;
printWeather(Weather,12);
cout << "Highest Temperature of year :"<< highestTemperature(Weather,12) <<endl;
cout << "Lowest Temperature of year :"<< lowestTemperature(Weather,12) <<endl;
cout << "Avrage Temperature of year :"<< avgTemperature(Weather,12) <<endl;
return 0;
}
void printWeather(WeatherType Weather[] ,int size){
cout << " Month Rainfall min Temp max temp avg temp ";
for (int i=0;i<size ;i++ )
{
cout <<Weather[i].month<<" "<<Weather[i].rainFall<<" " <<Weather[i].heighTemp<<" "<<Weather[i].lowTemp<<" "<<Weather[i].avgTemp<<endl;
}
}
double highestTemperature(WeatherType Weather[] ,int size){
double max = Weather[0].heighTemp;
for (int i=0;i<size ;i++ )
{
if(Weather[i].heighTemp>max){
max = Weather[i].heighTemp;
}
}
return max;
}
double lowestTemperature(WeatherType Weather[] ,int size){
double min = Weather[0].lowTemp;
for (int i=0;i<size ;i++ )
{
if(Weather[i].lowTemp<min){
min = Weather[i].lowTemp;
}
}
return min;
}
double avgTemperature(WeatherType Weather[] ,int size){
double total = 0.0;
for (int i=0;i<size ;i++ )
{
total += Weather[i].avgTemp;
}
return total/12.0;
}
//====================================================================//
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.