In: Computer Science
Write a program that uses a structure to store the following weather data for a particular month: Total Rainfall High Temperature Low Temperature Average Temperature.
The program should have an array of 12 structures to hold weather data for an entire year.
When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated.)
Once the data are entered for all the months, the program should calculate and display the average monthly rainfall, the total rainfall for the year, the highest and lowest temperatures for the year (and the months they occurred in), and the average of all the monthly average temperatures. Input Validation: Only accept temperatures within the range between –100 and +140 degrees Fahrenheit.
C++ program :-
#include <iostream>
using namespace std;
struct weather{
double rainfall;
int low_temp;
int high_temp;
double avg_temp;
};
int main() {
weather arr[12];
double avg_rainfall;
double total_rainfall = 0.0;
double total_temp = 0.0;
double avg_mon_temp;
int lowest_temp = 140;
int low_ind = 0;
int high_ind = 0;
int highest_temp = -100;
cout<<"Please enter weather data starting from Jan (1) to Dec
(12):"<<endl;
for(int i=0;i<12;i++){
cout<<"Month index = "<<i+1<<" :
"<<endl;
cout<<"Rainfall :";
cin>>arr[i].rainfall;
total_rainfall = total_rainfall + arr[i].rainfall;
cout<<"Lowest temperature :";
cin>>arr[i].low_temp;
if(arr[i].low_temp < lowest_temp){
lowest_temp = arr[i].low_temp;
low_ind = i;
}
cout<<"Highesh Temperature :";
cin>>arr[i].high_temp;
if(arr[i].high_temp > highest_temp){
highest_temp = arr[i].high_temp;
high_ind = i;
}
arr[i].avg_temp = (double) (arr[i].low_temp + arr[i].high_temp)/2.0
;
total_temp = total_temp + arr[i].avg_temp;
}
avg_mon_temp = total_temp / 12.0;
avg_rainfall = total_rainfall / 12.0;
cout<<"Total rainfall
:"<<total_rainfall<<endl;
cout<<"Average monthly rainfall
:"<<avg_rainfall<<endl;
cout<<"Average monthly temperature
:"<<avg_mon_temp<<endl;
cout<<"lowest temperature
:"<<lowest_temp<<endl;
cout<<" Month:";
switch(low_ind){
case 0:
cout<<"January"<<endl;
break;
case 1:
cout<<"February"<<endl;
break;
case 2:
cout<<"March"<<endl;
break;
case 3:
cout<<"April"<<endl;
break;
case 4:
cout<<"May"<<endl;
break;
case 5:
cout<<"June"<<endl;
break;
case 6:
cout<<"July"<<endl;
break;
case 7:
cout<<"August"<<endl;
break;
case 8:
cout<<"September"<<endl;
break;
case 9:
cout<<"October"<<endl;
break;
case 10:
cout<<"November"<<endl;
break;
case 11:
cout<<"December"<<endl;
break;
}
cout<<"highest temperature
:"<<highest_temp<<endl;
cout<<" Month:";
switch(low_ind){
case 0:
cout<<"January"<<endl;
break;
case 1:
cout<<"February"<<endl;
break;
case 2:
cout<<"March"<<endl;
break;
case 3:
cout<<"April"<<endl;
break;
case 4:
cout<<"May"<<endl;
break;
case 5:
cout<<"June"<<endl;
break;
case 6:
cout<<"July"<<endl;
break;
case 7:
cout<<"August"<<endl;
break;
case 8:
cout<<"September"<<endl;
break;
case 9:
cout<<"October"<<endl;
break;
case 10:
cout<<"November"<<endl;
; break;
case 11:
cout<<"December"<<endl;
break;
}
}