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

Slides:



Advertisements
Similar presentations
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Advertisements

 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
1 Introduction to Computing Lecture 15 Arrays (Part 1) Dr. Bekir KARLIK Yasar University Department of Computer Engineering
C workshop #3 flow control / strings.
1 CSE1301 Computer Programming Lecture 10: Iteration (Part 1)
Multiple-Subscripted Array
1 CSE1301 Computer Programming Lecture 18 Arrays (Part 1)
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II) Introduction to Computing Lecture 07: Repetition and Loop Statements (Part.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CPCS 202 Chapter 2 – Input/Output
A First Book of ANSI C Fourth Edition
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Artificial Intelligence and Robotics By Keith Bright & John DeBovis.
Nested LOOPS.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions (continued)
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
WEEK 6 Class Activities Lecturer’s slides.
How to design and code functions Chapter 4 (ctd).
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
Computer programming Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
UNIT 11 Random Numbers.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Computer Programming for Engineers
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Multidimensional Arrays As Parameter void fun ( int matrix [] [10] ) {…} void main ( ) { int mat[5][10]; … fun(mat); } void fun (float *mat_ptr, int num_rows,
Arrays float Scores[9]; ? index: element // one dimensional array 1.
EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100.
Revision. Generating a pseudo-random number Necessary libraries: and Seeding: srand(time(NULL))  We set the seed based on the current time  NB! Time-dependent.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
CSE 220 – C Programming Loops.
Introduction to Programming
Array 9/8/2018.
Week 4 – Chapter 3 Repetition.
CS1010 Programming Methodology
CS 1430: Programming in C++.
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Looping.
UMBC CMSC 104 – Section 01, Fall 2016
ECE 103 Engineering Programming Chapter 19 Nested Loops
More Loops Topics Counter-Controlled (Definite) Repetition
Character Arrays char string1[] = “first”;
More Loops Topics Counter-Controlled (Definite) Repetition
Computer Security Password Policy.
More Loops Topics Counter-Controlled (Definite) Repetition
Visit for more Learning Resources
Presentation transcript:

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

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

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

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

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

Source Code Source Code can be downloaded at The code is unfinished. You will need to complete the code. 6

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

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

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

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

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

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

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

Try this program Download and try this program 14

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