Question

I have to write a C program to assign seats on each flight of the airline’s...

I have to write a C program to assign seats on each flight of the airline’s only plane (capacity: 40 seats, in 10 rows). For the sake of simplicity, assume that each row has 4 seats labeled A, B, C, D. Your program should display the following menu of alternatives: Please type 1 for "first class" Please type 2 for "business class" Please type 3 for “economy class”. If the person types 1, then your program should assign a seat in the first class section (rows 1-2). If the person types 2, then your program should assign a seat in the business class section (seats 3-4). If the person types 3, then your program should assign a seat in the economy section (seats 5–10).

Your program should print:

(1) the seating map/chart showing all 40 seats and indicating if each seat is filled, (2) prints a passenger manifest showing names of all passengers and their seat numbers in a tabular format, and (3) prints a boarding pass for a chosen passenger indicating the person's name, seat number (For example: 2B) and whether it’s in the first class or business class, or economy section of the plane. Use a double-subscripted (2D) array to represent the seating chart of the plane. Initialize all the elements of the array to 0 to indicate that all seats are empty. As each seat is assigned, set the corresponding element of the array to 1 to indicate that the seat is no longer available.Your program should, of course, never assign a seat that has already been assigned. When the first class section is full, your program should ask the person if it’s acceptable to be placed in the business class. Similarly, if the business section is full, ask the user if economy section is acceptable. Similarly, user should have the choice of upgrading to business class or first class if a seat is available. If user chooses to change his/her choice, then make the appropriate seat assignment. If no seat is available in chosen section, then print the message "Next flight leaves in 3 hours"

Here is my code below. I can assign seats, print all seats if assigned or not, find a person based on their seat. I cannot however figure out the upgrades to different classes, the next flight leaves in 3 hours message, and the boarding pass. Also function find_seat_in_section has not been completed.

#include
#include
#define NUM_ROWS 10
#define SEATS_PER_ROW 4
#define MAX_NAME_LENGTH 50
//array seats represents seats on the plane.
//we take advantage of the fact that c initializes values of global variables to be zero
//so no explicit initialization is needed
int seats[NUM_ROWS][SEATS_PER_ROW];
char passengers[NUM_ROWS][SEATS_PER_ROW][MAX_NAME_LENGTH + 1];
void print_seats(){
   printf(" A B C D\n");

   int row;
   for(row = 0; row < NUM_ROWS; row += 1){
       int seat;
       printf("Row %2d: ", row+1);
       for(seat = 0; seat < SEATS_PER_ROW; seat += 1){
           //we get to here for every seat in every row
           //printf("%d(%d,%d) ", seats[row][seat], row, seat);
           if(seats[row][seat] == 0){
               printf(". ");
           }
           else {
               printf("$ ");
           }
       }
       printf("\n");
   }
  
}

void print_manifest(){

   int row;
   for(row = 0; row < NUM_ROWS; row += 1){
       int seat;
       for(seat = 0; seat < SEATS_PER_ROW; seat += 1){
          
           if(seats[row][seat] == 1){
               printf("%-55s %d%c\n", passengers[row][seat], row+1, 'A' + seat);
           }
          
       }
   }
  
}

int assign_seat_in_section(int first_row, int last_row, int assigned[]){
   int row;
   int seat;
   for(row = first_row; row <= last_row; row += 1){
       for(seat = 0; seat < SEATS_PER_ROW; seat += 1){
           if(seats[row][seat] == 0){
               seats[row][seat] = 1;
               assigned[0] = row;
               assigned[1] = seat;
               //printf("Found an empty seat at: row%d and seat%d\n", row, seat);
               return 1;
           }
       }
   }
   return 0;
}

int find_seat_in_section(int first_row, int last_row, int assigned[]){
   int row;
   int seat;
   for(row = first_row; row <= last_row; row += 1){
       for(seat = 0; seat < SEATS_PER_ROW; seat += 1){
           if(seats[row][seat] == 0){
               assigned[0] = row;
               assigned[1] = seat;
               //printf("Found an empty seat at: row%d and seat%d\n", row, seat);
               return 1;
           }
       }
   }
   return 0;
}


int main()
{
   printf("Size of seats: %zd\n", sizeof(seats)/sizeof(int));
   printf("Size of seats per row: %zd\n", sizeof(seats [0])/sizeof(int));
   print_seats();
   while(1){
       printf("Please type 1 for first class\n"
           "Please type 2 for business class\n"
           "Please type 3 for economy class\n");
       int class, got_it, assigned[2];

       scanf("%d", &class);
       if(class ==1){
           got_it = assign_seat_in_section(0,1,assigned); //would assign seat in first class
           if(got_it == 0){
               got_it = assign_seat_in_section(2,3,assigned); //would assign seat in business class
               if(got_it == 0){
                   got_it = assign_seat_in_section(4,9,assigned); //would assign seat in economy class
               }
           }

          
       }
       else if (class == 2){
           got_it = assign_seat_in_section(2,3,assigned);
       }
       else if(class == 3){
           got_it = assign_seat_in_section(4,9,assigned);


       }
       if(got_it == 1){
           int row = assigned[0];
           int seat = assigned[1];

           printf("Assigned Seat: %d%c\n", row+1, 'A' + seat);
           printf("What is your name?");
           char name[MAX_NAME_LENGTH + 1];
           scanf(" %[A-Za-z ]", name); //scan set for letters and blanks
           printf("Name is %s\n", name);
           strcpy(passengers[row][seat], name);


       }
       print_seats();
       print_manifest();
       printf("To find out where someone is sitting, enter the seat number (e.g. 1A): ");
       char who[10];
       scanf("%s", who);
       int row = atoi(who) - 1;
       int seat = who[strlen(who) - 1] - 'A';
       printf("Row = %d, %d %s\n", row, seat, passengers[row][seat]);
      
       //find_seat_in_section(specific rows per section);
      
   }
}

/* For upgrade, need to know if seat is available before offering an upgrade, write a function like assigned seat
except it doesnt assign a seat, it returns 1 if a seat is available, and puts the row and column in the assigned array
*/

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_ROWS 10
#define SEATS_PER_ROW 4
#define MAX_NAME_LENGTH 50
//array seats represents seats on the plane.
//we take advantage of the fact that c initializes values of global variables to be zero
//so no explicit initialization is needed
int seats[NUM_ROWS][SEATS_PER_ROW];
char passengers[NUM_ROWS][SEATS_PER_ROW][MAX_NAME_LENGTH + 1];
char name[MAX_NAME_LENGTH + 1];
int class, got_it, assigned[2];
char type[3][50] = {"First class","Business class","Economy"};
void print_seats(){
printf(" A B C D\n");

int row;
for(row = 0; row < NUM_ROWS; row += 1){
int seat;
printf("Row %2d: ", row+1);
for(seat = 0; seat < SEATS_PER_ROW; seat += 1){
//we get to here for every seat in every row
//printf("%d(%d,%d) ", seats[row][seat], row, seat);
if(seats[row][seat] == 0){
printf(". ");
}
else {
printf("$ ");
}
}
printf("\n");
}
  
}

void print_manifest(){

int row;
for(row = 0; row < NUM_ROWS; row += 1){
int seat;
for(seat = 0; seat < SEATS_PER_ROW; seat += 1){
  
if(seats[row][seat] == 1){
printf("%-55s %d%c\n", passengers[row][seat], row+1, 'A' + seat);
}
  
}
}
  
}

int assign_seat_in_section(int first_row, int last_row, int assigned[]){
int row;
int seat;
for(row = first_row; row <= last_row; row += 1){
for(seat = 0; seat < SEATS_PER_ROW; seat += 1){
if(seats[row][seat] == 0){
seats[row][seat] = 1;
assigned[0] = row;
assigned[1] = seat;
//printf("Found an empty seat at: row%d and seat%d\n", row, seat);
return 1;
}
}
}
return 0;
}

int find_seat_in_section(int first_row, int last_row){
int row;
int seat;
for(row = first_row; row <= last_row; row += 1){
for(seat = 0; seat < SEATS_PER_ROW; seat += 1){
if(seats[row][seat] == 0){
//printf("Found an empty seat at: row%d and seat%d\n", row, seat);
return 1;
}
}
}
return 0;
}

int upgrade(int class){
if(class == 2){
   if(find_seat_in_section(0,1)){
printf("Would you like to upgrade to first class? ");
char s;
scanf(" %c",&s);
if(s=='y')
{
seats[assigned[0]][assigned[1]] = 0;
assign_seat_in_section(0,1,assigned);
int row = assigned[0];
int seat = assigned[1];

printf("Assigned Seat: %d%c\n", row+1, 'A' + seat);
printf("Name is %s\n", name);
strcpy(passengers[row][seat], name);
}
return 1;
   }
   }
   if(class == 3){
       if(find_seat_in_section(0,1) || find_seat_in_section(2,3)){
printf("Would you like to upgrade to first class or business class? ");
int got;
char s;
scanf(" %c",&s);
if(s=='y')
{
seats[assigned[0]][assigned[1]] = 0;
got = assign_seat_in_section(0,1,assigned);
if(!got) got = assign_seat_in_section(2,3,assigned);
int row = assigned[0];
int seat = assigned[1];

printf("Assigned Seat: %d%c\n", row+1, 'A' + seat);
printf("Name is %s\n", name);
strcpy(passengers[row][seat], name);
}
return 1;
   }
   }
   return 0;
}

void nextflight()
{
   printf("the next flight leaves in 3 hours\n");
}

int main()
{
printf("Size of seats: %zd\n", sizeof(seats)/sizeof(int));
printf("Size of seats per row: %zd\n", sizeof(seats [0])/sizeof(int));
print_seats();
while(1){
printf("Please type 1 for first class\n"
"Please type 2 for business class\n"
"Please type 3 for economy class\n");

scanf("%d", &class);
if(class ==1){
got_it = assign_seat_in_section(0,1,assigned); //would assign seat in first class
if(got_it == 0){
got_it = assign_seat_in_section(2,3,assigned); //would assign seat in business class
if(got_it == 0){
got_it = assign_seat_in_section(4,9,assigned); //would assign seat in economy class
}
}

  
}
    else if (class == 2){
   got_it = assign_seat_in_section(2,3,assigned);
}

else if(class == 3){
got_it = assign_seat_in_section(4,9,assigned);


}
if(got_it == 1){
int row = assigned[0];
int seat = assigned[1];

printf("Assigned Seat: %d%c\n", row+1, 'A' + seat);
printf("What is your name?");
scanf(" %[A-Za-z ]", name); //scan set for letters and blanks
printf("Name is %s\n", name);
strcpy(passengers[row][seat], name);


}
if(upgrade(class)) got_it = 1;
if(got_it==0)
{
    nextflight();
}
else{
    int row = assigned[0];
int seat = assigned[1];
printf("-------------BOARDING PASS-------------\n");
    printf("Name: %-50s Type: %s Seat No: %d%c\n",name,type[class-1], row+1, 'A' + seat);
}
print_seats();
print_manifest();
// printf("To find out where someone is sitting, enter the seat number (e.g. 1A): ");
// char who[10];
// scanf("%s", who);
// int row = atoi(who) - 1;
// int seat = who[strlen(who) - 1] - 'A';
// printf("Row = %d, %d %s\n", row, seat, passengers[row][seat]);
  
//find_seat_in_section(specific rows per section);
  
}
}

Add a comment
Know the answer?
Add Answer to:
I have to write a C program to assign seats on each flight of the airline’s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in C to assign seats of a movie theater (capacity: 200 seats). Your...

    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...

  • In C++. Write a program that can be used to assign seats for a commercial airplane...

    In C++. Write a program that can be used to assign seats for a commercial airplane and print out the ticket total. The airplane has 13 rows, with row 7 being the Emergency Exit row. Each row has 7 seats, there are two seats on each side of the plane and three seats in the middle with aisles on both sides. Rows 1 and 2 are considered first class with tickets for this specific  flight being $872.00 round trip. Rows 3...

  • For c++ Write a program that can be used to assign seats for a commercial airplane....

    For c++ Write a program that can be used to assign seats for a commercial airplane. The airplane has 13 rows, with six seats in each row. Rows 1 and 2 are first class, rows 3 through 7 are business class, and rows 8 rows through 13 are economy class. Your program must prompt the user to enter the following information: Ticket type (first class, business class, or economy class) Desired seat a. b. Output the seating plan in the...

  • (Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system

    (Airline Reservations System) A small airline has just purchased a computer for its new automated reservations system. The president has asked you to program the new system. You'll write a program to assign seats on each flight of the airline's only plane (capacity: 10 scats).Your program should display the following menu of alternatives:Please type 1 for "first class"please type 2 for "economy"If the person types 1 , then your program should assign a seat in the first class section (seats...

  • Labs CSIT 210 Introduction to Programming LAB 9B Arrays Change the program AirlineDriver.java to assign passenger seats in an airplane. Chesapeake Airlines is a small commuter airline with seats for...

    Labs CSIT 210 Introduction to Programming LAB 9B Arrays Change the program AirlineDriver.java to assign passenger seats in an airplane. Chesapeake Airlines is a small commuter airline with seats for 20 passengers in 5 rows of 4 seats each. Here is the layout: 1ABCD 2ABCD 3ABCD 4ABCD 5ABCD The user enters the row (1 – 5) and the seat (A – D). The seat entry may be lowercase. Use this code to enter the seat: System.out.print(" Enter row and seat...

  • Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    //...

    Java // Topic 2c // Program reserves airline seats. import java.util.Scanner public class Plane {    // checks customers in and assigns them a boarding pass    // To the human user, Seats 1 to 2 are for First Class passengers and Seats 3 to 5 are for Economy Class passengers    //    public void reserveSeats()    {       int counter = 0;       int section = 0;       int choice = 0;       String eatRest = ""; //to hold junk in input buffer       String inName = "";      ...

  • C# WINDOWS FORMS APPLICATION (not CONSOLE APPLICATION ) (Please screenshot window form of this program and...

    C# WINDOWS FORMS APPLICATION (not CONSOLE APPLICATION ) (Please screenshot window form of this program and write code on computer. Thank you very much !) For this week's assignment , create and complete a Windows application for the following question. Airline Reservation System: An airline has just bought a computer for its new reservation system. Develop a new system to assign seats on the new airplane( capacity: 10 seats) Display the following alternatives: "Please type 1 for first class, and...

  • I could really use some help with this question! Thanks!! Must use C# and it must...

    I could really use some help with this question! Thanks!! Must use C# and it must be a console application ------------------------------------------------------------------------------------------------------------------------------ You start working on this w/o arrays just with seats: bool f1, f2,..., e1, e2,.... You can use Console application for now. A small airline has just purchased a computer for its new automated reservations system. You have been asked to develop the new system. You’re to write an app to assign seats on each flight of the airline’s...

  • Programming language C++. (Airline Reservations System) A small airline has just purchased a computer for its...

    Programming language C++. (Airline Reservations System) A small airline has just purchased a computer for its new au- tomated reservations system. You have been asked to develop the new system. You’re to write an app to assign seats on each flight of the airline’s only plane (capacity: 10 seats). Display the following alternatives: Please type 1 for First Class and Please type 2 for Economy. If the user types 1, your app should assign a seat in the first-class section...

  • C# Question. Please do a lot of documenting of what your code did and thorough explaining...

    C# Question. Please do a lot of documenting of what your code did and thorough explaining in your // comments. A small airline has just purchased a computer for its new automated reservations system. You have been asked to develop the new system. You’re to write an app to assign seats on each flight of the airline’s only plane (capacity: 10 seats). Display the following alternatives: Please enter 1 for First Class or 2 for Economy. If the user types...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT