This is an exercise for 2-Darray application and the ‘continue’ statement usage
ID: 3609405 • Letter: T
Question
This is an exercise for 2-Darray application and the ‘continue’ statement usage inloop. Before you get onboard an airport, you’ll have to get aboarding card in the airport. There is a machine with touch screen,like an ATM in a bank branch. In that touch screen it will displayall available and taken seats. You job is to write such a programto assign passengers seats for a small airplane with seat numberingas follows:1 A B C D
2 A B C D
3 A B C D
4 A B C D
5 A B C D
6 A B C D
7 A B C D
Your program should display the seat pattern, with ‘X’marking the seat already taken. For example, after seats 1A, 2B,and 4C are taken, the display should look like this:
1 X B C D
2 A X C D
3 A B C D
4 A B X D
5 A B C D
6 A B C D
7 A B C D
After displaying the seats available, the program prompts for theseat desired, the user types in a seat, and then the display ofavailable seats is updated. This continues until all seats arefilled or until the user signals that the program should end. Ifthe user types in a seat that is already assigned, the programshould say that seat is occupied and ask for another choice.
Explanation / Answer
using namespacestd;
void print_array(chararray[7][4]);
int checkseats (char array[7][4]);
int main()
{
int x,y,aisle,status,check;
char seat;
char array[7][4]; // Declares an array for the planeseating
// Initialize seats withletter designations
for ( x = 0; x <7; x++ )
{
for ( y = 0; y < 4; y++ )
{
if (y==0)
array[x][y] = 'A';
if (y==1)
array[x][y] = 'B';
if (y==2)
array[x][y] = 'C';
if (y==3)
array[x][y] = 'D';
}
}
print_array(array);
while(status!=1)
{
check=checkseats(array);
// Check if theplane is full and print a message if true and exit.
if(check==0)
{
cout<<endl;
cout<<"The plane isfull. Sorry."<<endl;
cin.get();
cin.get();
break;
}
// Enter an aisle and seat.
cout<<endl;
cout<<"Enter the desired aisle number or 0 toexit: ";
cin >>aisle;
if (aisle==0)
break;
cout<<"Enter the desired seat (A,B,C, or D): ";
cin >>seat;
if (seat=='A'|| seat=='a')
y=0;
if (seat=='B' || seat=='b')
y=1;
if (seat=='C' || seat=='c')
y=2;
if (seat=='D' || seat=='d')
y=3;
if(array[aisle][y]=='X')
cout<<"This seat is taken! Tryagain!"<<endl;
else
{
array[aisle-1][y]='X';
}
print_array(array);
}
return 0;
}
//**********************Functions*******************************
// Print seating chart
void print_array(chararray[7][4])
{
for ( int i = 0; i < 7; i++ )
cout<<"Aisle"<<i+1<<" "<<array[i][0]<<""<<array[i][1]<<" "<<array[i][2]<<""<<array[i][3]<<endl;
return;
}
// Check for a fullplane.
int checkseats(chararray[7][4])
{
int p=0;
for ( int x = 0; x < 7; x++ )
{
for ( int y = 0; y < 4; y++ )
{
if(array[x][y]!='X')
p=1;
}
}
return (p);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.