C++ Design and implement a program that assigns seats on an airplane. Assume the
ID: 3866359 • Letter: C
Question
C++
Design and implement a program that assigns seats on an airplane. Assume the airplane has 20 seats in economy and has 20 seats in the first class (5 rows of 4 seats each, separated by an isle). Your program should take three commands: add passengers, show seating, and quit. When passengers are added, ask for the class (first or the economy), the number of passengers traveling together (1 to 2 in the first class, 1 to 3 in the economy), and the seating preference (aisle or a window in the first class; aisle, center or window in economy). Then try to find a match and assign the seats. If no match exits, print a message. Follow the object printed design process. Show the step by step process.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
/* functions */
int mymenu();
int gettingclass();
int passengerstraveling(int);
int seatselection(int);
void firstclass(char[][4]);
void economyclass(char[][6]);
void printresult(char[][4],char[][6]);
/* main method */
int main()
{
/* variable declarations */
int selection,i1,j1;
char myfirst[5][4],myeconomy[30][6];
for(i1=0;i1<5;i1++)
for(j1=0;j1<4;j1++)
myfirst[i1][j1]='-';
for(i1=0;i1<30;i1++)
for(j1=0;j1<6;j1++)
myeconomy[i1][j1]='-';
selection=mymenu();
while(selection!=3)
{
if(selection==1)
{
selection=gettingclass();
if(selection==1)
firstclass(myfirst);
else
economyclass(myeconomy);
}
else if(selection==2)
{
printresult(myfirst,myeconomy);
}
else if(selection!=3)
cout<<"Entry is Invalid!"<<endl;
selection=mymenu();
}
}
/* module for getting class */
int gettingclass()
{
int selection;
cout<<"1 - first class"<<endl<<"2 - economy"<<endl<<">>"<<endl;
cin>>selection;
while(selection<1||selection>2)
{
cout<<"Entry is Invalid!"<<endl;
cout<<"1 - first class"<<endl<<"2 - economy"<<endl<<">>"<<endl;
cin>>selection;
}
return selection;
}
/* module for passengers accompanying */
int passengerstraveling(int n)
{
int selection;
cout<<"Enter number of People accompanying with you(maximum "<<n<<"): ";
cin>>selection;
while(selection<1||selection>n)
{
cout<<"Entry is Invalid!"<<endl;
cout<<"Enter number of People accompanying with you(maximum "<<n<<"): ";
cin>>selection;
}
return selection;
}
/* module for menu */
int mymenu()
{
int selection;
cout<<"Enter selection"<<endl;
cout<<"1 - add passengers"<<endl;
cout<<"2 - show seat selection"<<endl;
cout<<"3 - quit"<<endl;
cout<<">> ";
cin>>selection;
return selection;
}
/* module for first class */
void firstclass(char f[][4])
{
int selection,number,i1,j1,k1,s1;
int aisleseat[2][2]={1,2,0,4};
bool isfound;
number=passengerstraveling(2);
for(i1=1;i1<=number;i1++)
{
cout<<"passenger "<<i1<<endl<<"enter seat selection : ";
s1=seatselection(0);
isfound=false;
j1=-1;
while(!isfound&&j1<4)
{
j1++;
for(k1=0;k1<2&&!isfound;k1++)
if(f[j1][aisleseat[s1][k1]]=='-')
{
f[j1][aisleseat[s1][k1]]='X';
isfound=true;
}
}
if(isfound)
cout<<"seat is found in row "<<j1+1<<endl;
else
cout<<"seat not is found"<<endl;
}
}
/* module for economy class */
void economyclass(char e[][6])
{
int selection,number,i1,j1,k1,s1;
bool isfound;
int aisleseat[3][2]={2,3,1,4,0,5};
number=passengerstraveling(3);
for(i1=1;i1<=number;i1++)
{
cout<<"passenger "<<i1<<endl<<"enter seat selection: ";
s1=seatselection(1);
isfound=false;
j1=-1;
while(!isfound&&j1<29)
{
j1++;
for(k1=0;k1<2&&!isfound;k1++)
{
if(e[j1][aisleseat[s1][k1]]=='-')
{
e[j1][aisleseat[s1][k1]]='X';
isfound=true;
}
}
}
if(isfound)
cout<<"seat found in row "<<j1+6<<endl;
else
cout<<"seat is not found ";
}
}
/* module for seat selection */
int seatselection(int n)
{
int selection=0;
string message[3]={"seataisle","seatwindow","seatcenter"};
int myorder[2][3]={0,1,0,0,2,1};
int maxi[2]={2,3};
int i1,j1;
while(selection<1||selection>maxi[n])
{
cout<<"enter seat selection"<<endl;
for(i1=0;i1<maxi[n];i1++)
cout<<i1+1<<" - "<<message[myorder[n][i1]]<<endl;
cout<<">> ";
cin>>selection;
if(selection<1||selection>maxi[n])
cout<<"invalid entry"<<endl;
}
return selection-1;
}
/* module for printing result */
void printresult(char f[][4],char e[][6])
{
int i1,j1;
cout <<"Seating"<<endl<<"X=assigned seat"<<endl<<"-=unassigned seat"<<endl<<endl;
for(i1=0;i1<5;i1++)
{
cout<<" ";
for(j1=0;j1<4;j1++)
{
cout<<f[i1][j1]<<" ";
if(j1==1)
cout<<" ";
}
cout<<endl;
}
for(i1=0;i1<30;i1++)
{
for(j1=0;j1<6;j1++)
{
cout<<e[i1][j1]<<" ";
if(j1==2)
cout<<" ";
}
cout<<endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.