Sharif University of Technology Fundamentals of Programming Lecture
































































- Slides: 64

Sharif University of Technology Fundamentals of Programming Lecture 5 - Structured Program Development Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad

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

Structured Program Development– Lecture 5 Requirements Discovery • Problem Analysis – Problem Identification – Abstraction • Inputs and outputs determination • Defining their relation Write a program to compute an employee's weekly pay? 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 3 Sharif University of Technology

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 4 Sharif University of Technology

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) • • 5 Action Order Sharif University of Technology

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 6 Sharif University of Technology

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 7 Sharif University of Technology

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 8 Sharif University of Technology

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

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 10 END Sharif University of Technology

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

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

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

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

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

Structured Program Development– Lecture 5 Execution 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: ? START Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 16 Sharif University of Technology

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

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 18 Sharif University of Technology

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 START A END A 19 Sharif University of Technology

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 • A separate flowchart can be constructed for the module 20 Read Input module Call calc_pay function Display results END Sharif University of Technology

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 21 Sharif University of Technology

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

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, { } 23 Sharif University of Technology

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 24 Process B Sharif University of Technology

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 x < y? if (x < y) a = x * 2; Calculate a as x plus y. 25 Calculate a as x times 2. else a = x + y; Sharif University of Technology

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" ); } 26 Sharif University of Technology

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. 27 Sharif University of Technology

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"); 28 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; 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? 29 Sharif University of Technology

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; 30 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++; Sharif University of Technology

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

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 1 bonus = 100 32 If years_employed = 3, bonus is set to 400 If years_employed is any other value, bonus is set to 800 CASE years_employed 2 3 bonus = 200 bonus = 400 Other bonus = 800 Sharif University of Technology

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 33 Sharif University of Technology

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

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

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? 36 YES Process A Sharif University of Technology

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

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

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? 39 x < y? YES Display x Sharif University of Technology

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

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? 41 YES Display x Add 1 to x Sharif University of Technology

Structured Program Development– Lecture 5 Example 42 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 ++); } Sharif University of Technology

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 { printf(…); x++; } while (x < y); 43 Display x Add 1 to x x < y? YES Sharif University of Technology

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) 44 Sharif University of Technology

Structured Program Development– Lecture 5 Example Draw a flowchart for the following problem: Read 5 integer and display the value of their summation. 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 45 Sharif University of Technology

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 46 Sharif University of Technology

Structured Program Development– Lecture 5 Assume input example: 2 3 4 5 6 counter ← 1, sum ← 0 counter < 6 false 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 counter++ output sum 47 This loop Uses only is The counter-controlled 3 variables Increases by 1 Sharif University of Technology

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 48 Sharif University of Technology

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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++; } } 64 getch(); return 0; Sharif University of Technology