Engineering Programming Iksan Bukhori M Phil Engineering Programming
Engineering Programming Iksan Bukhori, M. Phil.
Engineering Programming � SKS: 3 � Lecturer: – Iksan Bukhori, M. Phil. 1) President University 2009 – 20014 (B. Sc. ) 2) Universiti Teknologi Malaysia 2015 – 2017 (M. Phil. ) – Phone: 0852 1702 9493 – iksan. bukhori@president. ac. id – iksanbukhori@wordpress. com
REFERENSI �Deitel, John. & Deitel, Harvey. C How to Program (6 th Edition). New Jersey, USA : Pearson Eduation, Inc. 2010. �Deitel, Harvey. & Deitel, John. C++ How to Program (5 th Edition). New Jersey, USA : Pearson Education, Inc. 2005.
Course Outline 1. 2. 3. 4. 5. 6. 7. 8. 9. Algorithm and Flow Charts Intro to C Language Data Types and Operators C Program Control: Repetition Functions Array Pointers Intro to C++ Programming Intro to Object Oriented Programming
Rules A. Grades • Final Grade = • • • 10% Homeworks + 20% Quizzes + 30% Midterm Exam + 40% Final Exam + Extra Points Homeworks are to be written on A 4 papers, otherwise they will not be graded. Homeworks must be submitted on time, on the schedule of the lecture. If you submit late, the penalty will be – 10. n points, where n is the total number of lateness made. There will be 2 quizzes. The average of quiz grades contributes 20% of final grade There will be no remedial for any quizzes nor exams. As replacement, you may submit a neat summary of your notes or collection of problems and solutions related to all topics covered up to that point in handwritten with A 4 paper The extra grade depends on the materials covered, how neat it is and how good your understanding of the materials actually is (Copying my slides or the book(s) will not do). These extra grades DO NOT GUARANTEE you passing the quizzes/exams you took.
Programming Homework For each programming homework please do the following: 1. Add comment lines to explain what your program is supposed to do. 2. Write each question in one project named HW#Q#_EP_Student. ID_your. Name Where # denotes homework/question index 3. Write HW#_EP_Student. ID_your. Name. # denotes homework index as the subject in your email
Rules B. Attendance • Attendance is not part of the final grade • C. However, those who did not come to class 4 times or more will automatically get 0 (zero) for final exams regardless of the actual score they got. Class Activities • Lectures will be held in the form of Power. Point presentations. • However, you should never be dependent on what is on the slides. Many important topics might just be discussed or written on the board. • You are expected to write a note along the lectures to record your own conclusions or materials which are not covered by the lecture slides • Latest lecture slides will be uploaded to my blog iksanbukhori@wordpress. com
LECTURE IV: Repetitions
Outline 4. 1 4. 2 4. 3 4. 4 4. 5 4. 6 4. 7 4. 8 4. 9 4. 10 4. 11 4. 12 Introduction The Essentials of Repetition Counter-Controlled Repetition The For Repetition Structure The For Structure: Notes and Observations Examples Using the For Structure The Switch Multiple-Selection Structure The Do/While Repetition Structure The break and continue Statements Logical Operators Confusing Equality (==) and Assignment (=) Operators Structured Programming Summary
4. 1 Introduction • This chapter introduces – Additional repetition control structures • for • do/while – switch multiple selection structure – break statement • Used for exiting immediately and rapidly from certain control structures – continue statement • Used for skipping the remainder of the body of a repetition structure and proceeding with the next iteration of the loop
4. 2 The Essentials of Repetition • Loop – Group of instructions computer executes repeatedly while some condition remains true • Counter-controlled repetition – Definite repetition: know how many times loop will execute – Control variable used to count repetitions • Sentinel-controlled repetition – Indefinite repetition – Used when number of repetitions not known – Sentinel value indicates "end of data"
4. 3 Essentials of Counter-Controlled Repetition • Counter-controlled repetition requires – The name of a control variable (or loop counter) – The initial value of the control variable – A condition that tests for the final value of the control variable (i. e. , whether looping should continue) – An increment (or decrement) by which the control variable is modified each time through the loop
4. 3 Essentials of Counter-Controlled Repetition • Example: int counter = 1; // initialization while ( counter <= 10 ) { // repetition condition printf( "%dn", counter ); ++counter; // increment } – The statement int counter = 1; • • Names counter Declares it to be an integer Reserves space for it in memory Sets it to an initial value of 1
4. 4 The for Repetition Structure • Format when using for loops for ( initialization; loop. Continuation. Test; increment ) statement • Example: for( int counter = 1; counter <= 10; counter++ ) printf( "%dn", counter ); – Prints the integers from one to ten No semicolon (; ) after last expression
4. 4 The for Repetition Structure • For loops can usually be rewritten as while loops: initialization; while ( loop. Continuation. Test ) { statement; increment; } • Initialization and increment – Can be comma-separated lists – Example: for (int i = 0, j = 0; j + i <= 10; j++, i++) printf( "%dn", j + i );
4. 5 The for Structure: Notes and Observations • Arithmetic expressions – Initialization, loop-continuation, and increment can contain arithmetic expressions. If x equals 2 and y equals 10 for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to for ( j = 2; j <= 80; j += 5 ) • Notes about the for structure: – "Increment" may be negative (decrement) – If the loop continuation condition is initially false • The body of the for structure is not performed • Control proceeds with the next statement after the for structure – Control variable • Often printed or used inside for body, but not necessary
1 /* Fig. 4. 5: fig 04_05. c 2 Summation with for */ 3 #include <stdio. h> 4 5 int main() 6 { 7 int sum = 0, number; 8 9 for ( number = 2; number <= 100; number += 2 ) 10 sum += number; 11 12 printf( "Sum is %dn", sum ); 13 14 return 0; 15 } Sum is 2550
4. 7 The switch Multiple-Selection Structur • switch – Useful when a variable or expression is tested for all the values it can assume and different actions are taken • Format – Series of case labels and an optional default case switch ( value ){ case '1': actions case '2': actions default: actions } – break; exits from structure
4. 7 The switch Multiple-Selection Structure • Flowchart of the switch structure
1 2 3 4 5 6 7 8 9 10 11 12 input. n" ); 13 14 15 16 while */ 17 18 uppercase A */ 19 20 21 22 uppercase B */ 23 24 25 26 uppercase C */ 27 28 29 30 uppercase D */ 31 32 /* Fig. 4. 7: fig 04_07. c Counting letter grades */ #include <stdio. h> Note: EOF means End of File. What int main() kind of character this { int grade; is depends on the int a. Count = 0, b. Count = 0, c. Count = 0, system. For d. Count = 0, f. Count = 0; Windows based system, it’s ctrl+z printf( "Enter the letter grades. n" ); printf( "Enter the EOF character to end while ( ( grade = getchar() ) != EOF ) { switch ( grade ) { /* switch nested in case 'A': case 'a': /* grade was ++a. Count; /* or lowercase a */ break; case 'B': case 'b': /* grade was ++b. Count; /* or lowercase b */ break; case 'C': case 'c': /* grade was ++c. Count; /* or lowercase c */ break; case 'D': case 'd': /* grade was ++d. Count; /* or lowercase d */ break; • 1. Initialize variables • 2. Input data • 2. 1 Use switch loop to update count
33 34 uppercase F */ 35 36 case 'F': case 'f': /* grade was ++f. Count; /* or lowercase f */ break; 37 input */ 38 case 'n': case' ': /* ignore these in 39 break; 40 41 characters */ 42 entered. " ); 43 default: /* catch all other printf( "Incorrect letter grade printf( " Enter a new grade. n" ); 44 break; 45 } 46 } 47 ); 48 printf( "n. Totals for each letter grade are: n" 49 printf( "A: %dn", a. Count ); 50 printf( "B: %dn", b. Count ); 51 printf( "C: %dn", c. Count ); 52 printf( "D: %dn", d. Count ); 53 printf( "F: %dn", f. Count ); 54 55 return 0; 56 } • 2. 1 Use switch loop to update count • 3. Print results
Enter the letter grades. Enter the EOF character to end input. A B C C A D F C E Incorrect letter grade entered. Enter a new grade. D A B Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1 • Program Output
4. 8 The do/while Repetition Structure • The do/while repetition structure – Similar to the while structure – Condition for repetition tested after the body of the loop is performed • All actions are performed at least once – Format: do { statement; } while ( condition );
4. 8 The do/while Repetition Structure • Example (letting counter = 1): do { printf( "%d ", counter ); } while (++counter <= 10); – Prints the integers from 1 to 10
4. 8 The do/while Repetition Structure • Flowchart of the do/while repetition structure
1 /* Fig. 4. 9: fig 04_09. c 2 Using the do/while repetition structure */ 3 #include <stdio. h> 4 5 int main() 6 { 7 int counter = 1; 8 9 do { 10 printf( "%d ", counter ); 11 } while ( ++counter <= 10 ); • 1. Initialize variable • 2. Loop • 3. Print 12 13 return 0; 14 } 1 2 3 4 5 6 7 8 9 10 • Program Output
4. 9 The break and continue Statements • break – Causes immediate exit from a while, for, do/while or switch structure – Program execution continues with the first statement after the structure – Common uses of the break statement • Escape early from a loop • Skip the remainder of a switch structure
4. 9 The break and continue Statements • continue – Skips the remaining statements in the body of a while, for or do/while structure • Proceeds with the next iteration of the loop – while and do/while • Loop-continuation test is evaluated immediately after the continue statement is executed – for • Increment expression is executed, then the loop -continuation test is evaluated
*/ 1 /* Fig. 4. 12: fig 04_12. c 2 Using the continue statement in a for structure 3 #include <stdio. h> 4 5 int main() 6 { 7 int x; 8 9 for ( x = 1; x <= 10; x++ ) { • 1. Initialize variable • 2. Loop • 3. Print 10 only 11 if ( x == 5 ) 12 continue; /* skip remaining code in loop 13 if x == 5 */ 14 15 printf( "%d ", x ); 16 } 17 18 value 5n" ); 19 20 printf( "n. Used continue to skip printing the return 0; } 1 2 3 4 6 7 8 9 10 Used continue to skip printing the value 5 • Program Output
4. 10 Logical Operators • && ( logical AND ) – Returns true if both conditions are true • || ( logical OR ) – Returns true if either of its conditions are true • ! ( logical NOT, logical negation ) – Reverses the truth/falsity of its condition – Unary operator, has one operand • Useful as conditions in loops Expression true && false true || false !false Result false true
4. 11 Confusing Equality (==) and Assignment (=) Operators • Dangerous error – Does not ordinarily cause syntax errors – Any expression that produces a value can be used in control structures – Nonzero values are true, zero values are false – Example using ==: if ( pay. Code == 4 ) printf( "You get a bonus!n" ); • Checks paycode, if it is 4 then a bonus is awarded
4. 11 Confusing Equality (==) and Assignment (=) Operators – Example, replacing == with =: if ( pay. Code = 4 ) printf( "You get a bonus!n" ); • This sets paycode to 4 • 4 is nonzero, so expression is true, and bonus awarded no matter what the paycode was – Logic error, not a syntax error
4. 11 Confusing Equality (==) and Assignment (=) Operators • lvalues – Expressions that can appear on the left side of an equation – Their values can be changed, such as variable names • x = 4; • rvalues – Expressions that can only appear on the right side of an equation – Constants, such as numbers • Cannot write 4 = x; • Must write x = 4; – lvalues can be used as rvalues, but not vice versa • y = x;
Homework 4 1. Write a program which uses loop to ask user to enter 10 grades, and print the maximum and average. 2. Similar to Q 1, but this time the user should be able to input as many grades as they want, until they input give input -1. That is, the loop should be terminated once the user entered -1. Note: For both of these questions, you also have to print out some informative statements alongside the result of the formula, for example: “The roots of your quadratic function are …. And …. ”
Submission Format For each programming homework please do the following: 1. Add comment lines to explain what your program is supposed to do. 2. Write each question in one project named HW#Q#_EP_Student. ID_your. Name Where # denotes homework/question index 3. Write HW#_EP_Student. ID_your. Name. # denotes homework index as the subject in your email
- Slides: 35