It Cambridge Textbook
It Cambridge Textbook
rs
ve
y
op
ni
U
C
ge
w
ie
id
ev
br
am
-R
-C
s
es
Chapter 4
y
Pr
op
Algorithms ity
C
rs
w
ie
ve
y
ev
op
ni
R
C
and flowcharts
ge
w
ie
id
ev
br
am
-R
-C
s
es
y
Pr
op
ity
C
rs
w
ie
ve
y
LEARNING INTENTIONS
ev
op
ni
R
-R
s
es
y
Pr
op
ity
C
rs
w
ie
ve
y
ev
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
w
BEFORE YOU START
ie
id
ev
br
• Do you have experience of following instructions and creating sequences of instructions for someone to
follow?
am
-R
• Do you have any experience of block programming in languages such as Scratch, or of controlling a
-C
s
es
y
Pr
op
Introduction
ity
C
Start
rs
w
ve
to solve problems in a form that a computer could
y
ev
op
ni
C
computer’s processor can then follow one at a time.
ge
w
ie
id
Finish
4.1 Algorithms
ev
br
am
-R
An algorithm is a series of steps that are followed. Figure 4.1: Maze 1, start.
You follow lots of algorithms without even realising
-C
it, for example, following a recipe or following a list of To get to ‘Finish’ you would need to follow these steps:
es
design them using a flowchart and/or using pseudocode. Step 2: RT90 Step 6: RT90
ity
C
ve
y
ev
op
ni
flowchart: a set of symbols put together with You are on Start, facing to the right.
ge
an algorithm Finish
am
-R
-C
ity
C
ve
op
ni
to the right.
w
g
ie
id
ev
br
am
-R
54
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
w
CONTINUED
ie
id
You are facing to the right so need to count how many You need to face to the right again, so:
ev
br
grey spaces there are until you need to turn. There are
am RT90
-R
4 spaces, so the command is:
To the next turn is 3 spaces:
FD4
-C
s
FD3
es
y
Finish
Pr
op
Finish
ity
C
rs
w
ie
ve
y
ev
op
ni
R
C
ge
Start
w
Start
ie
id
ev
br
-R
You need to turn so you are facing up, this means
turning to the left, 90 degrees. The command is You need to face up, so you are turning left again:
-C
LT90 LT90
es
y
How many spaces forward? 2 Now you need to move forward 5 spaces:
Pr
op
FD2 FD5
ity
C
rs
w
Finish Finish
ie
ve
y
ev
op
ni
R
C
ge
w
ie
id
ev
br
am
-R
Start Start
-C
Figure 4.4: Maze 2, FD4, LT90, FD2. Figure 4.6: Maze 2, FD4, LT90, FD2, RT90, FD3, LT90, FD5.
s
es
y
Pr
op
ity
C
rs
w
ie
ve
y
ev
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
55
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
4.2 Flowcharts and
w
CONTINUED
ie
id
To get to the Finish you need to turn left:
ev
br
LT90 am pseudocode
-R
Then move forward 7 spaces: A flowchart is a diagram that uses specific shapes and
arrows to produce an algorithm that someone can
-C
FD7
s
then follow.
es
y
Pr
op
ity
C
rs
w
ie
ve
y
Flowchart structure
ev
op
ni
R
C
be introduced to these in each section. Every flowchart
ge
Start
w
begins and ends with a terminator box (a Start box at
ie
id
ev
br
am
-R Start
Together, the algorithm becomes:
FD4, LT90, FD2, RT90, FD3, LT90, FD5, LT90, FD7
-C
s
es
Stop
y
Pr
op
2 How can an algorithm be represented? you follow the arrows. Algorithms don’t go straight
ie
ve
y
from start to stop. They also need input, output,
ev
op
ni
-R
to the user
-C
Start
make a change
y
Pr
op
ve
y
ev
C
e
w
g
ie
id
ev
br
am
-R
56
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
Input and output
w
KEY WORDS
ie
id
Algorithms often need to output messages (such as text) variable: a space in the memory of a computer,
ev
br
to the screen, and therefore to the user. They may also
am that has an identifier where you can store data;
-R
need to take in data from the user, for example from this data can be changed
the keyboard.
-C
s
es
Flowcharts subroutine or function
y
Pr
If you want the algorithm to output something to the concatenate: to join two strings together
op
ity
C
ve
OUTPUT
y
ev
op
ni
INPUT myNumber
R
C
ge
w
After the word OUTPUT you put the text you want
ie
id
to display. If you want to output "Hello World", Put these together with the Start and Stop to create a
ev
br
-R
Start
-C
INPUT myNumber
output, without them “Hello” could be a variable, or a
ie
ve
command word.
y
ev
op
ni
If you want to input (or read) data from the user into Stop
R
INPUT
ev
OUTPUT myNumber
stored somewhere. This is done using a variable. A
es
Pr
op
computer’s memory that stores a value. Think of it like Figure 4.15: Output box with identifier.
a box, with a name. You could call the box myNumber.
ity
C
You can then put data into myNumber. You can get data You might want to output a variable and some text. You
rs
w
out of myNumber, and so on. can combine them by using an ampersand (&). This is to
ie
ve
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
57
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
w
OUTPUT "My number CONTINUED
ie
id
is" & myNumber
Step 2: Take their name as input.
ev
br
Figure 4.16: Output box with concatenation.
am Input and store in variable
-R
Step 3: Welcome them by their name.
-C
Pseudocode
s
Output including variable
es
An output in pseudocode can use a command word such Draw the flowchart following the steps identified.
y
Pr
op
ity
C
Start
rs
w
ve
different words that you can use to output data, but they
y
OUTPUT "Enter
ev
all perform the task, for example all of these have the
op
ni
your name"
same result.
R
C
OUTPUT "Hello"
ge
w
WRITE "Hello"
ie
id
INPUT name
PRINT "Hello"
ev
br
-R
as INPUT. As with the flowcharts, this will need to be
stored somewhere such as in a variable, for example,
-C
OUTPUT "Welcome"
es
Pr
Stop
rs
w
ve
y
ev
Draw a flowchart for the algorithm. Convert the flowchart into pseudocode.
ev
br
Start by identifying the steps required. Take each statement from within the flowchart and
am
-R
INPUT name
Step 3: Welcome them by their name.
y
Questions
w
Output
ie
ve
op
ni
flowchart.
R
w
g
ie
id
ev
br
am
-R
58
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
There are different types of processes.
w
Start
ie
id
Table 4.1 shows the different mathematical processes
ev
that be performed.
br
am
-R
OUTPUT "Enter
a number" Operator Description Example
-C
s
+ Addition myNumber = 10 + 3
es
– Subtraction myNumber = 10 – 3
y
Pr
op
INPUT number
* Multiplication myNumber = 10 * 3
ity
C
/ Division myNumber = 10 / 3
rs
w
^ Power of myNumber = 10 ^ 3
ie
ve
y
ev
op
ni
number
C
the remainder) 20 MOD 8 = 4
ge
w
DIV Integer 10 DIV 3 = 3
ie
id
division (gives
20 DIV 6 = 2
ev
br
-R
Figure 4.18: Flowchart-Enter a number. Table 4.1: Mathematical (arithmetic) operators.
-C
s
es
y
Pr
KEY WORDS
ity
C
1 Convert the flowchart in Question 4 into modulus division: when the remainder is given
pseudocode. from a division
rs
w
ie
ve
2 An algorithm needs to take a word as an integer division: where only the whole number is
y
ev
input, and then output the same word. given from a division
op
ni
R
answer, before outputting the actual answer. Processes are in rectangular boxes.
ev
br
am
problem.
Process
-C
s
es
y
ity
C
ve
op
ni
C
e
w
g
ie
id
ev
br
am
-R
59
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
The mathematical calculations can make use of Notice that a new symbol has been introduced here, a
w
numbers, and/or variables. For example, circle with a letter inside. This is a connector symbol.
ie
id
myNumber = 10 + 3 myNumber now stores 13 Sometimes, if a flowchart is long, then it is convenient
ev
br
to split it into smaller portions (perhaps to show it all
myNumber = myNumber + 3
am myNumber now stores 16
-R
on one page). The connector letter shows where the
(it already had 13 in and
flowchart continues.
now has another 3)
-C
s
myNumber = myNumber myNumber now stores The algorithm takes in two numbers (num1 and num2),
es
+ myNumber 32 (it already had 16 in divides num1 by num2 and stores the result in answer.
y
Pr
It then outputs the message "The result is" and the
op
ity
C
rs
Pseudocode
w
ie
ve
Processes use the same structure as in a flowchart, but
y
ev
op
ni
R
C
ge
w
newNumber = 3 * 8
ie
id
ev
br
-R
would become this:
-C
newNumber = 3 * 8
s
es
newNumber = 3 * 8
y
Pr
op
Follow this flowchart. What does it do? A program needs to ask a person’s age, then tell them
rs
w
ve
A
y
Start
Draw a flowchart for the algorithm.
ev
op
ni
number"
Step 2: Take the age as input.
ie
id
ev
br
-R
INPUT num1 answer = num1 / num2 Step 4: Output the new age.
Identify what type of symbols these steps need;
-C
s
es
A Stop
Step 3: Add 50 to their age.
ie
ve
Output variable
e
w
g
ie
id
ev
br
am
-R
60
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
w
CONTINUED Start
ie
id
Draw the flowchart following the steps identified.
ev
br
am INPUT num1
-R
Start
-C
s
es
OUTPUT "Enter INPUT num2
A
your age"
y
Pr
op
ity
C
rs
w
ie
ve
y
ev
op
ni
C
ge
w
ie
id
OUTPUT "In 50
ev
total = num1 – num2
br
-R
-C
Stop
s
OUTPUT num1 & "–"
es
Pr
op
ity
C
Stop
rs
w
ve
op
ni
OUTPUT "In 50 years your will be" & newAge 2 An algorithm needs to take 3 numbers as
am
-R
Pr
5 What is a process?
pseudocode.
ity
C
ve
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
61
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
Decisions Flowcharts
w
ie
id
A decision is also known as comparison and selection. It Single selection
ev
br
is a statement that involves a comparison and is either The selection must be in the form of a question, for
am example ‘is 10 > 5?’ This is because it has two results:
-R
true or false. If the result is true, then some code is run,
if it is false then either different code is run or some true and false.
-C
code is skipped.
s
The selection flowchart symbol is a diamond.
es
y
Pr
op
KEY WORDS
DECISION
ity
C
rs
w
ie
ve
selection: use of a conditional statement to Figure 4.25: Selection box.
y
ev
op
ni
code to run
R
C
and False, or Yes and No. This means selection has
ge
w
two arrows (two flow lines), that need to be labelled,
A comparison needs two sides of the argument, and
ie
id
for example.
a comparison symbol. Table 4.2 shows common
ev
br
comparison operators.
am
-R
Yes
DECISION
Operator Description Example
-C
s
es
No
Pr
op
5 > 10 is false
Figure 4.26: Selection box with options.
ity
<
C
ve
op
ni
or equal to Start
10 >= 10 is true
R
5 >= 10 is false
ge
INPUT age
w
equal to
ev
br
10 <= 10 is true
am
-R
s
es
10 = 10 is true No
y
Pr
5 <> 10 is true
C
or young"
rs
w
<>
ie
ve
y
ev
Stop
Table 4.2: Comparison operators.
op
ni
R
a variable.
w
g
ie
id
ev
br
am
-R
62
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
This flowchart starts by the user inputting an age. The Multiple conditions
w
decision statement asks if age is greater than, or equal
ie
These can be created using ELSEIF statements. Each
id
to, 18. If the result is Yes, then it outputs “You are old condition has its own ELSEIF. You can even put an
ev
br
enough”, if it is No it outputs “You are too young”.
am ELSE on the end in case none of the conditions are
-R
true. For example, each of the conditions are checked
Multiple selection
in the order they are written. If one of the conditions is
-C
s
found to be true, then the code within it runs, and then
es
decisions. For example, it jumps to the ENDIF; the other conditions after it are
y
not checked.
Pr
op
ity
Yes
C
Is age <
10? OUTPUT "That is far too expensive"
rs
w
ve
OUTPUT "That’s a bit too expensive"
y
No
ev
op
ni
ELSEIF cost >= 60 THEN
R
C
ELSEIF cost >= 40 THEN
ge
w
20? OUTPUT "That looks like a good deal"
ie
id
ELSE
ev
br
-R
ENDIF
Figure 4.28: Flowchart with multiple selection boxes.
-C
CASE…END CASE
es
The first decision is ‘If age is less than 10?’ If it is not, This is a different selection statement. This is used when
y
Pr
you are checking the value of one variable and you have
whether age is less than 20. many different values you want to check it against.
ity
C
rs
Pseudocode Pseudocode
w
ie
ve
Selection uses the keyword IF and THEN. The question CASE uses the keyword SELECT, followed by the
y
ev
‘Is’ in the flowchart is replaced with IF, and the ‘?’ with identifier of the variable you are checking. Then it has
op
ni
THEN. All the code that runs when the condition is true the keyword CASE followed by the condition.
R
goes beneath the IF and this is finished with an ENDIF For example, if you are checking if a menu choice is
ge
1, 2 or 3:
ie
id
For example, if the value in age is more than 18, then SELECT menuChoice
ev
br
the output message will be displayed. CASE 1: OUTPUT "You chose 1"
am
-R
An IF statement can have an ELSE. This is what happens equals to, but this time you need to include the variable
y
Pr
if the condition is true. For example, if the age is not name to make it clear what the comparison is.
op
more than 18, then "You are not old enough" will be SELECT age
ity
C
displayed.
CASE age < 12: OUTPUT "You can watch PG
rs
w
ve
op
ELSE
ni
ie
id
ev
br
am
-R
63
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
CASE age >= 18: OUTPUT "You can watch all CASE "-":
w
films"
ie
result = value1 – value2
id
There is also a default keyword for CASE, the code in
ev
br
CASE "*":
this will run if none of the conditions have been met.
am
-R
result = value1 * value2
SELECT symbol
CASE "/":
-C
CASE "+":
s
result = value1 / value2
es
result = value1 + value2
y
CASE DEFAULT:
Pr
op
ity
C
rs
w
ve
y
ev
op
ni
An algorithm needs to take two numbers as input, and Step 5: Output the second number if it isn’t.
R
C
Output second number
ge
w
ie
id
Start
Start by identifying the steps required.
ev
br
-R
OUTPUT "Enter
Step 2: Input two numbers.
two numbers"
-C
second.
y
Input num1,
Pr
num2
ve
Yes
Step 1: Ask the user to input two numbers. Is num1 OUTPUT num1
y
> num2?
ev
Output
op
ni
R
No
ge
Step 3: Check if the first number is larger than the OUTPUT num2
ev
second.
br
-R
Stop
s
ity
C
rs
w
ie
ve
y
ev
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
64
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
w
WORKED EXAMPLE 4.07 CONTINUED
ie
id
Convert the flowchart into pseudocode.
ev
br
Start
am
Write each line separately. Only one value can be input
-R
at a time.
-C
OUTPUT "Enter
s
Replace Is with an IF statement. mark out of 100"
es
OUTPUT "Enter two numbers"
y
Pr
op
INPUT num1
INPUT mark
INPUT num2
ity
C
rs
w
OUTPUT num1
ie
ve
Is mark Yes
ELSE
y
OUTPUT "Distinction"
ev
>75?
op
ni
OUTPUT num2
R
ENDIF
C
No
ge
w
Is mark Yes
ie
id
OUTPUT "Merit"
>60?
Questions
ev
br
am
symbol in a flowchart?
-C
Is mark Yes
s
Pr
No
op
12 Is 123 != 123?
13 Is 55 = 66?
ity
OUTPUT "Fail"
C
rs
w
ve
y
ev
Stop
op
ni
2 Convert the flowchart in part 1 into Figure 4.30: Flowchart with comments.
ge
pseudocode.
w
ie
id
-R
Pr
KEY WORD
as input, then subtract the smallest from
ity
C
ve
op
ni
w
g
ie
id
ev
br
am
-R
65
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
Loops are mainly used in two scenarios: Pseudocode
w
ie
A FOR loop is a count-controlled loop. You need to
id
1 You know how many times you want to repeat the
know what number to start with, and when you will end.
ev
code. This might be 10 times, or age times (where
br
age is a variable that stores a value). This is a
am The loop increments the number each time through.
-R
count-controlled loop.
2
-C
You don’t know the number of times, but want KEY WORDS
s
to keep going until something happens. This is a
es
condition-controlled loop. increment: add 1 to something
y
Pr
op
ity
C
KEY WORDS
rs
w
count-controlled loop: a loop where you know In this example, the variable counter starts at 0 and
ie
ve
the number of times it will run continues until it is 9.
y
ev
op
ni
C
OUTPUT counter
it will run
ge
w
NEXT counter
ie
id
ev
br
-R
Flowcharts An algorithm needs to ask the user to input the 10
marks of its students, and then output the average.
-C
Pr
ve
Start
y
Step 4: Add the mark to a total.
ev
op
ni
num = 0
C
the average.
ge
No
br
OUTPUT
Is num > 5?
num
num = num + 1 Step 1: Repeat 10 times.
am
-R
Loop 10 times
Yes
Step 2: Ask the user to input a mark.
-C
Stop Output
es
Pr
until num is greater than 5, at which point the algorithm Step 5: After all marks are entered, calculate the
ie
ve
stops.
ev
op
ni
Output
C
e
w
g
ie
id
ev
br
am
-R
66
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
w
CONTINUED PRACTICAL ACTIVITY 4.05
ie
id
1 Describe the stages in this flowchart.
ev
br
Start
am
-R
Start
count = 0
-C
s
total = 0
es
y
count = 10
Pr
op
Is count No
ity
C
OUTPUT "Enter a
= 10? mark"
rs
w
Is count No
ie
y
ev
INPUT mark
op
ni
C
counter = counter – 1
ge
Stop
w
total = total + mark
ie
id
OUTPUT average
Figure 4.33: Flowchart with counter.
ev
br
count = count + 1
am
Pr
op
ve
Write each line separately. Replace the count and that number input.
op
ni
FOR count = 0 TO 10
ie
id
INPUT mark
Condition controlled
total = total + mark
am
-R
average = total / 10
s
OUTPUT average could be controlled by an input from the user (you could
y
Flowchart
rs
14 What is iteration?
w
ve
15 What is a count-controlled loop? to the condition, but it would be missing the variable
y
ev
controlled loop?
g
ie
id
ev
br
am
-R
67
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
Pseudocode
w
Start
A WHILE loop is a condition-controlled loop. It keeps
ie
id
looping while the condition is true.
ev
br
am
-R
INPUT age
KEY WORDS
-C
s
REPEAT UNTIL loop: a condition-controlled loop
es
that runs until the condition is true
y
Pr
IS age >= No OUTPUT "You are too
op
young"
18? WHILE loop: a condition-controlled loop that
ity
C
rs
w
Yes
ie
ve
In this example the loop will continue until the user
y
OUTPUT "You are
ev
op
ni
old enough" enters an age greater than or equal to 18.
R
age = 0
C
ge
w
ie
id
INPUT age
Figure 4.34: Flowchart with condition-controlled loop 1.
ev
br
ENDWHILE
am
The code inside the loop will always run once because
s
value = 1
es
value = value * 2 repeats until the value is >= 100. WHILE value <= 100
Pr
op
Start
C
ENDWHILE
rs
w
ie
ve
value = 1
y
WORKED EXAMPLE 4.10
ev
op
ni
value = value * 2
ie
id
-R
100? Step 2:
es
Pr
op
Yes
Step 4: Add the number to the total.
ity
C
ve
op
ni
In this example, the loop keeps on going until the number Step 1: Repeat until the total is more than 100.
R
in the variable value is greater than or equal to 100. Loop until total > 100
C
e
w
g
ie
id
ev
br
am
-R
68
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
Question
w
CONTINUED
ie
id
Step 2: Ask the user to input a number. 18 When should a condition-controlled loop be used?
ev
br
Output
am
-R
PRACTICAL ACTIVITY 4.06
Step 3: Input the number.
-C
s
Input 1 Describe the stages this flowchart follows.
es
Step 4: Add the number to the total.
y
Pr
Start
op
ity
C
rs
w
ie
Output
ve
y
ev
counter = 0
op
ni
Start
R
C
ge
w
total = 0 Is total NO OUTPUT "Enter a
< 0? number"
ie
id
ev
br
YES
INPUT number
am
INPUT number
y
Pr
op
OUTPUT total
counter = counter + 1
Stop
ity
C
Stop
w
ve
y
Figure 4.36: Flowchart-Totalling number with control
ev
op
ni
Convert the flowchart into pseudocode. Draw a flowchart for the algorithm.
am
-R
Replace the selection condition with a WHILE loop. 4 An algorithm stores a number. The user
-C
this problem.
ENDWHILE
ie
ve
OUTPUT total
y
ev
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
69
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
String manipulation In this example, the four digits of the year (1999) are
w
extracted from the string and output.
ie
id
Strings are collections of letters, characters and
ev
br
numbers. They are represented by speech marks; this
am Start
-R
is because the variable value is different from the
word “value”.
-C
date = "01/01/1999"
s
You may need to manipulate strings, for example get
es
one or more characters from it or find its length.
y
Pr
op
These are all processes, so would go inside a process box year = substring(date,
6, 4)
(apart from concatenation which can also be in an input
ity
C
or output box).
rs
w
OUTPUT year
Table 4.3 shows some common string manipulators.
ie
ve
y
ev
op
ni
C
length(string) Returns the length("Hello")
ge
length of the would give 5 Figure 4.38: Flowchart to extract year from a date
w
string pseudocode.
ie
id
ev
br
char(string, Returns the char("Hello",0) The statements are the same in pseudocode, they are just
characterNum) character at would give "H"
am
startingChar, numChars would give "el" A program is needed to input a word from the user,
Pr
op
numChars) or number of and then output alternate letters starting with the first
ity
substring(string, characters
C
letter.
startingChar, from
rs
w
ve
op
ni
capitals "HELLO"
w
-R
s
es
Pr
rs
w
ie
ve
y
ev
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
70
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
w
CONTINUED WORKED EXAMPLE 4.13
ie
id
Identify what type of symbols these steps need. Convert the flowchart into pseudocode. Replace the
ev
br
loop with a FOR loop (from 0 to wordlength) but it
Step 1: Ask the user to input a word.
am
-R
needs to add 2 each time.
Output
OUTPUT "Enter a word"
-C
s
Step 2: Input a word and store it in a variable. INPUT word
es
Input and store in a variable wordLength = length(word)
y
Pr
op
Step 3: Count the number of letters in the word . FOR counter = 0 TO wordLength
ity
OUTPUT mid(wordLength, counter, 1)
C
rs
w
ve
Process set counter to be 0
y
ev
op
ni
the loop.
C
Question
ge
w
19 What number is the first character in a string?
ie
id
ev
br
Start
how many letters they want to enter. The
program should then ask the user to enter
ity
C
word"
output the result.
ie
ve
y
Draw a flowchart for the algorithm.
ev
INPUT word
op
ni
3
U
wordLength = length(word)
w
ev
br
counter = 0
Create a pseudocode algorithm for
am
-R
the problem.
-C
wordLength? mid(wordLength,counter,1)
y
Pr
op
Yes
counter = counter + 2
ity
C
Stop
rs
w
ie
ve
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
71
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
control back to the program that called it. A function
w
CONTINUED can return a value to the program that called it. You do
ie
id
not need to know about functions, or use them in this
ev
br
Start
am chapter, but it is useful to understand that a procedure is
-R
not the only form of a subroutine.
OUTPUT "Enter a
-C
name"
s
KEY WORDS
es
y
Pr
procedure: a type of subroutine that does not
op
INPUT name
return a value to the main program
ity
C
rs
w
ve
from elsewhere in the code and returns a value
y
ev
op
ni
R
first = upper(first)
C
Flowcharts
ge
w
Each subroutine is a separate flowchart, and instead of
ie
id
restOfName = mid(name,1, length–1) having a Start box, it has the name of the subroutine,
ev
br
for example,
am
-R
Identifier
word = first & restOfName
-C
s
es
y
Pr
OUTPUT word
op
Stop
In this example, the subroutine is called OutputText.
rs
w
ve
Figure 4.40: Flowchart. process box but has a pair of parallel vertical lines at
y
ev
op
ni
Subroutines
w
Start Output
They have an identifier (a name) and can be called from
am
-R
s
es
Pr
op
Stop Stop
program and returns control when it was finished
ie
ve
y
ev
op
ni
w
g
ie
id
ev
br
am
-R
72
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
Pseudocode
w
Start calculate(value)
ie
id
In pseudocode, a subroutine still has an identifier. It
ev
starts with the command PROCEDURE and ends with
br
ENDPROCEDURE.am
-R
In this example, the procedure is called total = total +
INPUT number
-C
s
es
subroutine. The procedure outputs the words "Hello
World" and then returns control.
y
Pr
op
PROCEDURE outputMessage()
ity
C
rs
w
ENDPROCEDURE
ie
ve
y
A procedure is called by using the name of the
ev
op
ni
C
INPUT x Stop Stop
ge
w
outputMessage()
Figure 4.43: Flowchart calling calculate subroutine.
ie
id
OUTPUT "Finished"
ev
br
-R Start calculate(value)
the main program beneath all the function calls.
-C
Parameters
es
Pr
total = total +
op
KEY WORD
rs
w
ie
ve
subroutine
op
ni
calculate(number)
C
ge
Flowchart
ie
id
-R
> 10?
es
y
Pr
op
Yes
ity
C
rs
w
ie
ve
Stop
y
ev
op
ni
R
subroutine.
e
w
g
ie
id
ev
br
am
-R
73
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
Pseudocode PROCEDURE calculate(value)
w
total = total + value
ie
Parameters are identified within the brackets.
id
OUTPUT total
ev
br
In this example, the main program (beneath the
am ENDPROCEDURE
procedure), inputs a value into the variable number and
-R
then sends this to the procedure calculate. This is then INPUT number
-C
s
es
y
Pr
op
A program asks the user to input a number. It loops b Output the result.
ity
C
that a number of times. Each time through the loop, Output result
rs
w
if the number is even it calls a subroutine to output Create a subroutine taking number as parameter.
ie
ve
the number multiplied by itself. If the number is odd it
y
powerOf(number)
ev
op
ni
C
Draw a flowchart for this algorithm. Process number ^ number
ge
w
Start by identifying the steps required.
ie
Output result
id
ev
br
Step 2: Set the counter to 1. Draw the flowchart for the subroutines.
am
Step 5: Else if the number is odd, call subroutine. result = number * number result = number ^ number
y
Pr
ve
op
ni
Step 1: User inputs a number Draw the flowchart for the main program.
ie
id
Process counter = 1
-R
INPUT number
counter = 1
s
counter = counter + 1
Step 4: If the number is even call subroutine
y
Pr
number? mod 2 = 0?
C
Yes Yes
Create a subroutine taking number as parameter.
ie
ve
Stop multiply(number)
multiply(number)
y
ev
op
ni
w
g
ie
id
ev
br
am
-R
74
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
w
WORKED EXAMPLE 4.15 CONTINUED
ie
id
Convert the subroutines and main program to
ev
br
sub1
pseudocode. am
-R
Write each subroutine first, then the main program.
-C
PROCEDURE multiply(number)
s
OUTPUT "Choose 1
es
result = number * number or 2"
y
OUTPUT result
Pr
op
ENDPROCEDURE
ity
C
PROCEDURE powerOf(number)
rs
w
ve
OUTPUT result
y
ev
ENDPROCEDURE
op
ni
INPUT number
R
C
Counter = 1
ge
w
WHILE counter < number
ie
id
Is choice Yes
IF counter MOD 2 = 0 THEN sub2
= "1"?
ev
br
multiply(number)
am
ELSE
-R
powerOf(number)
-C
ENDIF No
es
ENDWHILE
y
Pr
op
Is choice Yes
sub3
ity
C
= "2"?
Yes
Questions
rs
w
ie
ve
20 What is a subroutine? No
y
ev
22 What is a parameter?
R
sub2
ev
br
-R
Start
OUTPUT "You
-C
chose 1"
es
y
sub1
Pr
op
ity
C
Stop
rs
w
ve
y
ev
C
e
w
g
ie
id
ev
br
am
-R
75
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
w
CONTINUED KEY WORDS
ie
id
construct: a control structure, such as a loop or a
ev
br
sub3
am conditional statement
-R
nested loops: one construct that is inside
-C
s
es
y
Pr
op
ity
C
WHILE loop.
rs
w
ie
ve
Start
y
ev
OUTPUT result
op
ni
R
Yes
Is continue
C
continue = False
= "YES"?
ge
w
Stop
ie
id
No
Figure 4.50: Flowchart d.
ev
br
am
-R Is continue Yes
OUTPUT "loop
2 Convert the flowcharts in part 1 into = False? again?"
pseudocode.
-C
No
es
continue =
Pr
op
Stop upper(INPUT)
lowercase. Create a procedure to turn a
word uppercase and output it, and a second
ity
C
ve
= "YES"? or
which choice the user inputs. continue ! = Loop again?"
y
ev
"NO"?
op
ni
pseudocode.
w
continue = False
ev
br
-R
continue = upper(INPUT)
inside of another construct (selection or iteration). For
es
Pr
"No"
op
ve
ENDWHILE
y
ev
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
76
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
w
WORKED EXAMPLE 4.16
ie
id
A program needs to input the results for 100 students.
ev
br
Start
Each student has 10 different results and the total
am
-R
needs to be output for each student.
Create a flowchart for the algorithm. studentNumber = 1
-C
s
Start by identifying the steps required.
es
studentNumber = studentNumber + 1
Step 1: Loop through all 100 students.
y
Pr
op
ity
Is
C
> 100?
ie
ve
Step 5: Add the result to their total.
y
Yes No
ev
op
ni
C
Identify what type of symbols these steps need. result" result
ge
w
Step 1: Loop through all 100 students.
ie
id
ev
br
studentResult
Step 2: Loop through all 10 results for each student.
am
-R
Count-controlled loop
Step 3: Ask for the student’s result.
-C
s
total = total + studentResult
Output
es
Pr
op
ve
Step 6: Output the total at the end of each student’s Figure 4.52: Flowchart-Totalling results.
y
ev
results.
op
ni
Output
R
C
ge
w
ie
id
total = 0
y
Pr
op
FOR result = 1 to 11
OUTPUT "Enter " & studentNumber & "'s result " & result
ity
C
INPUT studentResult
rs
w
ve
NEXT result
ev
op
ni
ENDWHILE
e
w
g
ie
id
ev
br
am
-R
77
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
Question
w
WORKED EXAMPLE 4.18
ie
id
23 What is a nested statement?
ev
br
am
-R
PRACTICAL ACTIVITY 4.09
-C
s
1 An algorithm should ask the user if they Start
es
want to perform a calculation, and should
y
Pr
op
ity
C
rs
w
ve
Finish
the result.
y
ev
op
ni
C
2 An algorithm should take a word as input
ge
w
from the user. Then take each letter in turn, The following algorithm visits the yellow box, then the
ie
id
and output it the number of times of its green before moving to finish. Start facing up.
ev
br
-R RT90
twice, and so on.
FD4
-C
RT90
s
LT90
Pr
op
FD1
RT90
ity
C
LT90
You need to be able to read an algorithm, or flowchart,
ie
ve
FD4
y
and be able to identify how to make changes to it.
ev
op
ni
algorithm, follow each step and write down what The algorithm can stay the same up to the yellow box,
w
happens. Then look at the difference between what the so repeat this:
ie
id
FD2
RT90
am
-R
FD4
-C
Pr
RT90
op
FD7
ity
C
LT90
FD5
rs
w
ie
ve
y
ev
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
78
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
Questions
w
WORKED EXAMPLE 4.19
ie
id
This flowchart takes in a user’s first name and 24 This flowchart takes 10 numbers as input, adds
ev
br
favourite colour. It takes the first three letters of the
am them together and outputs the total.
-R
first name, and the first three letters of the colour to
Start
create and output a username.
-C
s
es
Start
counter = 0
y
Pr
total = 0
op
INPUT firstname
ity
C
rs
w
No
ie
total = total +
ve
Is counter
INPUT number
number
y
INPUT colour = 10?
ev
op
ni
R
Yes
C
ge
username = Stop
w
mid(firstname,0,3)
ie
id
ev
br
mid(colour,0,3)
-R
Change the flowchart so it inputs 20 numbers and
adds them together.
-C
OUTPUT
username finds the smallest number and multiplies it by itself
y
Pr
smallest = 9999
ity
C
Stop
FOR counter = 0 to 50
rs
w
ve
y
ev
ENDIF
ge
letters are taken from the colour. This box was NEXT counter
ie
id
OUTPUT answer
-R
changing to:
Change the algorithm so it finds the largest number
-C
length(colour)–4,3)
y
Pr
op
Correcting an algorithm/
ity
C
flowchart
rs
w
ie
op
ni
w
g
ie
id
ev
br
am
-R
79
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
w
WORKED EXAMPLE 4.20
ie
id
An algorithm should take three numbers as input, it should add together the two largest, and output the result.
ev
br
am
Find the error in the algorithm.
-R
-C
Start
s
es
y
Pr
op
INPUT num1,
num2, num3
ity
C
rs
w
ie
ve
Is num1> Is num2>
y
ev
No No
num2 and num3 and
op
ni
C
ge
w
Yes Yes
ie
id
ev
Yes
br
Is num1
No No > num2?
Is num2 Is num1
am
-R
> num3? > num3?
-C
No
s
Yes
es
Yes
y
Pr
op
ity
ve
y
ev
op
ni
R
OUTPUT total
ge
w
ie
id
ev
br
Stop
am
-R
s
es
y
Pr
op
ity
C
rs
w
ie
ve
y
ev
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
80
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
w
CONTINUED
ie
id
Run the algorithm with all possible different types Test 2
ev
br
example data, for example,
am Is num1 > num2 and num1 > num3? No
-R
Test 1: num1 = 10, num2 = 5, num3 = 3 (num1 and Is num2 > num3 and num2 > num1? No
num2 are the largest), Output should be 15 Is num1 > num2? Yes, total = num3 + num2 = 10 + 3
-C
s
Test 2: num1 = 5, num2 = 3, num3 = 10 (num1 and OUTPUT 13 – This is incorrect. It should have carried
es
num3 are the largest), Output should be 15 out total = num3 + num1.
y
Pr
op
Test 3: num1 = 3, num2 = 10, num3 = 5 (num2 and The yes and no from the Is num1 > num2? Box are in the
num3 are the largest), Output should be 15 wrong order, they need swapping.
ity
C
Test 1 Test 3
rs
w
Is num1 > num2 and num1 > num3? Yes Is num1 > num2 and num1 > num3? No
ie
ve
y
Is num2 > num3 Yes this is true, total = num1 + num2 Is num2 > num3 and num2 > num1? Yes
ev
op
ni
C
OUTPUT 15 – correct OUTPUT 15 – correct
ge
w
ie
id
ev
br
-R
1 The algorithm should ask the user how many letters they want to enter, then let them enter that many
-C
Pr
op
Start
ity
C
rs
w
number of letters"
ve
y
ev
op
ni
R
INPUT number
C
ge
w
ie
id
counter = 0
ev
br
am
-R
-C
INPUT letter
number? & letter
y
Pr
op
Yes
ity
C
OUTPUT word
rs
w
ie
ve
y
ev
Stop
op
ni
R
w
g
ie
id
ev
br
am
-R
81
-C
s
es
y
CAMBRIDGE INTERNATIONAL AS & A LEVEL IT: COURSEBOOK
op
ni
U
C
ge
w
CONTINUED
ie
id
2 The algorithm should repeatedly ask the user to input a number. The difference between that number
ev
br
and the previous number should be added to the total. This should continue until the total is more
am
-R
than 1000.
-C
s
es
total = 1
y
previousNumber = 0
Pr
op
ity
C
INPUT number
rs
w
ve
previousNumber = number
y
ev
ENDWHILE
op
ni
R
C
ge
w
REFLECTION
ie
id
ev
1
br
am
s
es
y
Pr
op
ity
C
EXAM-STYLE QUESTIONS
rs
w
ve
y
ev
C
ge
w
ie
id
[3]
am
-R
-C
s
es
y
Pr
op
ity
C
rs
w
ie
ve
y
ev
op
ni
R
C
e
w
g
ie
id
ev
br
am
-R
82
-C
s
es
y
4 Algorithms and flowcharts
op
ni
U
C
ge
w
CONTINUED
ie
id
3 State what the output will be when this algorithm is run when the data input is 10. [1]
ev
br
am
-R
Start
-C
s
es
INPUT num
y
Pr
op
ity
C
rs
w
10?
ve
y
ev
op
ni
No
R
C
ge
w
Is num > Yes
ie
id
OUTPUT num + 10
5?
ev
br
am
No
-R
-C
OUTPUT num
es
y
Pr
op
Stop
ity
C
rs
w
ve
y
ev
op
ni
Start
ge
w
ie
id
ev
br
count = 1
am
-R
-C
s
es
Is count > No
answer = count * 5 OUTPUT answer
y
12?
Pr
op
ity
Yes
C
rs
w
Stop
ie
ve
y
ev
[3]
e
w
g
ie
id
ev
br
am
-R
83
-C
s
es