Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to develop a program?

Similar presentations


Presentation on theme: "How to develop a program?"— Presentation transcript:

1

2 How to develop a program?
Requirements Problem Analysis Design Designing algorithm Pseudo code Flow chart Implementation Implementing algorithm in c/c++ Validation Test Maintenance Refinement Software Development Life-Cycle

3 Requirements Discovery
Write a program to compute an employee's weekly pay? Problem Analysis 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

4 Problem vs. Solution A problem is something that causes trouble
The solution is how to solve or fixe the problem

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

6 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

7 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

8 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: Read Hours and Pay Rate weekly pay = Read Hours * Pay Rate

9 Flowchart 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 A flowchart is a diagram that depicts the flow of an algorithm pay-calculation program Each symbol represents a different type of operation

10 Terminals represented by rounded rectangles
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 represented by rounded rectangles indicate a starting or ending point START END Flow line Terminal

11 Input/Output Operations
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 indicate an input or output operation Display message “How many hours did you work?” Input/Output Operation Read Hours

12 Processes 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 indicates a process such as a mathematical computation or variable assignment Multiply Hours by Pay Rate. Store result in Gross Pay. Process

13 Execution Variable Contents: Hours: ? Pay Rate: ? Gross Pay: ?
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 How many hours did you work? Variable Contents: Hours: ? Pay Rate: ? Gross Pay: ?

14 Execution Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ?
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 Operation (User types 40) How many hours did you work? 40 Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ?

15 Execution Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ?
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 How much do you get paid per hour? Output Operation Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ?

16 Execution Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: ?
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 How many hours did you work? 20 Input Operation (User types 20) Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: ?

17 Execution Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800
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 How many hours did you work? 20 Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 Process (Gross Pay = Hours * Pay Rate)

18 Execution Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800
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 Your gross pay is 800 Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 Output Operation

19 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

20 Call calc_pay function
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

21 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

22 Sequence Structure a series of actions are performed in sequence
The pay-calculating example

23 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, { }

24 Decision Structure One of two possible actions is taken, depending on a condition Selection structures are used to choose among alternative courses of action YES NO YES NO x < y? Process B Process A

25 C programming language
Decision Structure The flowchart segment below shows how a decision structure is expressed in C as an if/else statement Flowchart C programming language YES NO x < y? Calculate a as x times 2. Calculate a as x plus y. if (x < y) a = x * 2; else a = x + y;

26 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" ); }

27 C programming language
Decision Structure The flowchart segment below shows a decision structure with only one action to perform Flowchart C programming language YES NO x < y? Calculate a as x times 2. if (x < y) a = x * 2;

28 Combining Structures YES NO if (x > min) { if (x < max)
Display “x is within limits.” Display “x is outside the limits.” YES NO x > min? x < max? if (x > min) { if (x < max) printf("x is within the limits"); else printf("x is outside the limits"); }

29 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?

30 Example if(x) if(x) if(x) if(y) { { printf("Yes"); if(y) if(y) else
printf("No"); if(x) { if(y) printf("Yes"); else printf("No"); } 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 < 0) sign = -1; else if (x == 0) sign = 0; else sign = 1;

31 Case Structure One of several possible actions is taken, depending on the contents of a variable

32 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 = 3, bonus is set to 400 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 is any other value, bonus is set to 800

33 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

34 switch switch (variable) { case const: statements...; default: }
switch (c = toupper(getch())) { case ‘R’: printf("Red"); break; case ‘G’: printf("Green"); default: printf("other"); } switch (variable) { case const: statements...; default: }

35 Example switch(betty) { case 1: printf("betty = 1\n"); case 2: case 3:
CASE betty? 1 2 3 Other betty = 1 switch(betty) { case 1: printf("betty = 1\n"); case 2: printf("betty=2\n"); break; case 3: printf("betty=3\n"); default: printf("Not sure\n"); } betty = 2 betty = 3 Not sure

36 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

37 C programming language
Repetition Structure The flowchart segment below shows a repetition structure expressed in C as a while loop Flowchart C programming language x < y? Add 1 to x YES while (x < y) x++;

38 While while (loop_repetition_condition) statement; OR
//Compound statement { statement1; statement2; // … }

39 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

40 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

41 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

42 Example int counter = 0; while (counter < 1000) ; while (1);
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 ++); }

43 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? C programming language do { printf(…); x++; } while (x < y);

44 do-while do statement; while (loop_repetition_condition) OR do //Compound statement { statement1; statement2; // … }

45 Example Draw a flowchart for the following problem:
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: Output example: 20

46 Assume input example: start Input n1 2 n1 Input n2 3 n2 This flowchart does not use loop, hence we need to use 6 different variables Input n3 4 n3 input n4 5 n4 input n5 sum ← n1+n2+n3+n4+n5 6 n5 output sum 20 sum end

47 This loop is Uses only The counter counter-controlled 3 variables
Assume input example: 3 2 1 4 6 5 counter counter ← 1, sum ← 0 2 20 9 14 5 sum 14 + 6 5 + 4 9 + 5 2 + 3 0 + 2 counter < 6 false 6 < 6 false 5 < 6 true 3 < 6 true 2 < 6 true 1 < 6 true 4 < 6 true true 4 6 3 2 5 input n n sum ← sum + n counter++ This loop is counter-controlled The counter Increases by 1 Uses only 3 variables output sum

48 Decreasing Counter-Controlled Loop
counter ← 5, sum ← 0 counter > 0 false true input x sum←sum+ x counter-- output sum C Programming Language

49 For for (initial_value ; condition; update_counter)
statement; OR // Compound statement for (initial_value ; condition; update_counter) { statement; // … }

50 int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(“%d”,&x);
counter ← 1, sum ← 0 int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(“%d”,&x); sum = sum + x; } counter < 6 false true input n sum ← sum + n counter++ printf(“%d”,sum); output sum 50

51 ??? num Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); _ 51

52 1 num Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); _ 52

53 for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num _ 53

54 for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 _ 54

55 for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);
2 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 _ 55

56 for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);
2 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 _ 56

57 for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);
2 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 _ 57

58 for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 _ 58

59 for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 _ 59

60 for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);
Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 3 _ 60

61 for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);
4 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 3 _ 61

62 for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);
4 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 3 _ 62

63 for (num = 1; num <= 3; num++ ) printf(“%d\t”, num);
4 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); printf(“have come to exit\n”); num 1 2 3 have come to exit_ 63

64 Example * ** *** **** ***** ****** #include <stdio.h> 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 "How to develop a program?"

Similar presentations


Ads by Google