A bus has 28 seats, arranged in 7 rows and 4 columns:
This seating arrangement is mapped, row-wise, to a 1D-array of size 28:
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
Implement a well-structured C program to enable a user to make and cancel seat reservations for the bus. The program uses a text-file seats.txt to store the reservation information: 0 indicates that a seat is not reserved, and 1 indicates that a seat is reserved. Originally all seats are not reserved:
|
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
Your program starts by initializing a 1D-array with the current contents of the seats.txt file. It must then display the following menu:
1. Display number of available seats.
2. Display seat status for all seats.
3. Reserve seat(s).
4. Delete reservation(s).
5. Exit.
Please select your choice:
Your program must loop as long as option 5 has not been selected. It must display an appropriate error message if an invalid choice is entered. After executing each of the options 1 to 4, your program must pause and display the message: “Press Enter key to continue . . .”. Your program must display the above menu after pressing the Enter keyThe options must have the following behaviors:
Option 1: Display number of available seats
It displays the current number of available seats. It then waits for the Enter key to be pressed before returning control to the main menu.
If four seats are reserved, for example, hen the output is:
Option 2: Display seat status for all seats
It displays the status of each seat. The option then waits for the Enter key to be pressed before returning control to the main menu.
Option 3: Reserve seat(s)
It displays the status of each seat.
It displays the number of available seats.
If there are no available seats, the message: “Sorry, there are no available seats.” is displayed:
If there is one or more available seats, it prompts the user for and reads the number of required reservations. If the number of required reservations is more than the number of available seats, the message: “Error: Insufficient available seats.” is displayed:
If the number of required reservations is zero or negative, the error message: “Error: Invalid number of seats.” Is displayed:
If the required number of reservations is valid and sufficient, the program prompts for and reads the seat number
and checks that the seat number is valid and does the reservation. If the seat number is not valid or already reserved, it displays a message and asks again for the seat until all seats are reserved.
If the reservation is successful, the message: “Required seats successfully reserved” is displayed:
Options 4 . Delete reservation(s)
It displays the available seats if any.
It prompts for and reads the number of seats whose reservations are to be deleted.
If the number of seats whose reservations are to be deleted is not valid, the error message: “Error: Invalid number of reservations to delete.” Is displayed:
If the number of seats whose reservations are to be deleted is valid, the program prompts for and reads the seat number of each seat whose reservation is to be deleted, the program prompts for the seat numbers and does the necessary validation as in reservation part.
If the deletion is successful, the message: “Required reservation(s) successfully deleted” is displayed:
Whether the update operation was done or not, control is returned to the main menu after pressing the Enter key.
Options 5. Exit
It terminates the program and writes the updated array into the seats.txt file.
Please refer to screenshots
Code Screenshots:
![#include <stdio.h> int arr[28]; // array to store seat status 5 6 // function to show menu void show menu() { printf(1. Disp](http://img.homeworklib.com/questions/be3c4370-6d01-11eb-9ce4-713e1e83c450.png?x-oss-process=image/resize,w_560)




Output

Code to copy
#include <stdio.h>
int arr[28];
void show_menu(){
printf("1. Display number of available
seats.\n");
printf("2. Display seat status of all
seats.\n");
printf("3. Reserve Seat(s).\n");
printf("4. Delete Reservation(s).\n");
printf("5. Exit.\n");
}
int display_available(){
int cnt = 0;
int i;
for(i = 0; i < 28; i++){
cnt += (arr[i] == 0);
}
}
void seat_status(){
int i;
for(i = 0; i < 28; i++){
printf("Seat %d: %s\n", i, ((arr[i]
== 0)?"Available":"Reserved"));
}
}
void write_status(){
int i;
FILE* file = fopen("seats.txt", "w");
for(i = 0; i < 28; i++){
fprintf(file, "%d\n",
arr[i]);
}
}
void reserve_seats(){
int cnt = display_available();
printf("Available Seats %d\n", cnt);
seat_status();
if(cnt == 0){
printf("Sorry, there is no
available seats\n");
return;
}
printf("Enter number of seats required\n");
int req;
sacnf("%d", &req);
if(req > cnt){
printf("Error: Insufficient
available seats.\n");
return;
}
if(req <= 0){
printf("Error: Invalid number of
seats\n");
return;
}
int i;
for(i = 0; i < req; i++){
int seat;
while(1){
printf("Enter
seat number\n");
scanf("%d",
&seat);
if(seat > 0
&& arr[seat] == 0){
arr[seat] = 1;
break;
}
printf("Seat %d
not available\n", seat);
}
}
printf("Required seats successfully
reserved\n");
}
void delete_reservation(){
int cnt = display_available();
printf("Enter number of reservation deleted\n");
int req;
scanf("%d", &req);
if(req > 28 - cnt || req <= 0){
printf("Error: Invalid number of
reservation to delete\n");
return;
}
int i;
for(i = 0; i < req; i++){
int seat;
while(1){
printf("Enter
seat number to delete\n");
scanf("%d",
%seat);
if(seat > 0
&& arr[seat] == 1){
arr[seat] = 0;
}
printf("Invalid
seat number\n");
}
}
printf("Required reservation(s) successfully is
deleted\n");
}
int main(){
int i;
FILE* file = fopen("seats.txt", "r");
for(i = 0; i < 28; i++){
fscanf(file, "%d",
&arr[i]);
}
while(1){
show_menu();
int ch;
scanf("%d", &ch);
if(ch == 1){
int cnt =
display_available();
printf("Available Seats are %d\n", cnt);
} else if(ch == 2){
seat_status();
} else if(ch == 3){
reserve_seats();
} else if(ch == 4){
delete_reservation();
} else if(ch == 5){
write_status();
break;
} else {
printf("Invalid
Choice Entered\n");
continue;
}
}
return 0;
}
A bus has 28 seats, arranged in 7 rows and 4 columns: This seating arrangement is...
Program Description: A bus has 28 seats, arranged in 7 rows and 4 columns: This seating arrangement is mapped, row-wise, to a 1D-array of size 28: 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 0 1 2 3 21 22 23 24 25 Implement a well-structured Java program to enable a user to make and cancel seat reservations for the bus. The program uses Array to store the reservation information: 0...
C++ Help please- kind of long. The aim is to implement a seat reservation system for a passenger airplane. We assume a small airplane with 10 rows and 4 seats per row. We assume that the seat chart is initially stored in a file “chartIn.txt” in the following format: 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 ...
Case Problem 11 - 1: Modify the GreenvilleRevenue program created in the previous chapter so that it performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. Use exception-handling techniques to ensure a valid value and display the error message: Number must be between 0 and 30 The program prompts the user for talent codes. Use exception-handling techniques to ensure a valid code and...
P7.5– A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 10 10 10 10 20 20 20 20 20 20 10 10 10 10 20 20 20 20 20 20 10 10 20 20 30 30...
The Course Project can be started in Week 7 and is due by 11:59
pm CT Saturday of Week 8. It must follow standard code formatting
and have a comment block at the top of the code file with a
detailed description of what the program does. Functions must have
a comment block with a detailed description of what it does.
Pseudocode must be provided in the comment block at the top of the
file. This program will allow the...
don't use continuity and break in python 3: (In python3) Exercise # 4: Write a program
that prompts for and reads the number ? of spheres to be processed.
If ?≤0 your program must display an error message and terminate;
otherwise it does the following for ? times: Prompts for and reads
the volume of a sphere, it then displays the surface area of the
sphere with that volume. Assume that each volume is in cubic
centimeters. The program finally...
Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
Write a program in C to assign seats of a movie theater (capacity: 200 seats). Your program should display the following menu of alternatives: Please type 1 for "section A, $50/ticket" type 2 for "section B, $70/ticket", and type 3 for "section C, $80/ticket". If the user types 1, then your program should assign a seat in the A section (seats 1–50). If the user types 2, then your program should assign a seat in the B section (seats 51–100). If...
This program is in C++ which reserves flight seats. It uses a file imported to get the seat chart called "chartIn.txt" which looks like this 1 A B C D E F 2 A B C D E F It continues until row 10. Sorry unable to provide the chartIn.txt file. I am having issues with function displaySeats, it displays then but when it comes to 10 the row looks like this 1 0 A B C D E ,...
Menu-driven programs will display the menu options to the user and then prompt them for a menu choice. This program will display the following menu in the following format: Calculator Options: Calculate the area of a circle Calculate the area of a rectangle Calculate the area of a triangle Calculate the area of a trapezoid Calculate the area of a sphere Exit Enter your choice (1-6) Once the user enters a choice for the menu, the program should use a...