Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial #7 Flowcharts (reprise) Program Design. Introduction We mentioned it already, that if we thing of an analyst as being analogous to an architect,

Similar presentations


Presentation on theme: "Tutorial #7 Flowcharts (reprise) Program Design. Introduction We mentioned it already, that if we thing of an analyst as being analogous to an architect,"— Presentation transcript:

1 Tutorial #7 Flowcharts (reprise) Program Design

2 Introduction We mentioned it already, that if we thing of an analyst as being analogous to an architect, and a developer as being analogous to a builder, then the most important thing we can do as analysts is to explain our designs to the developers in a simple and clear way. How do architects do this?

3

4 Symbols

5

6 Terminal Input/Output Operation Process Decision Connector Module Symbols

7 Flowcharts So we start the program as: START

8 Flowcharts Our program will finish with the following: END

9 SEQUENCE

10 Flowcharts When we write programs, we assume that the computer executes the program starting at the beginning and working its way to the end. This is a basic assumption of all algorithm design.

11 Flowcharts When we write programs, we assume that the computer executes the program starting at the beginning and working its way to the end. This is a basic assumption of all algorithm design. We call this SEQUENCE.

12 START END Statement1 Statement2

13 SELECTION

14 Flowcharts What if we want to make a choice, for example, do we want to add sugar or not to the tea?

15 Flowcharts What if we want to make a choice, for example, do we want to add sugar or not to the tea? We call this SELECTION.

16 START END Is Sugar required? No Yes Add SugarDon’t Add Sugar

17 ITERATION

18 Flowcharts What if we need to tell the computer to keep doing something until some condition occurs?

19 Flowcharts What if we need to tell the computer to keep doing something until some condition occurs? Let’s say we wish to indicate that the you need to keep filling the kettle with water until it is full.

20 Flowcharts What if we need to tell the computer to keep doing something until some condition occurs? Let’s say we wish to indicate that the you need to keep filling the kettle with water until it is full. We need a loop, or ITERATION.

21 START END Kettle is not full? No Yes Keep Filling Kettle

22 EXAMPLES

23 Flowcharts So let’s say we want to express the following algorithm: – Read in a number and print it out.

24 START

25 Read in A

26 START Read in A Print A

27 START END Read in A Print A

28 Flowcharts So let’s say we want to express the following algorithm: – Read in a number and print it out double the number.

29 START

30 Read in A

31 START Read in A Print A*2

32 START END Read in A Print A*2

33 Or alternatively...

34 START

35 Read in A

36 START Read in A B = A*2

37 START Read in A B = A * 2 can be read as “B gets the value of A multiplied by 2” B = A*2

38 START Read in A Print B B = A*2

39 START END Read in A Print B B = A*2

40 Flowcharts So let’s say we want to express the following algorithm: – Read in a number, check if it is odd or even.

41 START

42 Read in A

43 START Does A/2 give a remainder? Read in A

44 START Does A/2 give a remainder? Read in A Yes Print “It’s Odd”

45 START Does A/2 give a remainder? No Read in A Yes Print “It’s Odd”Print “It’s Even”

46 START END Does A/2 give a remainder? No Read in A Yes Print “It’s Odd”Print “It’s Even”

47 Flowcharts So let’s say we want to express the following algorithm to print out the bigger of two numbers: – Read in two numbers, call them A and B. Is A is bigger than B, print out A, otherwise print out B.

48 START

49 Read in A and B

50 START A>B? Read in A and B

51 START A>B? Read in A and B Yes Print A

52 START A>B? No Read in A and B Yes Print APrint B

53 START END A>B? No Read in A and B Yes Print APrint B

54 Flowcharts So let’s say we want to express the following algorithm to print out the bigger of three numbers: – Read in three numbers, call them A, B and C. If A is bigger than B, then if A is bigger than C, print out A, otherwise print out C. If B is bigger than A, then if B is bigger than C, print out B, otherwise print out C.

55 START

56 Read in A, B and C

57 START A>B? Read in A, B and C

58 START A>B? Read in A, B and C Yes A>C?

59 START A>B? No Read in A, B and C Yes A>C?B>C?

60 START A>B? No Read in A, B and C Yes A>C?B>C? Print C No

61 START A>B? No Read in A, B and C Yes A>C?B>C? Print APrint C Yes No

62 START A>B? No Read in A, B and C Yes A>C?B>C? Print APrint CPrint B Yes No

63 START END A>B? No Read in A, B and C Yes A>C?B>C? Print APrint CPrint B Yes No

64 Flowcharts So let’s say we want to express the following algorithm: – Print out the numbers from 1 to 5

65 START

66 Print 1

67 START Print 1 Print 2

68 START Print 1 Print 2 Print 3

69 START Print 1 Print 2 Print 3 Print 4

70 START Print 1 Print 2 Print 3 Print 4 Print 5

71 START END Print 1 Print 2 Print 3 Print 4 Print 5

72 Or alternatively...

73 Flowcharts If I say A = A + 1, that means “A gets the value of whatever is in itself, plus 1” If A is 14 It becomes 15 A (new) 15 A (old) 14 + 1 A = A + 1

74 START

75 A = 1

76 START Is A==6? A = 1

77 START Is A==6? No A = 1 Print A

78 START Is A==6? No A = 1 Print A A = A + 1

79 START Is A==6? No A = 1 Print A A = A + 1

80 START END Is A==6? No A = 1 Yes Print A A = A + 1

81 Flowcharts So let’s say we want to express the following algorithm: – Add up the numbers 1 to 5

82 Flowcharts But first a few points; If I say T = 5, that means “T gets the value 5” T = 5

83 Flowcharts But first a few points; If I say T = 5, that means “T gets the value 5” T T = 5

84 Flowcharts But first a few points; If I say T = 5, that means “T gets the value 5” T 5 T = 5

85 Flowcharts If I say T = X, that means “T gets the value of whatever is in the variable X” T = X

86 Flowcharts If I say T = X, that means “T gets the value of whatever is in the variable X” So if X is 14, then T will get the value 14. T = X

87 Flowcharts If I say T = X, that means “T gets the value of whatever is in the variable X” So if X is 14, then T will get the value 14. T X 14 T = X

88 Flowcharts If I say T = X+1, that means “T gets the value of whatever is in the variable X plus one” T = X + 1

89 Flowcharts If I say T = X+1, that means “T gets the value of whatever is in the variable X plus one” So if X is 14, T becomes 15, and X stays as 14. T = X + 1

90 Flowcharts If I say T = X+1, that means “T gets the value of whatever is in the variable X plus one” So if X is 14, T becomes 15, and X stays as 14. T X 15 14+ 1 T = X + 1

91 Flowcharts If I say T = T + X, that means “T gets the value of whatever is in itself plus whatever is in the variable X” T = T + X

92 Flowcharts If I say T = T + X, that means “T gets the value of whatever is in itself plus whatever is in the variable X” If T is 14 and X is 9 T becomes 23 X stays at 9 T = T + X

93 Flowcharts If I say T = T + X, that means “T gets the value of whatever is in itself plus whatever is in the variable X” If T is 14 and X is 9 T becomes 23 X stays at 9 T (new) X 23 9 T (old) 14 T = T + X

94 Flowcharts So let’s say we want to express the following algorithm: – Add up the numbers 1 to 5

95 START

96 Total = 0

97 START A = 1 Total = 0

98 START Is A==6? A = 1 Total = 0

99 START Is A==6? No A = 1 Total = Total + A; Total = 0

100 START Is A==6? No A = 1 Total = Total + A; A = A + 1 Total = 0

101 START END Is A==6? No A = 1 Yes Total = Total + A; A = A + 1 Total = 0

102 Flowcharts So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.

103 Flowcharts So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number. – What’s a prime number?

104 Flowcharts So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number. – What’s a prime number? – A number that’s only divisible by itself and 1, e.g. 7.

105 Flowcharts So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number. – What’s a prime number? – A number that’s only divisible by itself and 1, e.g. 7. – Or to put it another way, every number other than itself and 1 gives a remainder, e.g. For 7, if 6, 5, 4, 3, and 2 give a remainder then 7 is prime.

106 Flowcharts So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number. – What’s a prime number? – A number that’s only divisible by itself and 1, e.g. 7. – Or to put it another way, every number other than itself and 1 gives a remainder, e.g. For 7, if 6, 5, 4, 3, and 2 give a remainder then 7 is prime. – So all we need to do is divide 7 by all numbers less than it but greater than one, and if any of them have no remainder, we know it’s not prime.

107 Flowcharts So, If the number is 7, as long as 6, 5, 4, 3, and 2 give a remainder, 7 is prime. If the number is 9, we know that 8, 7, 6, 5, and 4, all give remainders, but 3 does not give a remainder, it goes evenly into 9 so we can say 9 is not prime

108 Flowcharts So remember, – if the number is 7, as long as 6, 5, 4, 3, and 2 give a remainder, 7 is prime. So, in general, – if the number is A, as long as A-1, A-2, A-3, A- 4,... 2 give a remainder, A is prime.

109 START

110 Read in A

111 START Read in A B = A -1

112 START Read in A B = A -1 Is B = = 1?

113 START Read in A B = A -1 No Is B = = 1?

114 START Read in A B = A -1 No Is B = = 1? Does A/B give a remainder?

115 START Read in A B = A -1 No Is B = = 1? Yes Does A/B give a remainder?

116 START Read in A B = A -1 No B = B - 1 Is B = = 1? Yes Does A/B give a remainder?

117 START Read in A B = A -1 No B = B - 1 Is B = = 1? Yes Does A/B give a remainder?

118 START Read in A B = A -1 No B = B - 1 Is B = = 1? Yes Does A/B give a remainder? No Print “Not Prime”

119 START Yes Read in A Print “Prime” B = A -1 No B = B - 1 Is B = = 1? No Print “Not Prime” Yes Does A/B give a remainder?

120 START END Yes Read in A Print “Prime” B = A -1 No B = B - 1 Is B = = 1? No Print “Not Prime” Yes Does A/B give a remainder?


Download ppt "Tutorial #7 Flowcharts (reprise) Program Design. Introduction We mentioned it already, that if we thing of an analyst as being analogous to an architect,"

Similar presentations


Ads by Google