Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 5 Structured Program."— Presentation transcript:

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

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

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

4 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 4 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

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

6 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 6 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

7 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 7 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

8 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 8 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

9 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 9 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

10 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 10 Flowchart A flowchart is a diagram that depicts the flow of an algorithm – pay-calculation program Each symbol represents a different type of operation 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

11 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 11 Terminals represented by rounded rectangles indicate a starting or ending point STARTEND 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 Terminal Flow line

12 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 12 Input/Output Operations indicate an input or output operation Display message “How many hours did you work?” Read Hours 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 Input/Output Operation

13 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 13 Processes indicates a process such as a mathematical computation or variable assignment 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 Multiply Hours by Pay Rate. Store result in Gross Pay. Process

14 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 14 Execution 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 Output Operation Variable Contents: Hours: ? Pay Rate: ? Gross Pay: ? How many hours did you work?

15 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 15 Execution 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 Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ? How many hours did you work? 40 Input Operation (User types 40)

16 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 16 Execution 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 Output Operation Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ? How much do you get paid per hour?

17 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 17 Execution 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 Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: ? How many hours did you work? 20 Input Operation (User types 20)

18 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 18 Execution 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 Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 How many hours did you work? 20 Process (Gross Pay = Hours * Pay Rate)

19 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 19 Execution 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 Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 Your gross pay is 800 Output Operation

20 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 20 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 A START END

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

22 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 22 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

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

24 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 24 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, { }

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

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

27 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 27 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" ); }

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

29 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 29 Combining Structures Display “x is within limits.” Display “x is outside the limits.” YESNO x > min? x < max? YES NO Display “x is outside the limits.” 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");

30 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 30 Example int k = 1, m = 4; if (k < 2 || m == 3) { m = 2 + k, printf("%d", m); } else { k = 1, printf("%d", k); } 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?

31 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 31 Example if(x) if(y) printf("Yes"); else printf("No"); if(x) { if(y) printf("Yes"); else printf("No"); } 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 < 0.25) count1++; else if (x >= 0.25 && x < 0.5) count2++; else if (x >= 0.5 && x < 0.75) count3++; else count4++;

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

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

34 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 34 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

35 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 35 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"); }

36 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 36 Example 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"); } CASE betty? 1 2 3 Other betty = 1 betty = 2 betty = 3 Not sure

37 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 37 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? Process A YES

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

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

40 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 40 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? Display x YES

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

42 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 42 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? Display x Add 1 to x YES

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

44 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 44 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 Display x Add 1 to x YES x < y? do { printf(…); x++; } while (x < y); C programming language

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

46 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 46 Example 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 Can you identify the input and output? Draw a flowchart for the following problem: Read 5 integer and display the value of their summation.

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

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

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

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

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

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

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

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

55 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 55 num 1 1_1_ Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”);

56 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 56 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 2 1_1_

57 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 57 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 2 1_1_

58 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 58 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 2 12_12_

59 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 59 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 3 12_12_

60 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 60 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 3 12_12_

61 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 61 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 3 123_123_

62 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 62 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 4 123_123_

63 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 63 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 4 123_123_

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

65 Structured Program Development – Lecture 5 Sharif University of Technology Department of Computer Engineering 65 Example * ** *** **** ***** ****** #include int main() { int iCounter = 0; int jCounter = 0; while (iCounter < 7) { jCounter = 0; while (jCounter < iCounter) { printf("*"); jCounter++; } printf("\n"); iCounter++; } getch(); return 0; }


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

Similar presentations


Ads by Google