Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering Problem#1 Engr201 Computer Programming for Engineers Faculty of Engineering Chiang Mai University 1.

Similar presentations


Presentation on theme: "Engineering Problem#1 Engr201 Computer Programming for Engineers Faculty of Engineering Chiang Mai University 1."— Presentation transcript:

1 Engineering Problem#1 Engr201 Computer Programming for Engineers Faculty of Engineering Chiang Mai University 1

2 Engineering Problem#1 Flight seat booking Program –This program displays (1) Seating chart for the entire plane: [ ] is an available seat and [x] is occupied (2) The quantity of available seats. (3) Available seat numbers. –This programs asks for the desired seat number. –This programs stays in the loop until the user enters 0.s 2

3 Example of a run Entire seating numbers Quantity of available seats Available seat numbers User selects a seat number Enter y to confirm the selection 0 to exit the program 3F is now occupied, 3

4 Main Data structure Two dimensional array and global variable, where 0 = available, and 1 = unavailable There are 9 rows and 6 columns int seat[9][6]; 4

5 Functions show_seat() displays the seating chart reserve_ticket() reserves seating list_avail_seat() lists all available seats find_num_avail_seat() displays quantity of available seats init_data() is used to initialized occupied seats with (Prob=0.8) 5

6 Source Code Source Code can be downloaded at http://dl.dropbox.com/u/1628480/airline_ticket.c The code is unfinished. You will need to complete the code. 6

7 show_seat() function void show_seat() { int i,j; char tmp; printf(" %4c%4c%4c %4c%4c%4c\n",'A','B','C','D','E','F'); for(i=0;i<NUM_ROW;i++) { printf("%3d ", i+1); for(j=0;j<NUM_COL;j++) { if (seat[i][j]==1) tmp = 'X'; else tmp = ' '; printf("[%c] ", tmp); if (j==2) { printf(" "); } printf("\n"); } 7

8 reserve_ticket() function(1/2) void reserve_ticket() { char seat_string[10]; char ans; int row; int col; do { show_seat(); printf("Number of available seats=%d\n", num_avail_seat); list_avail_seat(); do { printf("Enter seat number (0 to exit): "); scanf("%s", seat_string); if ((strlen(seat_string)!=1)||(seat_string[0]!='0')) { row = seat_string[0] - '1'; col = seat_string[1] - 'A'; 8

9 reserve_ticket() function(2/2) if ((row =NUM_ROW)||(col =NUM_COL)) { printf("Invalid seat number, please enter again!\n"); } else { printf("Reservation for seat %s, confirm (y/n)?", seat_string); scanf("\n%c", &ans); } } while (((row NUM_ROW)||(col NUM_COL)||(ans!='y'))&&((strlen(seat_stri ng)!=1)||(seat_string[0]!='0'))); if ((strlen(seat_string)!=1)||(seat_string[0]!='0')) { seat[row][col] = 1; num_avail_seat = find_num_avail_seat(); } } while ((strlen(seat_string)!=1)||(seat_string[0]!='0')); } 9

10 list_avail_seat() function void list_avail_seat() { int i,j; printf("Available seats: "); for(i=0;i<NUM_ROW;i++) { for(j=0;j<NUM_COL;j++) { if (seat[i][j] == 0) printf("%c%c ",('1'+i),('A'+j)); } printf("\n"); } 10

11 find_num_avail_seat() function int find_num_avail_seat() { int i,j,k=0; for(i=0;i<NUM_ROW;i++) { for(j=0;j<NUM_COL;j++) { if (seat[i][j] == 0) k++; } return k; } 11

12 init_data() for randomly initializing unavailable seats with (Prob=0.8) void init_data() { int i,j; int a; srand(time(NULL)); for(i=0;i<NUM_ROW;i++) { for(j=0;j<NUM_COL;j++) { a = rand()%100; if (a>=20) { // 80% occupied seat[i][j] = 1; } num_avail_seat = find_num_avail_seat(); } 12

13 Beginning of the program and the main() #include #define NUM_ROW 9 #define NUM_COL 6 void init_data();// function prototype void show_seat(); void reserve_ticket(); void list_avail_seat(); int find_num_avail_seat(); int seat[NUM_ROW][NUM_COL]; int num_avail_seat = NUM_ROW * NUM_COL; void main() { init_data(); reserve_ticket(); } 13

14 Try this program Download and try this program 14

15 Remaining parts This program allows you to select occupied seats ( it should not!) This program does not allow you to select available seats. It displays the following message, The selected seat is not available. Please enter again! and re-prompt for another input. 15


Download ppt "Engineering Problem#1 Engr201 Computer Programming for Engineers Faculty of Engineering Chiang Mai University 1."

Similar presentations


Ads by Google