Write a c program that can be used to assign seats for a com
Write a c++ program that can be used to assign seats for a commercial airplane. The airplane has a user-defined number of rows and columns. Your program must create a menu with the following options:
1) Book a seat
2) Cancel a Seat
3) Display seat map (Use ‘A’ and ‘T’)
4) Count available seats
You must use a separate function to perform each of the above tasks. Use dynamic 2D arrays
Solution
PROGRAM CODE:
#include <iostream>
 #include <string>
using namespace std;
// Im using 1 to assign a seat and 0 to empty it.
 // The number of rows is 30 and the number of columns is 6. i.e from A to F
 int seats[30][6];
void book()
 {
 int row;
 char column;
 cout<<\"\ Enter the Row for your seat(1-30): \";
 cin>>row;
 cout<<\"\ Enter the column for your seat (A-F): \";
 cin>>column;
// converting colum into lowercase so that checking becomes easier
 column = tolower(column);
if(row>30 || row < 0 || column < \'a\' || column > \'f\')
 {
 cout<<\"\ Invalid selection. Please try again !\ \";
 book();
 }
 else
 {
 // converting the char to respective ASCII number
 int columnNumber = tolower(column);
 // ASCII number for a is 97. So subtracting 96 to get the value of column number
 columnNumber -= 96;
 //checking if the column is not already selected
 if(seats[row][columnNumber] != 1)
 {
 cout<<\"\ Your seat is booked.\ \";
 seats[row][columnNumber] = 1;
 }
 else
 {
 cout<<\"\ Your selection is already booked.\ \";
 }
 }
 }
void cancel()
 {
 int row;
 char column;
 char f=\'f\';
 char F=\'F\';
 cout<<\"\ Enter the Row for your seat (1-30): \";
 cin>>row;
 cout<<\"\ Enter the column for your seat (A-F): \";
 cin>>column;
 // converting colum into lowercase so that checking becomes easier
 column = tolower(column);
 if(row>30 || row < 0 || column < \'a\' || column > \'f\')
 {
 cout<<\"\ Invalid selection. Please try again !\ \";
 book();
 }
 else
 {
 // converting the char to respective ASCII number
 int columnNumber = tolower(column);
 // ASCII number for a is 97. So subtracting 96 to get the value of column number
 columnNumber -= 96;
 //checking if the column is already selected
 if(seats[row][columnNumber] == 1)
 {
 cout<<\"\ Your seat is cancelled.\ \";
 seats[row][columnNumber] = 0;
 }
 else
 {
 cout<<\"\ This seat was already unbooked\ \";
 }
 }
 }
void display()
 {
 cout<<\"\  A represents an available seat and T represents booked seats.\ \";
 //looping through the 2D array to print the A and T seats
 for(int i=0; i<30; i++)
 {
 for(int j=0; j<6; j++)
 {
 if(seats[i][j] == 1)
 cout<<\"T\\t\";
 else
 cout<<\"A\\t\";
 }
 cout<<\"\ \";
 }
 }
void countAvailableSeats()
 {
 int counter = 0;
 // looping through the 2D array to count all the selected seats
 for(int i=0; i<30; i++)
 {
 for(int j=0; j<6; j++)
 {
 if(seats[i][j] == 1)
 counter++;
 }
 }
 cout<<\"\ The total available seats are \"<<counter<<\"\ \";
 }
void menu()
 {
 int choice;
 cout<<\"1. Book a seat\ 2. Cancel a seat\ 3. Display seat map\ 4. Count Available seats\ 5. quit\ \";
 cout<<\"Enter your choice: \";
 cin>>choice;
 switch(choice)
 {
 case 1: book();
 menu();
 break;
 case 2: cancel();
 menu();
 break;
 case 3: display();
 menu();
 break;
 case 4: countAvailableSeats();
 menu();
 break;
 case 5: break;
 }
 }
 int main() {
 menu();
 return 0;
 }
OUTPUT:
1. Book a seat
 2. Cancel a seat
 3. Display seat map
 4. Count Available seats
 5. quit
 Enter your choice: 1
 Enter the Row for your seat(1-30): 23
 Enter the column for your seat (A-F): A
 Your seat is booked.
 1. Book a seat
 2. Cancel a seat
 3. Display seat map
 4. Count Available seats
 5. quit
 Enter your choice: 1
 Enter the Row for your seat(1-30): 23
 Enter the column for your seat (A-F): A
 Your selection is already booked.
 1. Book a seat
 2. Cancel a seat
 3. Display seat map
 4. Count Available seats
 5. quit
 Enter your choice: 1
 Enter the Row for your seat(1-30): 22
 Enter the column for your seat (A-F): D
 Your seat is booked.
 1. Book a seat
 2. Cancel a seat
 3. Display seat map
 4. Count Available seats
 5. quit
 Enter your choice: 2
 Enter the Row for your seat (1-30): 22
 Enter the column for your seat (A-F): A
 This seat was already unbooked
 1. Book a seat
 2. Cancel a seat
 3. Display seat map
 4. Count Available seats
 5. quit
 Enter your choice: 4
 The total available seats are 2
 1. Book a seat
 2. Cancel a seat
 3. Display seat map
 4. Count Available seats
 5. quit
 Enter your choice: 3
 A represents an available seat and T represents booked seats.
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A T A
 A T A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 A A A A A A
 1. Book a seat
 2. Cancel a seat
 3. Display seat map
 4. Count Available seats
 5. quit
 Enter your choice: 5




