1. Engineering
  2. Computer Science
  3. i have to write a c program to assign seats...

Question: i have to write a c program to assign seats...

Question details

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
*/

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution