flow charting, structured english and pseudocode damian gordon

111
Flow Charting, Structured English and PseudoCode Damian Gordon

Post on 19-Dec-2015

232 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Flow Charting, Structured English and PseudoCode Damian Gordon

Flow Charting, Structured English and PseudoCode

Damian Gordon

Page 2: Flow Charting, Structured English and PseudoCode Damian Gordon

We will recall...

Page 3: Flow Charting, Structured English and PseudoCode Damian Gordon

Flowcharts

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

Page 4: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Page 5: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

Page 6: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

Print A

Page 7: Flow Charting, Structured English and PseudoCode Damian Gordon

START

END

Read in A

Print A

Page 8: Flow Charting, Structured English and PseudoCode Damian Gordon

Structured English and Pseudocode

Structured English

PROGRAM PrintNumber: Read in a number and print

it out.END.

Pseudocode

PROGRAM PrintNumber: Read A; Print A;END.

Page 9: Flow Charting, Structured English and PseudoCode Damian Gordon

Flowcharts

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

Page 10: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Page 11: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

Page 12: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

Print A*2

Page 13: Flow Charting, Structured English and PseudoCode Damian Gordon

START

END

Read in A

Print A*2

Page 14: Flow Charting, Structured English and PseudoCode Damian Gordon

Structured English and Pseudocode

Structured English

PROGRAM DoubleNumber: Read in a number and print

double the number out.END.

Pseudocode

PROGRAM DoubleNumber: Read A; Print A*2;END.

Page 15: Flow Charting, Structured English and PseudoCode Damian Gordon

Or alternatively...

Page 16: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Page 17: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

Page 18: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

B = A*2

Page 19: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in AB = A * 2

can be read as

“B gets the value of A

multiplied by 2”

B = A*2

Page 20: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

Print B

B = A*2

Page 21: Flow Charting, Structured English and PseudoCode Damian Gordon

START

END

Read in A

Print B

B = A*2

Page 22: Flow Charting, Structured English and PseudoCode Damian Gordon

Structured English and Pseudocode

Structured English

PROGRAM DoubleNumber: Read in a number and print

double the number out.END.

Pseudocode

PROGRAM DoubleNumber: Read A; B = A*2; Print B;END.

Page 23: Flow Charting, Structured English and PseudoCode Damian Gordon

Flowcharts

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

Page 24: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Page 25: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

Page 26: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Does A/2 give a

remainder?

Read in A

Page 27: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Does A/2 give a

remainder?

Read in A

YesPrint “It’s Odd”

Page 28: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Does A/2 give a

remainder?

No

Read in A

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

Page 29: Flow Charting, Structured English and PseudoCode Damian Gordon

START

END

Does A/2 give a

remainder?

No

Read in A

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

Page 30: Flow Charting, Structured English and PseudoCode Damian Gordon

Structured English and Pseudocode

Structured English

PROGRAM OddOrEven: Read in a number. Divide it by two. If there is a remainder, the

number is odd, otherwise it’s even.

END.

Pseudocode

PROGRAM OddOrEven: Read A; IF A/2 gives a remainder THEN Print “It’s Odd”; ELSE Print “It’s Even”; ENDIF;END.

Page 31: Flow Charting, Structured English and PseudoCode Damian Gordon

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 32: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Page 33: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A and B

Page 34: Flow Charting, Structured English and PseudoCode Damian Gordon

START

A>B?

Read in A and B

Page 35: Flow Charting, Structured English and PseudoCode Damian Gordon

START

A>B?

Read in A and B

YesPrint A

Page 36: Flow Charting, Structured English and PseudoCode Damian Gordon

START

A>B?No

Read in A and B

YesPrint A Print B

Page 37: Flow Charting, Structured English and PseudoCode Damian Gordon

START

END

A>B?No

Read in A and B

YesPrint A Print B

Page 38: Flow Charting, Structured English and PseudoCode Damian Gordon

Structured English and Pseudocode

Structured English

PROGRAM BiggerOfTwo: Read in a number. Read in a second number. If the first number is bigger,

then print out that number, otherwise print out the other number.

END.

Pseudocode

PROGRAM BiggerOfTwo : Read A; IF (A>B) THEN Print A; ELSE Print B; ENDIF;END.

Page 39: Flow Charting, Structured English and PseudoCode Damian Gordon

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 40: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Page 41: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A, B and C

Page 42: Flow Charting, Structured English and PseudoCode Damian Gordon

START

A>B?

Read in A, B and C

Page 43: Flow Charting, Structured English and PseudoCode Damian Gordon

START

A>B?

Read in A, B and C

YesA>C?

Page 44: Flow Charting, Structured English and PseudoCode Damian Gordon

START

A>B?No

Read in A, B and C

YesA>C? B>C?

Page 45: Flow Charting, Structured English and PseudoCode Damian Gordon

START

A>B?No

Read in A, B and C

YesA>C? B>C?

Print C

NoNo

Page 46: Flow Charting, Structured English and PseudoCode Damian Gordon

START

A>B?No

Read in A, B and C

YesA>C? B>C?

Print A Print C

Yes

NoNo

Page 47: Flow Charting, Structured English and PseudoCode Damian Gordon

START

A>B?No

Read in A, B and C

YesA>C? B>C?

Print A Print C Print B

Yes Yes

NoNo

Page 48: Flow Charting, Structured English and PseudoCode Damian Gordon

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 49: Flow Charting, Structured English and PseudoCode Damian Gordon

Structured English and Pseudocode

Structured English

PROGRAM BiggerOfThree: Read in a number. Read in a second number. Read in a third number. If the first number is bigger than the

second, then if the first number is bigger than the third, then print out the first number, otherwise print out the third number number.

If the first number is smaller than the second, then if the second number is bigger than the third, then print out the second number, otherwise print out the third number.

END.

PseudocodePROGRAM BiggerOfThree: Read A; Read B; Read C; IF A>B THEN IF A>C THEN Print A ELSE Print C END IF; ELSE IF B>C THEN Print B ELSE Print C END IF; END IF;END.

Page 50: Flow Charting, Structured English and PseudoCode Damian Gordon

Flowcharts

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

Page 51: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Page 52: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Print 1

Page 53: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Print 1

Print 2

Page 54: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Print 1

Print 2

Print 3

Page 55: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Print 1

Print 2

Print 3

Print 4

Page 56: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Print 1

Print 2

Print 3

Print 4

Print 5

Page 57: Flow Charting, Structured English and PseudoCode Damian Gordon

START

END

Print 1

Print 2

Print 3

Print 4

Print 5

Page 58: Flow Charting, Structured English and PseudoCode Damian Gordon

Structured English and Pseudocode

Structured English

PROGRAM Print1to5: Print out 1. Print out 2. Print out 3. Print out 4. Print out 5.END.

Pseudocode

PROGRAM Print1to5: Print 1; Print 2; Print 3; Print 4; Print 5;END.

Page 59: Flow Charting, Structured English and PseudoCode Damian Gordon

Or alternatively...

Page 60: Flow Charting, Structured English and PseudoCode Damian Gordon

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 61: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Page 62: Flow Charting, Structured English and PseudoCode Damian Gordon

START

A = 1

Page 63: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Is A==6?

A = 1

Page 64: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Is A==6?No

A = 1

Print A

Page 65: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Is A==6?No

A = 1

Print A

A = A + 1

Page 66: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Is A==6?No

A = 1

Print A

A = A + 1

Page 67: Flow Charting, Structured English and PseudoCode Damian Gordon

START

END

Is A==6?No

A = 1

Yes

Print A

A = A + 1

Page 68: Flow Charting, Structured English and PseudoCode Damian Gordon

Structured English and Pseudocode

Structured English

PROGRAM Print1to5: Keep printing out numbers

until you reach 5. END.

Pseudocode

PROGRAM Print1to5: A = 1; WHILE (A != 6) DO Print A; A = A + 1; ENDWHILE;END.

Page 69: Flow Charting, Structured English and PseudoCode Damian Gordon

Flowcharts

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

Page 70: Flow Charting, Structured English and PseudoCode Damian Gordon

Flowcharts

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

5”

T = 5

Page 71: Flow Charting, Structured English and PseudoCode Damian Gordon

Flowcharts

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

5”

T

T = 5

Page 72: Flow Charting, Structured English and PseudoCode Damian Gordon

Flowcharts

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

5”

T

5

T = 5

Page 73: Flow Charting, Structured English and PseudoCode Damian Gordon

Flowcharts

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

T = X

Page 74: Flow Charting, Structured English and PseudoCode Damian Gordon

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 75: Flow Charting, Structured English and PseudoCode Damian Gordon

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 76: Flow Charting, Structured English and PseudoCode Damian Gordon

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 77: Flow Charting, Structured English and PseudoCode Damian Gordon

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 78: Flow Charting, Structured English and PseudoCode Damian Gordon

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 79: Flow Charting, Structured English and PseudoCode Damian Gordon

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 80: Flow Charting, Structured English and PseudoCode Damian Gordon

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 81: Flow Charting, Structured English and PseudoCode Damian Gordon

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 82: Flow Charting, Structured English and PseudoCode Damian Gordon

Flowcharts

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

Page 83: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Page 84: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Total = 0

Page 85: Flow Charting, Structured English and PseudoCode Damian Gordon

START

A = 1

Total = 0

Page 86: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Is A==6?

A = 1

Total = 0

Page 87: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Is A==6?No

A = 1

Total = Total + A;

Total = 0

Page 88: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Is A==6?No

A = 1

Total = Total + A;

A = A + 1

Total = 0

Page 89: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Is A==6?No

A = 1

Yes

Total = Total + A;

A = A + 1

Total = 0

Print Total

Page 90: Flow Charting, Structured English and PseudoCode Damian Gordon

START

END

Is A==6?No

A = 1

Yes

Total = Total + A;

A = A + 1

Total = 0

Print Total

Page 91: Flow Charting, Structured English and PseudoCode Damian Gordon

Structured English and Pseudocode

Structured English

PROGRAM PrintSum1to5: Keep adding the numbers

from 1 to 5.END.

Pseudocode

PROGRAM PrintSum1to5: Total = 0; A = 1; WHILE (A != 6) DO Total = Total + A; A = A + 1; ENDWHILE; Print Total;END.

Page 92: Flow Charting, Structured English and PseudoCode Damian Gordon

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 93: Flow Charting, Structured English and PseudoCode Damian Gordon

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 94: Flow Charting, Structured English and PseudoCode Damian Gordon

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 95: Flow Charting, Structured English and PseudoCode Damian Gordon

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 96: Flow Charting, Structured English and PseudoCode Damian Gordon

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 97: Flow Charting, Structured English and PseudoCode Damian Gordon

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 98: Flow Charting, Structured English and PseudoCode Damian Gordon

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 99: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Page 100: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

Page 101: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

B = A -1

Page 102: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

B = A -1

Is B = = 1?

Page 103: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

B = A -1

NoIs B = = 1?

Page 104: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

B = A -1

NoIs B = = 1?

Does A/B give a

remainder?

Page 105: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

B = A -1

NoIs B = = 1?

YesDoes

A/B give a remainder?

Page 106: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

B = A -1

NoB = B - 1 Is B = = 1?

YesDoes

A/B give a remainder?

Page 107: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

B = A -1

NoB = B - 1 Is B = = 1?

YesDoes

A/B give a remainder?

Page 108: Flow Charting, Structured English and PseudoCode Damian Gordon

START

Read in A

B = A -1

NoB = B - 1 Is B = = 1?

YesDoes

A/B give a remainder?

No

Print “Not Prime”

Page 109: Flow Charting, Structured English and PseudoCode Damian Gordon

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 110: Flow Charting, Structured English and PseudoCode Damian Gordon

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?

Page 111: Flow Charting, Structured English and PseudoCode Damian Gordon

Structured English and PseudocodeStructured English

PROGRAM Prime: Read in a number Divide that number by

every number less than it, but greater than one.

If any of them divide in evenly, then it’s not prime, otherwise it is prime.

END.

PseudocodePROGRAM Prime: Read A; B = A - 1; IsPrime=True; WHILE (B != 1) DO IF (A/B gives no remainder) THEN IsPrime= False; ENDIF; B = B – 1; ENDWHILE; IF (IsPrime == true) THEN Print “Prime”; ELSE Print “Not Prime”; ENDIF;END.