Download presentation
Presentation is loading. Please wait.
1
Flow Charting, Structured English and PseudoCode
Damian Gordon
2
We will recall...
3
Flowcharts So let’s say we want to express the following algorithm:
Read in a number and print it out.
4
START
5
START Read in A
6
START Read in A Print A
7
START Read in A Print A END
8
Structured English and Pseudocode
PROGRAM PrintNumber: Read in a number and print it out. END. PROGRAM PrintNumber: Read A; Print A; END.
9
Flowcharts So let’s say we want to express the following algorithm:
Read in a number and print it out double the number.
10
START
11
START Read in A
12
START Read in A Print A*2
13
START Read in A Print A*2 END
14
Structured English and Pseudocode
PROGRAM DoubleNumber: Read in a number and print double the number out. END. PROGRAM DoubleNumber: Read A; Print A*2; END.
15
Or alternatively...
16
START
17
START Read in A
18
START Read in A B = A*2
19
“B gets the value of A multiplied by 2”
START B = A * 2 can be read as “B gets the value of A multiplied by 2” Read in A B = A*2
20
START Read in A B = A*2 Print B
21
START Read in A B = A*2 Print B END
22
Structured English and Pseudocode
PROGRAM DoubleNumber: Read in a number and print double the number out. END. PROGRAM DoubleNumber: Read A; B = A*2; Print B; END.
23
Flowcharts So let’s say we want to express the following algorithm:
Read in a number, check if it is odd or even.
24
START
25
START Read in A
26
Does A/2 give a remainder?
START Read in A Does A/2 give a remainder?
27
Does A/2 give a remainder?
START Read in A Does A/2 give a remainder? Print “It’s Odd” Yes
28
Does A/2 give a remainder?
START Read in A Does A/2 give a remainder? Print “It’s Odd” Print “It’s Even” Yes No
29
Does A/2 give a remainder?
START Read in A Does A/2 give a remainder? Print “It’s Odd” Yes No Print “It’s Even” END
30
Structured English and Pseudocode
PROGRAM OddOrEven: Read in a number. Divide it by two. If there is a remainder, the number is odd, otherwise it’s even. END. PROGRAM OddOrEven: Read A; IF A/2 gives a remainder THEN Print “It’s Odd”; ELSE Print “It’s Even”; ENDIF; END.
31
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.
32
START
33
START Read in A and B
34
START Read in A and B A>B?
35
START Read in A and B A>B? Print A Yes
36
START Read in A and B A>B? Print A Print B Yes No
37
START Read in A and B A>B? Print A Yes No Print B END
38
Structured English and Pseudocode
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. PROGRAM BiggerOfTwo : Read A; IF (A>B) THEN Print A; ELSE Print B; ENDIF; END.
39
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.
40
START
41
START Read in A, B and C
42
START Read in A, B and C A>B?
43
START Read in A, B and C A>C? A>B? Yes
44
START Read in A, B and C A>C? A>B? B>C? Yes No
45
START Read in A, B and C A>C? A>B? B>C? Yes No No No Print C
46
A>C? A>B? B>C? START Read in A, B and C Yes Yes No No No
Print A Print C
47
A>C? A>B? B>C? START Read in A, B and C Yes Yes No Yes No No
Print A Print C Print B
48
A>C? A>B? B>C? START Read in A, B and C Yes Yes No Yes No No
Print A Print C Print B END
49
Structured English and Pseudocode
PROGRAM 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 END. 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.
50
Flowcharts So let’s say we want to express the following algorithm:
Print out the numbers from 1 to 5
51
START
52
START Print 1
53
START Print 1 Print 2
54
START Print 1 Print 2 Print 3
55
START Print 1 Print 2 Print 3 Print 4
56
START Print 1 Print 2 Print 3 Print 4 Print 5
57
START Print 1 Print 2 Print 3 Print 4 Print 5 END
58
Structured English and Pseudocode
PROGRAM Print1to5: Print out 1. Print out 2. Print out 3. Print out 4. Print out 5. END. PROGRAM Print1to5: Print 1; Print 2; Print 3; Print 4; Print 5; END.
59
Or alternatively...
60
Flowcharts 14 + 1 A(old) 15 A(new) A = A + 1
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 14 + 1 A(old) 15 A(new)
61
START
62
START A = 1
63
START A = 1 Is A==6?
64
START A = 1 Is A==6? No Print A
65
START A = 1 A = A + 1 Is A==6? No Print A
66
START A = 1 A = A + 1 Is A==6? No Print A
67
START A = 1 A = A + 1 Is A==6? No Print A Yes END
68
Structured English and Pseudocode
PROGRAM Print1to5: Keep printing out numbers until you reach 5. END. PROGRAM Print1to5: A = 1; WHILE (A != 6) DO Print A; A = A + 1; ENDWHILE; END.
69
Flowcharts So let’s say we want to express the following algorithm:
Add up the numbers 1 to 5
70
Flowcharts T = 5 But first a few points;
If I say T = 5, that means “T gets the value 5”
71
Flowcharts T T = 5 But first a few points;
If I say T = 5, that means “T gets the value 5” T
72
Flowcharts 5 T T = 5 But first a few points;
If I say T = 5, that means “T gets the value 5” 5 T
73
T = X Flowcharts If I say T = X, that means “T gets the value of whatever is in the variable X”
74
T = X 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.
75
T = X 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. 14 X 14 T
76
T = X + 1 Flowcharts If I say T = X+1, that means “T gets the value of whatever is in the variable X plus one”
77
T = X + 1 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.
78
T = X + 1 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. + 1 14 X 15 T
79
T = T + X 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”
80
T = T + X 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
81
Flowcharts 14 9 T(old) X 23 T(new) T = T + X
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 14 9 T(old) X 23 T(new)
82
Flowcharts So let’s say we want to express the following algorithm:
Add up the numbers 1 to 5 and print out the result
83
START
84
START Total = 0
85
START Total = 0 A = 1
86
START Total = 0 A = 1 Is A==6?
87
START Total = 0 A = 1 Is A==6? No Total = Total + A;
88
START Total = 0 A = 1 A = A + 1 Is A==6? No Total = Total + A;
89
Is A==6? START Total = 0 A = 1 A = A + 1 No Total = Total + A; Yes
Print Total
90
Is A==6? START Total = 0 A = 1 A = A + 1 No Total = Total + A; Yes
Print Total END
91
Structured English and Pseudocode
PROGRAM PrintSum1to5: Keep adding the numbers from 1 to 5. END. PROGRAM PrintSum1to5: Total = 0; A = 1; WHILE (A != 6) DO Total = Total + A; A = A + 1; ENDWHILE; Print Total; END.
92
Flowcharts So let’s say we want to express the following algorithm:
Read in a number and check if it’s a prime number.
93
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?
94
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.
95
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.
96
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.
97
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
98
Flowcharts So remember, So, in general,
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, give a remainder, A is prime.
99
START
100
START Read in A
101
START Read in A B = A -1
102
START Read in A B = A -1 Is B = = 1?
103
START Read in A B = A -1 Is B = = 1? No
104
START Read in A B = A -1 Is B = = 1? No Does A/B give a remainder?
105
START Read in A B = A -1 Is B = = 1? No Does A/B give a remainder? Yes
106
START Read in A B = A -1 No B = B - 1 Yes Does A/B give a remainder?
Is B = = 1? B = B - 1 No Does A/B give a remainder? Yes
107
START Read in A B = A -1 No B = B - 1 Yes Does A/B give a remainder?
Is B = = 1? B = B - 1 No Does A/B give a remainder? Yes
108
START Read in A B = A -1 No B = B - 1 Yes Does A/B give a remainder?
Is B = = 1? B = B - 1 No Does A/B give a remainder? Yes No Print “Not Prime”
109
START Read in A B = A -1 B = B - 1 No Yes Print “Prime” Does
Is B = = 1? B = B - 1 No Yes Print “Prime” Does A/B give a remainder? Yes No Print “Not Prime”
110
START Read in A B = A -1 B = B - 1 No Yes Print “Prime” Does
Is B = = 1? B = B - 1 No Yes Print “Prime” Does A/B give a remainder? Yes No Print “Not Prime” END
111
Structured English and Pseudocode
PROGRAM 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”; END. 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.