Tutorial #7 Flowcharts (reprise)

120
Tutorial #7 Flowcharts (reprise) Program Design

description

Tutorial #7 Flowcharts (reprise). Program Design. Introduction. - PowerPoint PPT Presentation

Transcript of Tutorial #7 Flowcharts (reprise)

Page 1: Tutorial #7 Flowcharts (reprise)

Tutorial #7Flowcharts (reprise)

Program Design

Page 2: Tutorial #7 Flowcharts (reprise)

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?

Page 3: Tutorial #7 Flowcharts (reprise)
Page 4: Tutorial #7 Flowcharts (reprise)

Symbols

Page 5: Tutorial #7 Flowcharts (reprise)

Symbols

Page 6: Tutorial #7 Flowcharts (reprise)

Terminal

Input/Output Operation

Process

Decision

Connector

Module

Symbols

Page 7: Tutorial #7 Flowcharts (reprise)

Flowcharts

• So we start the program as:

START

Page 8: Tutorial #7 Flowcharts (reprise)

Flowcharts

• Our program will finish with the following:

END

Page 9: Tutorial #7 Flowcharts (reprise)

SEQUENCE

Page 10: Tutorial #7 Flowcharts (reprise)

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.

Page 11: Tutorial #7 Flowcharts (reprise)

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.

Page 12: Tutorial #7 Flowcharts (reprise)

START

END

Statement1

Statement2

Page 13: Tutorial #7 Flowcharts (reprise)

SELECTION

Page 14: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

Page 15: Tutorial #7 Flowcharts (reprise)

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.

Page 16: Tutorial #7 Flowcharts (reprise)

START

END

Is Sugar required?

NoYesAdd Sugar Don’t Add Sugar

Page 17: Tutorial #7 Flowcharts (reprise)

ITERATION

Page 18: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

Page 19: Tutorial #7 Flowcharts (reprise)

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.

Page 20: Tutorial #7 Flowcharts (reprise)

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.

Page 21: Tutorial #7 Flowcharts (reprise)

START

END

Kettle is not full?

No

Yes

Keep Filling Kettle

Page 22: Tutorial #7 Flowcharts (reprise)

EXAMPLES

Page 23: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

Page 24: Tutorial #7 Flowcharts (reprise)

START

Page 25: Tutorial #7 Flowcharts (reprise)

START

Read in A

Page 26: Tutorial #7 Flowcharts (reprise)

START

Read in A

Print A

Page 27: Tutorial #7 Flowcharts (reprise)

START

END

Read in A

Print A

Page 28: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

Page 29: Tutorial #7 Flowcharts (reprise)

START

Page 30: Tutorial #7 Flowcharts (reprise)

START

Read in A

Page 31: Tutorial #7 Flowcharts (reprise)

START

Read in A

Print A*2

Page 32: Tutorial #7 Flowcharts (reprise)

START

END

Read in A

Print A*2

Page 33: Tutorial #7 Flowcharts (reprise)

Or alternatively...

Page 34: Tutorial #7 Flowcharts (reprise)

START

Page 35: Tutorial #7 Flowcharts (reprise)

START

Read in A

Page 36: Tutorial #7 Flowcharts (reprise)

START

Read in A

B = A*2

Page 37: Tutorial #7 Flowcharts (reprise)

START

Read in AB = A * 2

can be read as

“B gets the value of A

multiplied by 2”

B = A*2

Page 38: Tutorial #7 Flowcharts (reprise)

START

Read in A

Print B

B = A*2

Page 39: Tutorial #7 Flowcharts (reprise)

START

END

Read in A

Print B

B = A*2

Page 40: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

Page 41: Tutorial #7 Flowcharts (reprise)

START

Page 42: Tutorial #7 Flowcharts (reprise)

START

Read in A

Page 43: Tutorial #7 Flowcharts (reprise)

START

Does A/2 give a

remainder?

Read in A

Page 44: Tutorial #7 Flowcharts (reprise)

START

Does A/2 give a

remainder?

Read in A

YesPrint “It’s Odd”

Page 45: Tutorial #7 Flowcharts (reprise)

START

Does A/2 give a

remainder?

No

Read in A

YesPrint “It’s Odd” Print “It’s Even”

Page 46: Tutorial #7 Flowcharts (reprise)

START

END

Does A/2 give a

remainder?

No

Read in A

YesPrint “It’s Odd” Print “It’s Even”

Page 47: Tutorial #7 Flowcharts (reprise)

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.

Page 48: Tutorial #7 Flowcharts (reprise)

START

Page 49: Tutorial #7 Flowcharts (reprise)

START

Read in A and B

Page 50: Tutorial #7 Flowcharts (reprise)

START

A>B?

Read in A and B

Page 51: Tutorial #7 Flowcharts (reprise)

START

A>B?

Read in A and B

YesPrint A

Page 52: Tutorial #7 Flowcharts (reprise)

START

A>B?No

Read in A and B

YesPrint A Print B

Page 53: Tutorial #7 Flowcharts (reprise)

START

END

A>B?No

Read in A and B

YesPrint A Print B

Page 54: Tutorial #7 Flowcharts (reprise)

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.

Page 55: Tutorial #7 Flowcharts (reprise)

START

Page 56: Tutorial #7 Flowcharts (reprise)

START

Read in A, B and C

Page 57: Tutorial #7 Flowcharts (reprise)

START

A>B?

Read in A, B and C

Page 58: Tutorial #7 Flowcharts (reprise)

START

A>B?

Read in A, B and C

YesA>C?

Page 59: Tutorial #7 Flowcharts (reprise)

START

A>B?No

Read in A, B and C

YesA>C? B>C?

Page 60: Tutorial #7 Flowcharts (reprise)

START

A>B?No

Read in A, B and C

YesA>C? B>C?

Print C

NoNo

Page 61: Tutorial #7 Flowcharts (reprise)

START

A>B?No

Read in A, B and C

YesA>C? B>C?

Print A Print C

Yes

NoNo

Page 62: Tutorial #7 Flowcharts (reprise)

START

A>B?No

Read in A, B and C

YesA>C? B>C?

Print A Print C Print B

Yes Yes

NoNo

Page 63: Tutorial #7 Flowcharts (reprise)

START

END

A>B?

No

Read in A, B and C

YesA>C? B>C?

Print A Print C Print B

Yes Yes

No

No

Page 64: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

Page 65: Tutorial #7 Flowcharts (reprise)

START

Page 66: Tutorial #7 Flowcharts (reprise)

START

Print 1

Page 67: Tutorial #7 Flowcharts (reprise)

START

Print 1

Print 2

Page 68: Tutorial #7 Flowcharts (reprise)

START

Print 1

Print 2

Print 3

Page 69: Tutorial #7 Flowcharts (reprise)

START

Print 1

Print 2

Print 3

Print 4

Page 70: Tutorial #7 Flowcharts (reprise)

START

Print 1

Print 2

Print 3

Print 4

Print 5

Page 71: Tutorial #7 Flowcharts (reprise)

START

END

Print 1

Print 2

Print 3

Print 4

Print 5

Page 72: Tutorial #7 Flowcharts (reprise)

Or alternatively...

Page 73: Tutorial #7 Flowcharts (reprise)

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)

15A(old)

14 + 1

A = A + 1

Page 74: Tutorial #7 Flowcharts (reprise)

START

Page 75: Tutorial #7 Flowcharts (reprise)

START

A = 1

Page 76: Tutorial #7 Flowcharts (reprise)

START

Is A==6?

A = 1

Page 77: Tutorial #7 Flowcharts (reprise)

START

Is A==6?No

A = 1

Print A

Page 78: Tutorial #7 Flowcharts (reprise)

START

Is A==6?No

A = 1

Print A

A = A + 1

Page 79: Tutorial #7 Flowcharts (reprise)

START

Is A==6?No

A = 1

Print A

A = A + 1

Page 80: Tutorial #7 Flowcharts (reprise)

START

END

Is A==6?No

A = 1

Yes

Print A

A = A + 1

Page 81: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

Page 82: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

5”

T = 5

Page 83: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

5”

T

T = 5

Page 84: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

5”

T

5

T = 5

Page 85: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

T = X

Page 86: Tutorial #7 Flowcharts (reprise)

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

Page 87: Tutorial #7 Flowcharts (reprise)

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

X14

14

T = X

Page 88: Tutorial #7 Flowcharts (reprise)

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

Page 89: Tutorial #7 Flowcharts (reprise)

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

Page 90: Tutorial #7 Flowcharts (reprise)

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

X15

14+ 1

T = X + 1

Page 91: Tutorial #7 Flowcharts (reprise)

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

Page 92: Tutorial #7 Flowcharts (reprise)

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

Page 93: Tutorial #7 Flowcharts (reprise)

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)

X23

9T(old)

14

T = T + X

Page 94: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

Page 95: Tutorial #7 Flowcharts (reprise)

START

Page 96: Tutorial #7 Flowcharts (reprise)

START

Total = 0

Page 97: Tutorial #7 Flowcharts (reprise)

START

A = 1

Total = 0

Page 98: Tutorial #7 Flowcharts (reprise)

START

Is A==6?

A = 1

Total = 0

Page 99: Tutorial #7 Flowcharts (reprise)

START

Is A==6?No

A = 1

Total = Total + A;

Total = 0

Page 100: Tutorial #7 Flowcharts (reprise)

START

Is A==6?No

A = 1

Total = Total + A;

A = A + 1

Total = 0

Page 101: Tutorial #7 Flowcharts (reprise)

START

END

Is A==6?No

A = 1

Yes

Total = Total + A;

A = A + 1

Total = 0

Page 102: Tutorial #7 Flowcharts (reprise)

Flowcharts

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

Page 103: Tutorial #7 Flowcharts (reprise)

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?

Page 104: Tutorial #7 Flowcharts (reprise)

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.

Page 105: Tutorial #7 Flowcharts (reprise)

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.

Page 106: Tutorial #7 Flowcharts (reprise)

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.

Page 107: Tutorial #7 Flowcharts (reprise)

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

Page 108: Tutorial #7 Flowcharts (reprise)

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.

Page 109: Tutorial #7 Flowcharts (reprise)

START

Page 110: Tutorial #7 Flowcharts (reprise)

START

Read in A

Page 111: Tutorial #7 Flowcharts (reprise)

START

Read in A

B = A -1

Page 112: Tutorial #7 Flowcharts (reprise)

START

Read in A

B = A -1

Is B = = 1?

Page 113: Tutorial #7 Flowcharts (reprise)

START

Read in A

B = A -1

NoIs B = = 1?

Page 114: Tutorial #7 Flowcharts (reprise)

START

Read in A

B = A -1

NoIs B = = 1?

Does A/B give a

remainder?

Page 115: Tutorial #7 Flowcharts (reprise)

START

Read in A

B = A -1

NoIs B = = 1?

YesDoes

A/B give a remainder?

Page 116: Tutorial #7 Flowcharts (reprise)

START

Read in A

B = A -1

NoB = B - 1 Is B = = 1?

YesDoes

A/B give a remainder?

Page 117: Tutorial #7 Flowcharts (reprise)

START

Read in A

B = A -1

NoB = B - 1 Is B = = 1?

YesDoes

A/B give a remainder?

Page 118: Tutorial #7 Flowcharts (reprise)

START

Read in A

B = A -1

NoB = B - 1 Is B = = 1?

YesDoes

A/B give a remainder?

No

Print “Not Prime”

Page 119: Tutorial #7 Flowcharts (reprise)

START

Yes

Read in A

Print “Prime”

B = A -1

NoB = B - 1 Is B = = 1?

No

Print “Not Prime”

YesDoes

A/B give a remainder?

Page 120: Tutorial #7 Flowcharts (reprise)

START

END

Yes

Read in A

Print “Prime”

B = A -1

NoB = B - 1 Is B = = 1?

No

Print “Not Prime”

YesDoes

A/B give a remainder?