Fundamental of Programming C Lecturer Omid Jafarinezhad Lecture

  • Slides: 65
Download presentation
Fundamental of Programming (C) Lecturer: Omid Jafarinezhad Lecture 5 Structured Program Development Department of

Fundamental of Programming (C) Lecturer: Omid Jafarinezhad Lecture 5 Structured Program Development Department of Computer Engineering 1 Sharif University of Technology

Structured Program Development – Lecture 5 How to develop a program? Requirements Problem Analysis

Structured Program Development – Lecture 5 How to develop a program? Requirements Problem Analysis Designing algorithm Implementation Implementing algorithm in c/c++ Validation Test Maintenance Department of Computer Engineering 2 Refinement Pseudo code Flow chart Software Development Life-Cycle Sharif University of Technology

Structured Program Development – Lecture 5 Department of Computer Engineering 3 Sharif University of

Structured Program Development – Lecture 5 Department of Computer Engineering 3 Sharif University of Technology

Structured Program Development – Lecture 5 Requirements Discovery • Problem Analysis Write a program

Structured Program Development – Lecture 5 Requirements Discovery • Problem Analysis Write a program to compute an employee's weekly pay? – Problem Identification – Abstraction • Inputs and outputs determination • Defining their relation How many hours did you work? How much do you get paid per hour? employee's weekly pay equal to multiplication of Hours by Pay Rate Department of Computer Engineering 4 Sharif University of Technology

Structured Program Development – Lecture 5 Problem vs. Solution • A problem is something

Structured Program Development – Lecture 5 Problem vs. Solution • A problem is something that causes trouble • The solution is how to solve or fixe the problem Department of Computer Engineering 5 Sharif University of Technology

Structured Program Development – Lecture 5 Algorithm • A procedure for solving a problem

Structured Program Development – Lecture 5 Algorithm • A procedure for solving a problem in terms of actions and the order in which these actions are to be executed in a computer program (program control) • Action • Order Department of Computer Engineering 6 Sharif University of Technology

Structured Program Development – Lecture 5 Execution order • Sequential execution, normally, statements in

Structured Program Development – Lecture 5 Execution order • Sequential execution, normally, statements in a program are executed one after the other in the order in which they’re written • Various C statements enable you to specify that the next statement to be executed may be other than the next one in sequence. This is called transfer of control Department of Computer Engineering 7 Sharif University of Technology

Structured Program Development – Lecture 5 Structured Programming • goto statement that allows programmers

Structured Program Development – Lecture 5 Structured Programming • goto statement that allows programmers to specify a transfer of control to one of many possible destinations in a program • Research had demonstrated that programs could be written without any goto statements • Programs produced with structured techniques were clearer, easier to debug and modify and more likely to be bug free in the first place. • Research had demonstrated that all programs could be written in terms of only three control structures, namely the sequence structure, the selection structure and the repetition structure Department of Computer Engineering 8 Sharif University of Technology

Structured Program Development – Lecture 5 Pseudo code • An artificial and informal language

Structured Program Development – Lecture 5 Pseudo code • An artificial and informal language that helps you develop algorithms – similar to everyday English – are not executed on computers – consists only of action statements pay-calculation program: 1. Read Hours and Pay Rate 2. weekly pay = Read Hours * Pay Rate Department of Computer Engineering 9 Sharif University of Technology

Structured Program Development – Lecture 5 Flowchart START • A flowchart is a diagram

Structured Program Development – Lecture 5 Flowchart START • A flowchart is a diagram that depicts the flow of an algorithm – pay-calculation program Display message “How many hours did you work? ” Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate • Each symbol represents a different type of operation Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 10 Sharif University of Technology

Structured Program Development – Lecture 5 Terminals START Terminal • represented by rounded rectangles

Structured Program Development – Lecture 5 Terminals START Terminal • represented by rounded rectangles • indicate a starting or ending point Display message “How many hours did you work? ” Read Hours Display message “How much do you get paid per hour? ” START Read Pay Rate END Multiply Hours by Pay Rate. Store result in Gross Pay. Flow line Display Gross Pay Terminal Department of Computer Engineering 11 END Sharif University of Technology

Structured Program Development – Lecture 5 Input/Output Operations • indicate an input or output

Structured Program Development – Lecture 5 Input/Output Operations • indicate an input or output operation START Display message “How many hours did you work? ” Read Hours Display message “How many hours did you work? ” Input/Output Operation Display message “How much do you get paid per hour? ” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Read Hours Display Gross Pay END Department of Computer Engineering 12 Sharif University of Technology

Structured Program Development – Lecture 5 Processes START • indicates a process such as

Structured Program Development – Lecture 5 Processes START • indicates a process such as a mathematical computation or variable assignment Multiply Hours by Pay Rate. Store result in Gross Pay. Display message “How many hours did you work? ” Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Process Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 13 Sharif University of Technology

Structured Program Development – Lecture 5 Execution START Display message “How many hours did

Structured Program Development – Lecture 5 Execution START Display message “How many hours did you work? ” Output Operation Read Hours How many hours did you work? Display message “How much do you get paid per hour? ” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Variable Contents: Hours: ? Pay Rate: ? Gross Pay: ? Display Gross Pay END Department of Computer Engineering 14 Sharif University of Technology

Structured Program Development – Lecture 5 Execution START Input Operation (User types 40) How

Structured Program Development – Lecture 5 Execution START Input Operation (User types 40) How many hours did you work? Display message “How many hours did you work? ” Read Hours Display message “How much do you get paid per hour? ” 40 Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ? Display Gross Pay END Department of Computer Engineering 15 Sharif University of Technology

Structured Program Development – Lecture 5 Execution START Display message “How many hours did

Structured Program Development – Lecture 5 Execution START Display message “How many hours did you work? ” Read Hours How much do you get paid per hour? Display message “How much do you get paid per hour? ” Output Operation Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ? Display Gross Pay END Department of Computer Engineering 16 Sharif University of Technology

Structured Program Development – Lecture 5 Execution START Display message “How many hours did

Structured Program Development – Lecture 5 Execution START Display message “How many hours did you work? ” Read Hours How many hours did you work? Display message “How much do you get paid per hour? ” 20 Input Operation (User types 20) Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: ? Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 17 Sharif University of Technology

Structured Program Development – Lecture 5 Execution START Display message “How many hours did

Structured Program Development – Lecture 5 Execution START Display message “How many hours did you work? ” Read Hours How many hours did you work? Display message “How much do you get paid per hour? ” 20 Read Pay Rate Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 Multiply Hours by Pay Rate. Store result in Gross Pay. Process (Gross Pay = Hours * Pay Rate) Display Gross Pay END Department of Computer Engineering 18 Sharif University of Technology

Structured Program Development – Lecture 5 Execution START Display message “How many hours did

Structured Program Development – Lecture 5 Execution START Display message “How many hours did you work? ” Read Hours Your gross pay is 800 Display message “How much do you get paid per hour? ” Read Pay Rate Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 Multiply Hours by Pay Rate. Store result in Gross Pay. Output Operation Display Gross Pay END Department of Computer Engineering 19 Sharif University of Technology

Structured Program Development – Lecture 5 Connectors • Sometimes a flowchart will not fit

Structured Program Development – Lecture 5 Connectors • Sometimes a flowchart will not fit on one page – A connector (represented by a small circle) allows you to connect two flowchart segments A START END A Department of Computer Engineering 20 Sharif University of Technology

Structured Program Development – Lecture 5 Modules • A program module (such as a

Structured Program Development – Lecture 5 Modules • A program module (such as a function in C) is represented by a special symbol START • The position of the module symbol indicates the point the module is executed Read Input module Call calc_pay function Display results • A separate flowchart can be constructed for the module Department of Computer Engineering 21 END Sharif University of Technology

Structured Program Development – Lecture 5 Control Structures • Sequence • Decision selection statement

Structured Program Development – Lecture 5 Control Structures • Sequence • Decision selection statement – The if statement is called a single-selection statement because it selects or ignores a single action. – The if…else statement is called a double-selection statement because it selects between two different actions. – The switch statement is called a multiple-selection statement because it selects among many different actions • Repetition – while – do…while – for Department of Computer Engineering 22 Sharif University of Technology

Structured Program Development – Lecture 5 Sequence Structure • a series of actions are

Structured Program Development – Lecture 5 Sequence Structure • a series of actions are performed in sequence – The pay-calculating example Department of Computer Engineering 23 Sharif University of Technology

Structured Program Development – Lecture 5 Compound Statements • A statement is a specification

Structured Program Development – Lecture 5 Compound Statements • A statement is a specification of an action to be taken by the computer as the program executes • Compound Statements is a list of statements enclosed in braces, { } Department of Computer Engineering 24 Sharif University of Technology

Structured Program Development – Lecture 5 Decision Structure • One of two possible actions

Structured Program Development – Lecture 5 Decision Structure • One of two possible actions is taken, depending on a condition • Selection structures are used to choose among alternative courses of action NO YES x < y? Process A Department of Computer Engineering 25 Process B Sharif University of Technology

Structured Program Development – Lecture 5 Decision Structure • The flowchart segment below shows

Structured Program Development – Lecture 5 Decision Structure • The flowchart segment below shows how a decision structure is expressed in C as an if/else statement Flowchart NO C programming language YES if (x < y) x < y? a = x * 2; Calculate a as x plus y. Calculate a as x times 2. Department of Computer Engineering 26 else a = x + y; Sharif University of Technology

Structured Program Development – Lecture 5 Example • Compound statement is way to group

Structured Program Development – Lecture 5 Example • Compound statement is way to group many statements together so they are treated as one if (grade >= 60) printf( "Passed. n" ); // { printf( "Passed. n" ); } else { printf( "Failed. n" ); printf( "You must take this course again. n" ); } Department of Computer Engineering 27 Sharif University of Technology

Structured Program Development – Lecture 5 Decision Structure • The flowchart segment below shows

Structured Program Development – Lecture 5 Decision Structure • The flowchart segment below shows a decision structure with only one action to perform C programming language Flowchart NO YES if (x < y) x < y? a = x * 2; Calculate a as x times 2. Department of Computer Engineering 28 Sharif University of Technology

Structured Program Development – Lecture 5 Combining Structures NO YES x > min? if

Structured Program Development – Lecture 5 Combining Structures NO YES x > min? if (x > min) Display “x is outside { the limits. ” if (x < max) printf("x is within the limits"); else printf("x is outside the limits"); } else printf("x is outside the limits"); Department of Computer Engineering 29 NO YES x < max? Display “x is outside the limits. ” Display “x is within limits. ” Sharif University of Technology

Structured Program Development – Lecture 5 Example int k = 1, m = 4;

Structured Program Development – Lecture 5 Example int k = 1, m = 4; if (k < 2 || m == 3) { m = 2 + k; printf("%d", m); } else { k = 1; printf("%d", k); } if (k < 2 || m == 3) { m = 2 + k, printf("%d", m); } else { k = 1, printf("%d", k); } Which is more readable? Department of Computer Engineering 30 Sharif University of Technology

Structured Program Development – Lecture 5 Example if(x) if(y) printf("Yes"); else printf("No"); if (x

Structured Program Development – Lecture 5 Example if(x) if(y) printf("Yes"); else printf("No"); if (x < 0) sign = -1; else if (x == 0) sign = 0; else sign = 1; if(x) { if(y) printf("Yes"); else printf("No"); } if(x) { if(y) printf("Yes"); } else printf("No"); if (x < 0. 25) count 1++; else if (x >= 0. 25 && x < 0. 5) count 2++; else if (x >= 0. 5 && x < 0. 75) count 3++; else count 4++; Department of Computer Engineering 31 Sharif University of Technology

Structured Program Development – Lecture 5 Case Structure • One of several possible actions

Structured Program Development – Lecture 5 Case Structure • One of several possible actions is taken, depending on the contents of a variable Department of Computer Engineering 32 Sharif University of Technology

Structured Program Development – Lecture 5 Case Structure • indicates actions to perform depending

Structured Program Development – Lecture 5 Case Structure • indicates actions to perform depending on the value in years_employed If years_employed = 2, bonus is set to 200 If years_employed = 1, bonus is set to 100 If years_employed is any other value, bonus is set to 800 CASE years_employed 1 bonus = 100 If years_employed = 3, bonus is set to 400 2 3 bonus = 200 bonus = 400 Department of Computer Engineering 33 Other bonus = 800 Sharif University of Technology

Structured Program Development – Lecture 5 switch • A switch statement allows a single

Structured Program Development – Lecture 5 switch • A switch statement allows a single variable (integer or char) to be compared with several possible constants – A constant can not appear more than once, and there can only be one default expression Department of Computer Engineering 34 Sharif University of Technology

Structured Program Development – Lecture 5 switch (variable) { case const: statements. . .

Structured Program Development – Lecture 5 switch (variable) { case const: statements. . . ; default: statements. . . ; } Department of Computer Engineering switch (c = toupper(getch())) { case ‘R’: printf("Red"); break; case ‘G’: printf("Green"); break; default: printf("other"); } 35 Sharif University of Technology

Structured Program Development – Lecture 5 Example switch(betty) { case 1: printf("betty = 1n");

Structured Program Development – Lecture 5 Example switch(betty) { case 1: printf("betty = 1n"); case 2: printf("betty=2n"); break; CASE betty? 1 2 betty = 1 betty = 2 3 betty = 3 Other Not sure case 3: printf("betty=3n"); break; default: } printf("Not suren"); Department of Computer Engineering 36 Sharif University of Technology

Structured Program Development – Lecture 5 Repetition Structure • A loop tests a condition,

Structured Program Development – Lecture 5 Repetition Structure • A loop tests a condition, and if the condition exists, it performs an action. Then it tests the condition again. If the condition still exists, the action is repeated. This continues until the condition no longer exists x < y? Department of Computer Engineering 37 YES Process A Sharif University of Technology

Structured Program Development – Lecture 5 Repetition Structure • The flowchart segment below shows

Structured Program Development – Lecture 5 Repetition Structure • The flowchart segment below shows a repetition structure expressed in C as a while loop C programming language Flowchart while (x < y) x < y? YES x++; Add 1 to x Department of Computer Engineering 38 Sharif University of Technology

Structured Program Development – Lecture 5 While while (loop_repetition_condition) statement; OR //Compound statement while

Structured Program Development – Lecture 5 While while (loop_repetition_condition) statement; OR //Compound statement while (loop_repetition_condition) { statement 1; statement 2; // … } Department of Computer Engineering 39 Sharif University of Technology

Structured Program Development – Lecture 5 Controlling a Repetition Structure • The action performed

Structured Program Development – Lecture 5 Controlling a Repetition Structure • The action performed by a repetition structure must eventually cause the loop to terminate. Otherwise, an infinite loop is created • In this flowchart segment, x is never changed. Once the loop starts, it will never end • How can this flowchart be modified so it is no longer an infinite loop? Department of Computer Engineering 40 x < y? YES Display x Sharif University of Technology

Structured Program Development – Lecture 5 Controlling a Repetition Structure • Adding an action

Structured Program Development – Lecture 5 Controlling a Repetition Structure • Adding an action within the repetition that changes the value of x x < y? YES Department of Computer Engineering Display x 41 Add 1 to x Sharif University of Technology

Structured Program Development – Lecture 5 A Pre-Test Repetition Structure • This type of

Structured Program Development – Lecture 5 A Pre-Test Repetition Structure • This type of structure is known as a pre-test repetition structure. The condition is tested BEFORE any actions are performed – if the condition does not exist, the loop will never begin x < y? YES Department of Computer Engineering Display x 42 Add 1 to x Sharif University of Technology

Structured Program Development – Lecture 5 Example while (1); int counter = 0; while

Structured Program Development – Lecture 5 Example while (1); int counter = 0; while (counter < 1000) ; int counter = 0; while (counter < 9) printf("%dn", counter ++); int counter = 9; while (counter > 0) printf("%dn", counter --); int counter = 0; while (counter < 9) { printf("%dn", counter); counter++; } int counter = 0; while (counter < 9) { printf("%dn", counter ++); } Department of Computer Engineering 43 Sharif University of Technology

Structured Program Development – Lecture 5 A Post-Test Repetition Structure • The condition is

Structured Program Development – Lecture 5 A Post-Test Repetition Structure • The condition is tested AFTER the actions are performed – A post-test repetition structure always performs its actions at least once C programming language do { Add 1 to x printf(…); x++; } while (x < y); Department of Computer Engineering Display x x < y? 44 YES Sharif University of Technology

Structured Program Development – Lecture 5 do-while do statement; while (loop_repetition_condition) OR do //Compound

Structured Program Development – Lecture 5 do-while do statement; while (loop_repetition_condition) OR do //Compound statement { statement 1; statement 2; // … } while (loop_repetition_condition) Department of Computer Engineering 45 Sharif University of Technology

Structured Program Development – Lecture 5 Example Draw a flowchart for the following problem:

Structured Program Development – Lecture 5 Example Draw a flowchart for the following problem: Read 5 integer and display the value of their summation. Can you identify the input and output? Input : 5 integer n 1, n 2, n 3, n 4, n 5 Output: The summation of n 1, n 2, . . , n 5 Input example: 2 3 4 5 6 Output example: 20 Department of Computer Engineering 46 Sharif University of Technology

Structured Program Development – Lecture 5 Assume input example: 2 3 4 5 6

Structured Program Development – Lecture 5 Assume input example: 2 3 4 5 6 start Input n 1 2 n 2 3 n 3 4 input n 5 n 4 5 sum ← n 1+n 2+n 3+n 4+n 5 6 output sum 20 Input n 2 This flowchart does not use loop, hence we need to use 6 different variables Input n 3 input n 4 end Department of Computer Engineering 47 Sharif University of Technology

Structured Program Development – Lecture 5 Assume input example: 2 3 4 5 6

Structured Program Development – Lecture 5 Assume input example: 2 3 4 5 6 counter ← 1, sum ← 0 false counter < 6 counter 1 2 3 4 5 6 sum 14 20 0 2 5 9 65 14<<<666 2 3 14 0 ++ 5 2 5 9 26 3 4 false true input n n 2 3 4 5 6 sum ← sum + n This loop Uses only is The counter-controlled 3 variables Increases by 1 counter++ output sum Department of Computer Engineering 48 Sharif University of Technology

Structured Program Development – Lecture 5 Decreasing Counter -Controlled Loop counter ← 5, sum

Structured Program Development – Lecture 5 Decreasing Counter -Controlled Loop counter ← 5, sum ← 0 counter > 0 false true input x sum←sum+ x counter-output sum Department of Computer Engineering 49 Sharif University of Technology

Structured Program Development – Lecture 5 For for (initial_value ; condition; update_counter) statement; OR

Structured Program Development – Lecture 5 For for (initial_value ; condition; update_counter) statement; OR // Compound statement for (initial_value ; condition; update_counter) { statement; // … } Department of Computer Engineering 50 Sharif University of Technology

Structured Program Development – Lecture 5 counter ← 1, sum ← 0 counter <

Structured Program Development – Lecture 5 counter ← 1, sum ← 0 counter < 6 int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(“%d”, &x); sum = sum + x; } printf(“%d”, sum); false true input n sum ← sum + n counter++ output sum Department of Computer Engineering 51 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); ? ? ? num _ Department of Computer Engineering 52 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 num _ Department of Computer Engineering 53 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 num _ Department of Computer Engineering 54 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 1 num _ Department of Computer Engineering 55 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 2 num _ Department of Computer Engineering 56 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 2 num _ Department of Computer Engineering 57 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 2 2 num _ Department of Computer Engineering 58 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 2 3 num _ Department of Computer Engineering 59 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 2 3 num _ Department of Computer Engineering 60 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 2 3 Department of Computer Engineering 3 num _ 61 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 2 3 Department of Computer Engineering 4 num _ 62 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 2 3 Department of Computer Engineering 4 num _ 63 Sharif University of Technology

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3;

Structured Program Development – Lecture 5 Example: for (num = 1; num <= 3; num++ ) printf(“%dt”, num); printf(“have come to exitn”); 1 2 3 Department of Computer Engineering 4 num have come to exit_ 64 Sharif University of Technology

Structured Program Development – Lecture 5 Example * ** ****** #include <stdio. h> int

Structured Program Development – Lecture 5 Example * ** ****** #include <stdio. h> int main() { int i. Counter = 0; int j. Counter = 0; while (i. Counter < 7) { j. Counter = 0; while (j. Counter < i. Counter) { printf("*"); j. Counter++; } printf("n"); i. Counter++; } } getch(); return 0; Department of Computer Engineering 65 Sharif University of Technology