Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C PROGRAMING LANGUAGUE problem:You are setting up a travel agency at a touris

ID: 3861518 • Letter: I

Question

IN C PROGRAMING LANGUAGUE

problem:You are setting up a travel agency at a tourist resort. Your agency organizes weekly bus tours. There is one tour on each day of the weekend and each tour has exactly 5 seats. Write a program for your agency to implement the weekly reservation of seats.

The program should repeatedly execute one of the following commands:

Book: This operation should ask the user for a day (Saturday or Sunday) and check if there is a seat in the user input day. If there is one, the program should remember that one more seat has been reserved for that day. Otherwise, the program should print that there is “No More Seats Left”.

Cancel: This operation should ask the user for a day (Saturday or Sunday) and cancel one reservation for the given day and remember that there is one more seat free. If no seats were reserved that day, you program should print "No reservation made on this day.

Switch: This operation switches one reservation from one day, D1 (Saturday or Sunday) to another day, D2 (Saturday or Sunday). In this case the number of reserved seats in D1 is decremented by 1 and the number of reserved seats in D2 is incremented by one. Note that if no seats were reserved in D1 or if there is no more available seat on D2, your program should print "No sear available to switch".

Report: Print the number of available seats in each day.

Quit: Exit the system.

Hint: You can code the days of the weekend as follows:

- #define Saturday 1

- #define Sunday 2

So the user will enter 1 for Saturday and 2 for Sunday.

Explanation / Answer

#include <stdio.h>

void main()
{
int choice=1; //Controls the flow in the loop i.e. which function to call
int day; //Stores the day for which the function is to be carried out
int seats[2] = {0,0}; //Array in which index 0 represents Saturday and 1 represents Sunday.
//This array stores the seats BOOKED on the day

while (choice != 5) //choice = 5 denotes QUIT so run the program until quit.
{
printf (" MENU"); //Displays the menu containing the different functions
printf (" 1. Book 2. Cancel 3. Switch 4. Report 5. Quit ");
printf (" Please enter your choice: ");
scanf ("%d", &choice); //Taking input for the choice representing which function to call

if(choice == 1)
{
printf (" Please enter the day for which you want to book a seat (1- Sat, 2-Sun): ");
scanf ("%d", &day);
day--; //day is decremented to match the index of the array as
//earlier 1 is Saturday and in the array 0 is Saturday (arrays are 0 indexed)
if(seats[day]==5) //All seats for the day are already booked
{
printf (" No More Seats Left. ");
continue;
}
else
{
seats[day]++; //Incrementing seats[day] to book one more seat
printf (" Seat booked successfully. ");
}
}
else if(choice == 2)
{
printf (" Please enter the day for which you want to cancel the seat (1- Sat, 2-Sun): ");
scanf ("%d", &day);
day--;

if(seats[day]== 0) //No seat is booked for the day i.e. empty bus
{
printf (" No reservation made on this day. ");
continue;
}
else
{
seats[day]--; //seats[day] is decremented to cancel one seat
printf (" Seat cancelled successfully. ");
}
}
else if(choice == 3)
{
printf (" Please enter the day from which you want to switch the seat (1- Sat, 2-Sun): ");
scanf ("%d", &day);
day--;

if( seats[day] == 0 || seats[1-day] == 5) //Checking for the given condition i.e. seat is not
{ //available for cancelling
printf (" No seat available to switch. ");
continue;
}
else
{
seats[day]--; //day denotes the day of which the seat is cancelled
seats[1-day]++; //1-day denotes the other day to which the seat is to be changed
printf (" Seat switched successfully. ");
}
}
else if (choice == 4)
{
printf(" The seats available on Saturday are: %d ", 5-seats[0]); //If seats[i] denotes the seats booked,
printf(" The seats available on Sunday are: %d ", 5-seats[1]); //5-seats[i] denotes the seats available
}
else if (choice == 5)
{
printf(" Thank you for visiting."); //Prints a prompt while exiting the loop
break;
}
else
{
printf(" Invalid choice entered! "); //Prompt in case an invalid choice is entered
}
}
}

This should solve the problem using an array seats[2] denoting the seats booked for the 2 days.

The problem can be solved without the use of arrays too, with 2 variables seats_saturday=0 (denoting the seats booked for Saturday) and seats_sunday=0 (denoting the seats booked for Sunday) and replacing seats[0] with seats_saturday and seats[1] with seats_sunday.