C++ Write a program that uses a structure to store the following information for
ID: 3794253 • Letter: C
Question
C++
Write a program that uses a structure to store the following information for a particular month at the local airport :
· Total number of planes that landed
· Total number of planes that departed
· Greatest number of planes that landed in a given day that month
· Least number of planes that landed in a given day that month
The program should have an array of twelve structures to hold travel information for the entire year. The program should prompt the user to enter data for each month . Once all data is entered, the program should calculate and output the average monthly number of landing planes, the average monthly number of departing planes, the total number of landing and departing planes for the year, and the greatest and least number of planes that landed on any one day and which month it occurred in.
Do not accept negative numbers for input. If the user enters a negative number, display an error message and let the user enter the value again. Repeat this step as long as user does not correct the mistake.
Explanation / Answer
#include <iostream>
using namespace std;
struct monthDetails{
int numLanded; //total number of planes landed in a month
int numDeparted; //total number of planes departed in a month
int numGrtDay; //greatest number of planes landed on a day
int numLeastDay; //least number of planes landed on a day
};
int main(){
monthDetails monthsDetails[12];
for(int i=0; i<12; i++){
monthDetails currentMonth;
int x,y,z,w;
while(1){
cout<<"Enter total number of planes landed in this month: ";
cin>>x;
if(x>0){
break;
}
else{
cout<<"Negative Value is not allowed, please re-enter value"<<endl;
}
}
while(1){
cout<<"Enter total number of planes departed in this month: ";
cin>>y;
if(y>0){
break;
}
else{
cout<<"Negative Value is not allowed, please re-enter value"<<endl;
}
}
while(1){
cout<<"Enter greatest number of planes landed on a day: ";
cin>>z;
if(z>0){
break;
}
else{
cout<<"Negative Value is not allowed, please re-enter value"<<endl;
}
}
while(1){
cout<<"Enter least number of planes landed on a day: ";
cin>>w;
if(w>0){
break;
}
else{
cout<<"Negative Value is not allowed, please re-enter value"<<endl;
}
}
currentMonth.numLanded=x;
currentMonth.numDeparted=y;
currentMonth.numGrtDay=z;
currentMonth.numLeastDay=w;
monthsDetails[i]=currentMonth;
}
int a=0; //total number of planes landed in a year
int b=0; //total number of planes departed in a year
int c=0; //greatest number of planes that landed on any one day
int d=0; //least number of planes that landed on any one day
int m; //the month number in which greatest number of planes landed
int n; //the month number in which least number of planes landed
for(int i=0; i<12; i++){
a+=monthsDetails[i].numLanded;
b+=monthsDetails[i].numDeparted;
if(c<monthsDetails[i].numGrtDay){
c=monthsDetails[i].numGrtDay;
}
if(d>monthsDetails[i].numLeastDay){
d=monthsDetails[i].numLeastDay;
}
}
string month[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
cout<<"The average monthly number of landing planes "<<a/12<<endl;
cout<<"The average monthly number of departing planes "<<b/12<<endl;
cout<<"The total number of landing for the year "<<a<<endl;
cout<<"the total number of departing planes for the year "<<b<<endl;
cout<<"The greatest number of planes that landed on any one day "<<c<<"in month "<<month[m]<<endl;
cout<<"the greatest and least number of planes that landed on any one day "<<d<<"in month "<<month[n]<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.