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

C programming- 23) Illustrating two dimensional array :- Program to enable Seat

ID: 3710358 • Letter: C

Question

C programming-

23) Illustrating two dimensional array :-

Program to enable Seat selection and booking

//Set row and column size

#define ROWS 20

#define COLS 6

int main()

{

                char seats[ROWS][COLS];

                //Initialize every even numbered seat with O(available) and odd numbered seat with X(unavailable) using two nested for loops and all the seats with column number two are aisle seats, you can denote them with ‘A’.

//print out the seating list

                //check if the passenger wants a window seat or aisle seat

                //print out all the available seats (window seat or aisle seat)

                //read the seat that the user wants to book

                //book the seat (set the seat to ‘X’)

                //print the new seating list

}

Explanation / Answer

PLEASE REFER BELOW CODE

#include<stdlib.h>
#include<stdio.h>


//Set row and column size
#define ROWS 20
#define COLS 6
int main()

{
char seats[ROWS][COLS];
int row,col;
char choice;
//Initialize every even numbered seat with O(available) and odd
//numbered seat with X(unavailable) using two nested for loops and all
//the seats with column number two are aisle seats, you can denote them with ‘A’.

for(row = 0; row < ROWS; row++)
{
for(col = 0; col < COLS; col++)
{
if( !(row % 2) && !(col % 2) && (col != 2))
seats[row][col] = 'O';

else if(col == 2)
seats[row][col] = 'A';

else
seats[row][col] = 'X';
}
}
//print out the seating list
for(row = 0; row < ROWS; row++)
{
for(col = 0; col < COLS; col++)
{
printf("%c ", seats[row][col]);
}
printf(" ");
}
//check if the passenger wants a window seat or aisle seat
printf(" Choose Aisle(a) or Window(w) seat : ");
scanf("%c", &choice);

//print out all the available seats (window seat or aisle seat)
printf(" ");
for(row = 0; row < ROWS; row++)
{
for(col = 0; col < COLS; col++)
{
if(seats[row][col] == 'O' || seats[row][col] == 'A' )
printf("%c ", seats[row][col]);

else
printf(" ");
}
printf(" ");
}
//read the seat that the user wants to book
printf("Enter row and column Number of selected seat : ");
scanf("%d %d", &row, &col);

//book the seat (set the seat to ‘X’)
seats[row][col] = 'X';

//print the new seating list
printf(" ");
for(row = 0; row < ROWS; row++)
{
for(col = 0; col < COLS; col++)
{
printf("%c ", seats[row][col]);
}
printf(" ");
}
return 0;
}

PLEASE REFER BELOW OUTPUT

O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X


Choose Aisle(a) or Window(w) seat : a


O A O
A
O A O
A
O A O
A
O A O
A
O A O
A
O A O
A
O A O
A
O A O
A
O A O
A
O A O
A
Enter row and column Number of selected seat : 2 2


O X A X O X
X X A X X X
O X X X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X
O X A X O X
X X A X X X

Process returned 0 (0x0) execution time : 33.352 s
Press any key to continue.