105k views
4 votes
Write a program that can be used to assign seats for a commercial airline. The airplane has 13 rows, with 6 seats in each row.

Create a menu-driven program so that the user (in a loop) can choose to display a map of the seats, or choose a seat.
Menu option 1: Display a "map" of all of the seats on the airplane (display a star (*) to indicate the seat is available; display an X if the seat is occupied).
Menu option 2: Ask the user and to select the specific seat he wishes to reserve. Either reserve the seat, or tell the user that the request could not be completed.
You must create this program by writing the following functions: displayMap will display the current seating map of the entire airplane makeReservation will let the user select the specific seat(s) they wish to reserve, call a function validateFunction, and reserve the seat it is available. validateFunction will take the user’s selection as a parameter and will return true if the user can reserve the seat and will return false if the seat is already taken.
A B C D E F
Row 1 * * X * X X
Row 2 * X * X * X
Row 3 X * X X * X
(etc.)

1 Answer

4 votes

Answer: Provided in the explanation segment

Step-by-step explanation:

The code below is provided to run this problem and solve this problem.

Code:

#include<stdio.h> //for printf and scanf

#include<stdbool.h> //for boolean datatype

char seats[13][6]; //2d char array represents seats of the airplane

//menu function prints the menu

void menu(){

printf("1) Display seats\\2) Reserve a seat\\Enter option: ");

}

//displayMap function prints the seat occupancies

void displayMap(){

int i,j;

printf("\\* - available\\X - occupied\\");

printf(" A B C D E F\\");

for(i=0;i<13;i++){

printf("%2d ",i+1);

for(j=0;j<6;j++){

printf("%c ",seats[i][j]);

}

printf("\\");

}

}

//validate returns true if seat is available, else returns false

bool validate(char c, int r)r<1

//prompts user to enter seat no. then validates and reserves the seat

void makeReservation(){

char s[3],col;

int row = 0, i=1;

printf("Enter seat number (eg: A1 or C12): ");

scanf("%s",s);

col = s[0];

while(i<3&&s[i]!='\0'){

row = row*10 + (int)(s[i]-'0');

i += 1;

}

if(validate(col,row)){

seats[row-1][col-'A'] = 'X';

printf("%c%d is reserved for You. Thank you.",col,row);

}

else printf("%c%d reservation failed. Please check seat no. & try again!!",col,row);

}

//main function

int main()

{

int i,j,option;

//loops through the char array and assigns * to represent the seats are available

for(i=0;i<13;i++){

for(j=0;j<6;j++){

seats[i][j] = '*';

}

}

do{ //loop continues until user enters neither 1 nor 2

menu(); //calls menu function

scanf("%d",&option);

if(option==1){ //if option is 1 then displayMap function is called

displayMap();

}

else if(option==2){ //if option entered is 2 then makeReservation function gets called

makeReservation();

}

printf("\\\\");

}while(option==1||option==2);

printf("Thank you for using our service\\");

return 0;

}

Cheers i hope this helped !!

User SMilbz
by
4.0k points