In: Computer Science
Solve the question using 2 dimensional pointers. You must use new and delete operator in the program and try to make an easier logic of the problem.
Q2. Write a C++ program using a dynamic array
(or arrays) to assign passengers seats in a Bus and your program
will ask the user how many rows the Bus has and will handle that
many rows (Assume the Bus does not always have the same rows). (5
points)
Expected output: Assume a small Bus with seat numbering as
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
8 A B C D
9 A B C D
10 A B C D
The program should display the seat pattern, with an X marking the
seats
already assigned. For example, after seats 1A, 2B, and 4C are
taken, the
display should look like this: (5 points)
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
8 A B C D
9 A B C D
10 A B C D
After displaying the seats available, the program prompts for the
seat
desired, the user types in a seat, and then the display of
available seats is
updated. This continues until all seats are filled or until the
user signals
that the program should end. If the user types in a seat that is
already
assigned, the program should say that the seat is occupied and ask
for
another choice. (10 points)
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
main.cpp
#include <iostream>
#include<iomanip>
using namespace std;
//check if empty seat in flight
int checkIsEmpty(int desiredRow,int seatNumber,char **
busAvalSeats){
if(busAvalSeats[desiredRow][seatNumber]=='*'){
busAvalSeats[desiredRow][seatNumber]='X';
return true;
}
return false;
}
//print menu and flight details
void printMenu(char ** flight,int seatRows){
cout<<"------------------------------------------------------------------------"<<endl;
cout<<setw(40)<<"MY BUS MANAGEMENT SYSTEM
"<<endl;
cout<<"------------------------------------------------------------------------"<<endl;
cout<<setw(11)<<" ";
for(int i=0;i<4;i++){
cout<<setw(5)<<char('A'+i);
}
cout<<endl;
for(int i=0;i<seatRows;i++){
cout<<"Row "<<setw(7)<<i+1;
for(int j=0;j<4;j++){
cout<<setw(5)<<flight[i][j];
}
cout<<endl;
}
}
int main () {
int seatRows;
cout<<"Enter number of seat in bus : ";
cin>>seatRows;
char ** busAvalSeats;
busAvalSeats = new char*[seatRows];
for(int i=0;i<seatRows;i++)
busAvalSeats[i] = new char[4];
for(int i=0;i<seatRows;i++)
for(int j=0;j<4;j++)
busAvalSeats[i][j]='*';
//print flight
printMenu(busAvalSeats,seatRows);
char choice='y';
char seat;
int seatNumber;
int row;
while(choice=='y'||choice=='Y'){
while(true){
cout<<"Enter row : ";
cin>>row;
cout<<"Enter desire seat (A-D) : "<<endl;
cin>>seat;
while(seat=='\n'){
cin>>seat;
}
row--;
if(seat>='A' && seat<='D' && row>=0
&& row<seatRows){
break;
}else{
cout<<"Please enter valid option"<<endl;
}
}
seatNumber = seat - 'A';
if(checkIsEmpty(row,seatNumber,busAvalSeats)){
cout<<"Congratulation!!! Your booking is
successful"<<endl;
}else{
cout<<"!!!Sorry . The seat is already
booked"<<endl;
}
printMenu(busAvalSeats,seatRows);
cout<<"\nContinue y/n : ";
do{
cin>>choice;
}while(choice=='\n');
}
delete []busAvalSeats;
}
output