Fundamentals of Programming Lecture 5 - Structured Program Development Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad
How to develop a program? Requirements Problem Analysis Refinement Design Designing algorithm Pseudo code Flow chart Implementation Implementing algorithm in c/c++ Validation Maintenance Test Software Development Life-Cycle 2
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
Problem vs. Solution A problem is something that causes trouble The solution is how to solve or fixe the problem 4
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 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
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
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
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
Terminals represented by rounded rectangles indicate a starting or ending point Terminal START Display message How many hours did you work? Read Hours START Display message How much do you get paid per hour? Read Pay Rate END Multiply Hours by Pay Rate. Store result in Gross Pay. Flow line Display Gross Pay Terminal END 10
Input/Output Operations indicate an input or output operation START Display message How many hours did you work? Display message How many hours did you work? Input/Output Operation Read Hours 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
Processes indicates a process such as a mathematical computation or variable assignment Multiply Hours by Pay Rate. Store result in Gross Pay. Process START Display message How many hours did you work? Read Hours Display message How much do you get paid per hour? Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 12
Execution START Display message How many hours did you work? Output Operation How many hours did you work? 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
How many hours did you work? 40 Execution Input Operation (User types 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
Execution START Display message How many hours did you work? How much do you get paid per hour? 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
Execution START Display message How many hours did you work? How many hours did you work? 20 Read Hours Display message How much do you get paid per hour? Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay:? Input Operation (User types 20) Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 16
Execution START Display message How many hours did you work? How many hours did you work? 20 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 (Gross Pay = Hours * Pay Rate) Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 17
Execution START Display message How many hours did you work? Your gross pay is 800 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 18 Output Operation Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END
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
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 module Read Input Call calc_pay function A separate flowchart can be constructed for the module Display results END 20
Sequence Decision selection statement Control Structures 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
Sequence Structure a series of actions are performed in sequence The pay-calculating example 22
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
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 NO x < y? YES Process A Process B 24
Decision Structure The flowchart segment below shows how a decision structure is expressed in C as an if/else statement Flowchart C programming language NO x < y? YES if (x < y) a = x * 2; Calculate a as x plus y. Calculate a as x times 2. else a = x + y; 25
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
Decision Structure The flowchart segment below shows a decision structure with only one action to perform Flowchart C programming language NO x < y? YES if (x < y) a = x * 2; Calculate a as x times 2. 27
Combining Structures NO x > min? YES if (x > min) { if (x < max) printf("x is within the limits"); else printf("x is outside the limits"); } else printf("x is outside the limits"); Display x is outside the limits. NO Display x is outside the limits. x < max? YES Display x is within limits. 28
int k = 1, m = 4; if (k < 2 m == 3) { m = 2 + k; printf("%d", m); } else { k = 1; printf("%d", k); } Example int k = 1, m = 4; if (k < 2 m == 3) { m = 2 + k, printf("%d", m); } else { k = 1, printf("%d", k); } Which is more readable? 29
if(x) if(y) printf("yes"); else printf("no"); if (x < 0) sign = -1; else if (x == 0) sign = 0; else sign = 1; Example if(x) { if(y) printf("yes"); else printf("no"); } if (x < 0.25) count1++; else if (x >= 0.25 && x < 0.5) count2++; else if (x >= 0.5 && x < 0.75) count3++; else count4++; if(x) { if(y) printf("yes"); } else printf("no"); 30
Case Structure One of several possible actions is taken, depending on the contents of a variable 31
Case Structure indicates actions to perform depending on the value in years_employed If years_employed = 1, bonus is set to 100 If years_employed = 2, bonus is set to 200 CASE years_employed If years_employed = 3, bonus is set to 400 If years_employed is any other value, bonus is set to 800 1 2 3 Other bonus = 100 bonus = 200 bonus = 400 bonus = 800 32
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
switch switch (variable) { case const: statements...; default: statements...; } switch (c = toupper(getch())) { case R : printf("red"); break; case G : printf("green"); break; default: printf("other"); } 34
switch(betty) { case 1: printf("betty = 1\n"); case 2: printf("betty=2\n"); break; case 3: printf("betty=3\n"); break; default: printf("not sure\n"); } Example CASE betty? 1 2 3 Other betty = 1 betty = 2 betty = 3 Not sure 35
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? YES Process A 36
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? YES Add 1 to x x++; 37
While while (loop_repetition_condition) statement; OR //Compound statement while (loop_repetition_condition) { statement1; statement2; // } 38
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? x < y? YES Display x 39
Controlling a Repetition Structure Adding an action within the repetition that changes the value of x YES x < y? Display x Add 1 to x 40
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 YES x < y? Display x Add 1 to x 41
while (1); Example int counter = 0; while (counter < 1000) ; int counter = 0; while (counter < 9) printf("%d\n", counter ++); int counter = 9; while (counter > 0) printf("%d\n", counter --); int counter = 0; while (counter < 9) { printf("%d\n", counter); counter++; } int counter = 0; while (counter < 9) { printf("%d\n", counter ++); } 42
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); Display x Add 1 to x x < y? YES 43
do-while do statement; while (loop_repetition_condition) OR do //Compound statement { statement1; statement2; // } while (loop_repetition_condition) 44
Draw a flowchart for the following problem: Example Read 5 integer and display the value of their summation. Input : 5 integer n1, n2, n3, n4, n5 Output: The summation of n1, n2,.., n5 Input example: 2 3 4 5 6 Output example: 20 45
Assume input example: 2 3 4 5 6 start Input n1 n1 2 This flowchart does not use loop, hence we need to use 6 different variables Input n2 Input n3 input n4 n2 n3 3 4 input n5 n4 5 sum n1+n2+n3+n4+n5 n5 6 output sum sum 20 end 46
Assume input example: 2 3 4 5 6 counter 1, sum 0 counter 12 34 56 sum 02 59 14 20 14 02 59 + 23 45 6 counter < 6 true input n sum sum + n counter++ output sum false 61 23 54 < < 6 6 false true n 23 45 6 The This Uses counter loop only is counter-controlled Increases 3 variables by 1 47
Decreasing Counter- Controlled Loop counter 5, sum 0 counter > 0 true false input x sum sum+ x counter-- output sum 48
For for (initial_value ; condition; update_counter) statement; OR // Compound statement for (initial_value ; condition; update_counter) { statement; statement; // } 49
counter 1, sum 0 counter < 6 true input n sum sum + n counter++ output sum false int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf( %d,&x); sum = sum + x; } printf( %d,sum); 50
??? Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); num _ 51
1 Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); num _ 52
Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 1 num _ 53
Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 1 num 1 _ 54
Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 2 num 1 _ 55
Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 2 num 1 _ 56
Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 2 num 1 2 _ 57
Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 3 num 1 2 _ 58
Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 3 num 1 2 _ 59
Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 3 num 1 2 3 _ 60
Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 4 num 1 2 3 _ 61
Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 4 num 1 2 3 _ 62
Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 4 num 1 2 3 have come to exit_ 63
* ** *** **** ***** ****** #include <stdio.h> int main() { int icounter = 0; int jcounter = 0; while (icounter < 7) { jcounter = 0; while (jcounter < icounter) { printf("*"); jcounter++; } printf("\n"); icounter++; } Example getch(); return 0; } 64