Write a program using structures to store the following weather information: Tot
ID: 3690452 • Letter: W
Question
Write a program using structures to store the following weather information: Total Rainfall High Temperature Low Temperature Average Temperature The program should have an 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
Comment
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
struct weather_months
{
int total_rain,high_temp,low_temp;
float avg_temp;
};
int main()
{
int i,n=5,total_rainfall=0,highest_temp,highest_temp_month,lowest_temp,lowest_temp_month;
float avg_avg;
string month_name[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
weather_months * month = new weather_months[n];
for(i=0;i<n;i++)
{
cout << " Enter the Total Rainfall in month of "<< month_name[i] <<": ";
cin>>month[i].total_rain;
cout << " Enter the High Temperature in month of "<< month_name[i] <<": ";
cin>>month[i].high_temp;
cout << " Enter the Low Temperature in month of "<< month_name[i] <<": ";
cin>>month[i].low_temp;
month[i].avg_temp = (float) (month[i].high_temp + month[i].low_temp)/2;
//printf(" %f",month[i].avg_temp);
}
highest_temp=month[0].high_temp;
lowest_temp = month[0].low_temp;
for(i=0;i<n;i++)
{
total_rainfall=total_rainfall + month[i].total_rain;
if(month[i].high_temp > highest_temp)
{
highest_temp = month[i].high_temp;
highest_temp_month = i;
}
if(month[i].low_temp < lowest_temp)
{
lowest_temp = month[i].low_temp;
lowest_temp_month = i;
}
avg_avg=avg_avg+month[i].avg_temp;
}
cout<<" Total rainfall of the year is: "<<total_rainfall;
cout<<" Highest tempereture occurred in month of "<<month_name[highest_temp_month] <<". Temperature was "<<highest_temp;
cout<<" Lowest tempereture occurred in month of "<<month_name[lowest_temp_month] <<". Temperature was "<<lowest_temp;
cout<<" The average of all the months average temperatures is: "<< (float) (avg_avg/n);
cout<<" ";
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.