C++ Write a program using structures to store the following weather information:
ID: 3691187 • Letter: C
Question
C++
Write a program using structures to store the following weather information:
Total Rainfall
High Temperature
Low Temperature
Average Temperature
Use an input file called input.txt to load the data to your array of 12 structures to hold the weather data (the average temperature should be calculated). Once the data for all the months is entered, the program should calculate and display the average monthly rainfall, total rainfall for the year, highest and lowest temps for the year (and the months they happen in) and the average of all the months average temperatures.
Input temps should be between -100 and +140 and and total rainfall must not be less than 0
The information in the input file is the following:
January 0 -10 -20 -12
February 0 20 -30 -10
March 5 45 20 32
April 25 65 45 55
May 12 75 32 66
June 18 85 65 72
July 6 98 88 90
August 18 102 82 91
September 16 85 65 73
October 12 72 45 59
November 5 62 30 45
December 0 46 -15 33
This is an example of the output:
Annual Rain Analysis
Total Rain: ###
Average Rain: ###
Average of average tempature: ###
The highest temp is: ### in August
The Lowest temp is: ### in February
Explanation / Answer
weather.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
struct weather
{
string month;
int rainfall;
int minTemp;
int maxTemp;
int averageTemp;
};
int calculateTotalRain(weather w[]);
double averageRain(weather w[]);
double averageTemp(weather w[]);
int highestTemp(weather w[]);
int lowestTemp(weather w[]);
int main()
{
weather w[12];
int i=0;
//give the filepath
ifstream data(filepath,ifstream::in);
string m;
int rain=0,min=0,max=0,avg=0;
string line;
if(data.is_open())
{
while (getline(data, line))
{
istringstream iss(line);
iss>>m>>rain>>max>>min>>avg;
w[i].month=m;
w[i].rainfall=rain;
w[i].maxTemp=max;
w[i].minTemp=min;
w[i].averageTemp=avg;
i++;
}
}
data.close();
cout<<"Annual Rain Analysis"<<endl;
cout<<"Total Rain :"<<calculateTotalRain(w)<<endl;
cout<<"Average Rain :"<<averageRain(w)<<endl;
cout<<"Average of average temperatures :"<<averageTemp(w)<<endl;
int maxIndex=highestTemp(w);
cout<<"The highest temperature is :"<<w[maxIndex].month<<" "<<w[maxIndex].maxTemp<<endl;
int minIndex=lowestTemp(w);
cout<<"The lowest temperature is :"<<w[minIndex].month<<" "<<w[minIndex].minTemp<<endl;
}
int calculateTotalRain(weather w[])
{
int total=0;
for(int i=0;i<12;++i)
{
total+=w[i].rainfall;
}
return total;
}
double averageRain(weather w[])
{
int totalRain=calculateTotalRain(w);
return totalRain/(double)12;
}
double averageTemp(weather w[])
{
int total=0;
for(int i=0;i<12;++i)
{
total+=w[i].averageTemp;
}
double avg=total/(double)12;
return avg;
}
//will return the index of month having the maximum temperature
int highestTemp(weather w[])
{
int high=w[0].maxTemp;
int index=0;
for(int i=1;i<12;++i)
{
if(w[i].maxTemp>high)
{
high=w[i].maxTemp;
index=i;
}
}
return index;
}
//will return the index of month having the minimum temperature
int lowestTemp(weather w[])
{
int low=w[0].minTemp;
int index=0;
for(int i=1;i<12;++i)
{
if(w[i].minTemp<low)
{
low=w[i].minTemp;
index=i;
}
}
return index;
}
output:
Annual Rain Analysis
Total Rain :117
Average Rain :9.75
Average of average temperatures :49.5
The highest temperature is :August 102
The lowest temperature is :February -30
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.