0% found this document useful (0 votes)
76 views31 pages

It Cambridge Textbook

This document covers algorithms and flowcharts, teaching how to edit and create algorithms using pseudocode and flowcharts to solve problems. It introduces key concepts such as input, output, processes, decisions, and loops, along with practical examples and exercises. The chapter aims to equip readers with the skills to develop algorithms that can be followed by computers.

Uploaded by

xtodan Anobeli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views31 pages

It Cambridge Textbook

This document covers algorithms and flowcharts, teaching how to edit and create algorithms using pseudocode and flowcharts to solve problems. It introduces key concepts such as input, output, processes, decisions, and loops, along with practical examples and exercises. The chapter aims to equip readers with the skills to develop algorithms that can be followed by computers.

Uploaded by

xtodan Anobeli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

ity

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

By the end of this chapter, you will be able to:


C
ge

• edit a given algorithm


w
ie
id

• write an algorithm using pseudocode to solve a given problem


ev
br

• edit a given flowchart


am

-R

• draw a flowchart to solve a given problem.


-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
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

turtle on screen such as through Logo?

s
es
y

Pr
op

Introduction

ity
C

Start

rs
w

In this chapter you will learn how to develop algorithms


ie

ve
to solve problems in a form that a computer could

y
ev

follow. All computer programs are simply a series of

op
ni

algorithms that are written in a specific way that the


R

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

instructions. These are both examples of algorithms.


Step 1: FD7 Step 5: FD2
y

Before you write algorithms on a computer, you can


Pr
op

design them using a flowchart and/or using pseudocode. Step 2: RT90 Step 6: RT90
ity
C

Pseudocode simply means there is no specific syntax that


Step 3: FD6 Step 7: FD1
you have to use. It doesn’t have to follow a set format,
rs
w

but it is more structured than English sentences. Step 4: LT90


ie

ve

y
ev

op
ni

KEY WORDS WORKED EXAMPLE 4.01


R

flowchart: a set of symbols put together with You are on Start, facing to the right.
ge

commands that are followed to solve a problem


Write an algorithm to move from Start to Finish.
ie
id

pseudocode: a language that is used to display


ev
br

an algorithm Finish
am

-R
-C

A simple form of an algorithm is a series of steps to


es

move someone (or something) through a maze. The


y

instructions you can use are:


Pr
op

ity
C

FD5 Move forward 5 spaces


rs
w

BK2 Move backward 2 spaces


Start
ie

ve

RT90 Turn right 90 degrees


y
ev

op
ni

LT90 Turn left 90 degrees Figure 4.2: Maze 2, start.


R

You start in the space labelled ‘Start’ in this maze, facing


e

to the right.
w
g

ie
id

ev
br
am

-R

54
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

Figure 4.3: Maze 2, FD4.

ev
br

Figure 4.5: Maze 2, FD4, LT90, FD2, RT90, FD3.


am

-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

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

Finish Pseudocode does not use symbols, but structured

Pr
op

statements. There is no specific syntax (rules) that it


must follow, but it should use key words (as in the flow

ity
C

diagrams) such as INPUT, OUTPUT, and so on.

rs
w
ie

ve

y
Flowchart structure
ev

op
ni
R

There are set symbols throughout a flowchart. You will

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

the beginning and a Stop box at the end).


Figure 4.7: Maze 2, finish.

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

Questions Figure 4.9: Start/Stop box.


ity
C

1 What is meant by an algorithm?


The arrow is the flow line. When following a flowchart
rs
w

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

PRACTICAL ACTIVITY 4.01 processes, decisions and loops.


R

Write an algorithm to move from Start to Finish


ge

in this maze: KEY WORDS


ie
id

input: putting data into an algorithm


ev
br

output: displaying data from an algorithm


am

-R

to the user
-C

process: an action performed to some data to


es

Start
make a change
y

Pr
op

Finish decision: a comparison is used to decide if code


is run, or not
ity
C

loop: code that is repeated


rs
w
ie

ve

y
ev

Figure 4.8: Maze 3, Start.


op
ni
R

C
e

w
g

ie
id

ev
br
am

-R

56
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

identifier: the name given to a variable,

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

user, for example a word, a sentence or a number, then

ity
C

you will need an output box.


Here’s the symbol for inputting a value into myNumber.
rs
w
ie

ve
OUTPUT

y
ev

op
ni
INPUT myNumber
R

Figure 4.10: Output box.

C
ge

Figure 4.13: Input box with identifier.

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

you use this, flowchart.


am

-R
Start
-C

OUTPUT "Hello World"


s
es
y

Figure 4.11: Output box with text.


Pr

OUTPUT "Hello World"


op

Notice how the words being output are in speech


ity
C

marks. This is because you want those actual words


rs
w

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

the algorithm, you use an input box.


C
ge

Figure 4.14: Flowchart "Hello World".


ie
id

INPUT
ev

Once you have a variable, you can output this by


br

using its name.


am

Figure 4.12: Input box.


-R
-C

When data is input into an algorithm it needs to be


s

OUTPUT myNumber
stored somewhere. This is done using a variable. A
es

variable is a name (identifier) that is given to a space in a


y

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

concatenate (join) the values together. For example,


y
ev

op
ni
R

C
e

w
g

ie
id

ev
br
am

-R

57
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

as OUTPUT, WRITE or PRINT, for example,

Pr
op

OUTPUT "Hello World"

ity
C

Start

The words "Hello World" are a string as in the

rs
w

flowcharts, so are still in speech marks. There are many


ie

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

An input in pseudocode can use a command word such


am

-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

num = INPUT & name


y

Pr

or you could combine the input and output, for example,


op

num = INPUT("Enter a number")


ity
C

Stop
rs
w

WORKED EXAMPLE 4.02 Figure 4.17: Flowchart-"Welcome".


ie

ve

y
ev

An algorithm needs to ask a user to enter their


op
ni

name. It should take their name as an input and then


R

welcome them by name, for example, "Welcome


ge

Sasha". WORKED EXAMPLE 4.03


w
ie
id

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

write it on its own line.


Step 1: Ask them to enter their name.
OUTPUT "Enter your name"
-C

Step 2: Take their name as input.


s
es

INPUT name
Step 3: Welcome them by their name.
y

OUTPUT "Welcome" & name


Pr
op

Then identify what type of symbols these steps need.


ity
C

Step 1: Ask them to enter their name.


rs

Questions
w

Output
ie

ve

3 Identify the symbol for Start and Stop in a


ev

op
ni

flowchart.
R

4 Describe the stages in this flowchart.


C
e

w
g

ie
id

ev
br
am

-R

58
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

OUTPUT "Your MOD 10 MOD 3 = 1


Modulus

op
ni

number is" &


division (gives
R

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

Stop the whole


number)
am

-R
Figure 4.18: Flowchart-Enter a number. Table 4.1: Mathematical (arithmetic) operators.
-C

s
es
y

Pr

PRACTICAL ACTIVITY 4.02


op

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

Draw a flowchart for this algorithm.


C
ge

3 An algorithm needs to tell a joke. It should


w

output the joke, and let the user give an Flowcharts


ie
id

answer, before outputting the actual answer. Processes are in rectangular boxes.
ev
br
am

Create a pseudocode algorithm for the


-R

problem.
Process
-C

s
es
y

Figure 4.19: Process box.


Processes
Pr
op

ity
C

A process is an action that is performed. In a program


it usually means that a change is being made to
rs
w

something. If you have a number stored in a variable,


ie

ve

you could change this; you might add something to it,


y
ev

op
ni

or subtract from it.


R

C
e

w
g

ie
id

ev
br
am

-R

59
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

it, and it was added to


itself, so 16 + 16 = 32) value in answer.

ity
C

Putting a value into a variable is called assignment.

rs
Pseudocode
w
ie

ve
Processes use the same structure as in a flowchart, but

y
ev

KEY WORD without the box. Therefore this:

op
ni
R

assignment: giving a variable a value

C
ge

w
newNumber = 3 * 8

ie
id

In this process box two numbers are multiplied together

ev
br

and stored in a variable. Figure 4.22: Assignment in process box.


am

-R
would become this:
-C

newNumber = 3 * 8
s
es

newNumber = 3 * 8
y

Pr
op

Figure 4.20: Assignment in process box.


WORKED EXAMPLE 4.04
ity
C

Follow this flowchart. What does it do? A program needs to ask a person’s age, then tell them
rs
w

how old they will be in 50 years.


ie

ve

A
y
Start
Draw a flowchart for the algorithm.
ev

op
ni

Start by identifying the steps required.


R

Step 1: Ask them to enter their age.


ge

OUTPUT "Enter a INPUT num2


w

number"
Step 2: Take the age as input.
ie
id

ev
br

Step 3: Add 50 to their age.


am

-R

INPUT num1 answer = num1 / num2 Step 4: Output the new age.
Identify what type of symbols these steps need;
-C

s
es

Step 1: Ask them to enter their age.


y

OUTPUT "The result


OUTPUT "Enter a Output
Pr
op

second number" is" & answer

Step 2: Take the age as input.


ity
C

Input and store in a variable


rs
w

A Stop
Step 3: Add 50 to their age.
ie

ve

Add 50 to the variable and store the result


ev

Figure 4.21: Flowchart-Arithmetic calculation.


op
ni

Step 4: Output their new age.


R

Output variable
e

w
g

ie
id

ev
br
am

-R

60
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

INPUT age total = num1 + num2

rs
w
ie

ve

y
ev

op
ni

newAge = age + 50 OUTPUT num1 & "+"


R

& num2 & "=" total

C
ge

w
ie
id

OUTPUT "In 50

ev
total = num1 – num2
br

years you will be"


& newAge
am

-R
-C

Stop
s
OUTPUT num1 & "–"
es

& num2 & "=" total


Figure 4.23: Flowchart-New age calculator.
y

Pr
op

ity
C

Stop
rs
w

WORKED EXAMPLE 4.05


ie

ve

Figure 4.24: Flowchart-Arithmetic calculation.


y
Convert the flowchart into pseudocode.
ev

op
ni

Take each line and write it on a separate line.


R

OUTPUT "Enter you age" PRACTICAL ACTIVITY 4.03


ge

INPUT age 1 Convert the algorithm in Question 8 into


ie
id

newAge = age + 50 pseudocode.


ev
br

OUTPUT "In 50 years your will be" & newAge 2 An algorithm needs to take 3 numbers as
am

-R

input. Add them together and output the


result. Then divide the total by the quantity
-C

of numbers and output the average.


s
es

Questions Draw a flowchart for this algorithm.


y

Pr

3 Convert your algorithm from part 2 into


op

5 What is a process?
pseudocode.
ity
C

6 What action does the operator ^ process?


7
rs

Write a statement to multiply 3 by 7.


w
ie

ve

8 Describe the stages in this flowchart.


y
ev

op
ni
R

C
e

w
g

ie
id

ev
br
am

-R

61
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

comparison: comparing two items of data


resulting in true or false

rs
w
ie

ve
selection: use of a conditional statement to Figure 4.25: Selection box.

y
ev

decide a course of action or which section of

op
ni

code to run
R

There are two options from a selection box: True

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

> Greater than 10 > 5 is true


y

No
Pr
op

5 > 10 is false
Figure 4.26: Selection box with options.
ity

<
C

Less than 10 < 5 is false


rs
w

5 < 10 is true Follow this flowchart. What does it do?


ie

ve

>= Greater than 10 >= 5 is true


y
ev

op
ni

or equal to Start
10 >= 10 is true
R

5 >= 10 is false
ge

INPUT age
w

<= Less than or 10 <= 5 is false


ie
id

equal to
ev
br

10 <= 10 is true
am

-R

5 <= 10 is true Is age >= Yes OUTPUT "You are old


18? enough"
= Equals to 10 = 5 is false
-C

s
es

10 = 10 is true No
y

Pr

!= Not equal to 10 != 10 is false


op

OUTPUT "You are too


ity

5 <> 10 is true
C

or young"
rs
w

<>
ie

ve

y
ev

Stop
Table 4.2: Comparison operators.
op
ni
R

Figure 4.27: Flowchart with selection box.


C

Either side of the operator can be a number or


e

a variable.
w
g

ie
id

ev
br
am

-R

62
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

If you have multiple options, then you will need multiple

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

IF cost >= 100 THEN

ity
Yes
C

Is age <
10? OUTPUT "That is far too expensive"

rs
w

ELSEIF cost >= 80 THEN


ie

ve
OUTPUT "That’s a bit too expensive"

y
No
ev

op
ni
ELSEIF cost >= 60 THEN
R

OUTPUT "That is reasonable"

C
ELSEIF cost >= 40 THEN
ge

Is age < Yes

w
20? OUTPUT "That looks like a good deal"

ie
id

ELSE

ev
br

No OUTPUT "I think that is too cheap"


am

-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

then it goes straight to another decision, and asks


op

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

to show where it ends.


w

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

IF age > 18 THEN CASE 2: OUTPUT "You chose 2"


OUTPUT("You are old enough") CASE 3: OUTPUT "You chose 3"
-C

ENDIF A CASE statement can perform comparisons other than


es

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

IF age > 18 THEN films"


ie

ve

OUTPUT("You are old enough")


y

CASE (age >= 12 and age < 15): OUTPUT "You


ev

op

ELSE
ni

can watch PG and PG12 films"


R

OUTPUT("You are not old enough")


C

CASE (age >= 15 and age < 18): OUTPUT "You


ENDIF
e

can watch PG, PG12 and 15 rated films"


w
g

ie
id

ev
br
am

-R

63
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

OUTPUT "Invalid symbol"

ity
C

rs
w

WORKED EXAMPLE 4.06


ie

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

output which is the largest.

C
Output second number
ge

Draw a flowchart for the algorithm.

w
ie
id

Start
Start by identifying the steps required.

ev
br

Step 1: Ask the user to input two numbers.


am

-R
OUTPUT "Enter
Step 2: Input two numbers.
two numbers"
-C

Step 3: Check if the first number is larger than the


s
es

second.
y

Input num1,
Pr

Step 4: Output the first number if it is.


op

num2

Step 5: Output the second number if it isn’t.


ity
C

Identify what type of symbols these steps need.


rs
w
ie

ve

Yes
Step 1: Ask the user to input two numbers. Is num1 OUTPUT num1
y
> num2?
ev

Output
op
ni
R

Step 2: Input two numbers.


C

No
ge

Input the store in a variable


w
ie
id

Step 3: Check if the first number is larger than the OUTPUT num2
ev

second.
br

If first > second


am

-R

Step 4: Output the first number if it is.


-C

Stop
s

Output first number


es
y

Figure 4.29: Flowchart-Compare two numbers.


Pr
op

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

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

IF num1 > num2 THEN

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

9 How many arrows must come out of a selection


-R No

symbol in a flowchart?
-C

Is mark Yes
s

10 Define selection. >45?


OUTPUT "Pass"
es

11 Is 956 >= 856?


y

Pr

No
op

12 Is 123 != 123?
13 Is 55 = 66?
ity

OUTPUT "Fail"
C

rs
w

PRACTICAL ACTIVITY 4.04


ie

ve

y
ev

Stop
op
ni

1 Describe the stages in the flowchart opposite.


R

2 Convert the flowchart in part 1 into Figure 4.30: Flowchart with comments.
ge

pseudocode.
w
ie
id

3 An algorithm needs to take three numbers


ev
br

as input, and output the smallest.


Loops
am

-R

Draw a flowchart for this algorithm.


A loop means repetition; doing the same thing several
-C

4 Convert your flowchart from part 3 into


s

times. Its technical term is iteration.


pseudocode.
es
y

Pr

5 An algorithm needs to take two numbers


op

KEY WORD
as input, then subtract the smallest from
ity
C

the largest. iteration: a loop to repeat a section of code


rs

for a fixed number of times or until a required


w

Create a pseudocode algorithm for the


outcome is achieved
ie

ve

problem using an IF statement.


y
ev

op
ni

6 Replace the IF statement in task 5 with a


R

SELECT CASE statement.


C
e

w
g

ie
id

ev
br
am

-R

65
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

FOR loop: a count-controlled loop

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

condition-controlled loop: a loop that runs FOR counter = 0 to 9


R

based on a condition, not the number of times

C
OUTPUT counter
it will run
ge

w
NEXT counter

ie
id

ev
br

Count controlled WORKED EXAMPLE 4.08


am

-R
Flowcharts An algorithm needs to ask the user to input the 10
marks of its students, and then output the average.
-C

This type of loop needs a counter to keep track of the


s

number of repetitions you have done (the number of


es

Draw a flowchart for the algorithm.


loops). The counter will be a variable. Before you start
y

Pr

Start by identifying the steps required.


op

the loop, the counter needs to be set to the starting value


(usually 0 or 1). Then inside the code that loops, you Step 1: Repeat 10 times.
ity
C

need to increment the counter (this means add 1 to it).


Step 2: Ask the user to input a mark.
rs
w

Step 3: Input the mark.


ie

ve

Start
y
Step 4: Add the mark to a total.
ev

op
ni

Step 5: After all the marks are entered, calculate


R

num = 0
C

the average.
ge

Step 6: Output the average.


w
ie
id

Identify what type of symbols these steps need.


ev

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

Step 3: Input the mark.


y

Pr

Figure 4.31: Flowchart with counter.


op

Input and store in variable


Step 4: Add the mark to a total.
ity
C

In this example, the counter variable is num. num is set to


Add input to total
start at 0. Following the arrows, it keeps increasing by 1
rs
w

until num is greater than 5, at which point the algorithm Step 5: After all marks are entered, calculate the
ie

ve

average. Total divided by 10


y

stops.
ev

op
ni

Step 6: Output the average.


R

Output
C
e

w
g

ie
id

ev
br
am

-R

66
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

Yes OUTPUT counter


ve
< 0?

y
ev

INPUT mark

op
ni

average = total / 10 Yes


R

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

-R 2 Convert the flowchart from part 1 into


Stop
pseudocode.
-C

Figure 4.32: Flowchart-Calculating average. 3 An algorithm needs to output the 12-times


es

table using iteration.


y

Pr
op

Create a flowchart for the algorithm.


ity
C

WORKED EXAMPLE 4.09 4 An algorithm needs to ask the user how


rs
w

many numbers to output. The algorithm


Convert the flowchart into pseudocode.
ie

ve

should then print out the numbers from 1 to


y
ev

Write each line separately. Replace the count and that number input.
op
ni

selection with a FOR loop.


R

Create a pseudocode algorithm for


C

total = 0 the problem.


ge

FOR count = 0 TO 10
ie
id

OUTPUT "Enter a mark"


ev
br

INPUT mark
Condition controlled
total = total + mark
am

-R

A condition-controlled loop has the same structure as


NEXT count
a count-controlled loop, but it doesn’t need a counting
-C

average = total / 10
s

variable, because something else is controlling it. This


es

OUTPUT average could be controlled by an input from the user (you could
y

loop until it is a valid input), or until a specific number


Pr
op

is greater than another.


Questions
ity
C

Flowchart
rs

14 What is iteration?
w

The flowchart looks similar to a count controlled due


ie

ve

15 What is a count-controlled loop? to the condition, but it would be missing the variable
y
ev

16 When is it appropriate to use a count-


op
ni

counter and does not run a set number of times.


controlled loop?
R

17 What three elements do you need in a count-


e

controlled loop?
g

ie
id

ev
br
am

-R

67
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

runs while the condition is true

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

Stop WHILE age < 18

w
ie
id

INPUT age
Figure 4.34: Flowchart with condition-controlled loop 1.

ev
br

ENDWHILE
am

The following flowchart is an example of a REPEAT


-R
In this example, the loop continues until loop is
UNTIL loop. This is a type of condition controlled loop. greater than 100.
-C

The code inside the loop will always run once because
s

value = 1
es

the condition is at the end of the code. So the statement


y

value = value * 2 repeats until the value is >= 100. WHILE value <= 100
Pr
op

value = value + value


ity

Start
C

ENDWHILE
rs
w
ie

ve

value = 1
y
WORKED EXAMPLE 4.10
ev

op
ni

An algorithm needs to continually ask the user to


R

input numbers, and add them to a total, until the total


ge

is more than 100, then output the total.


w

value = value * 2
ie
id

Draw a flowchart for the algorithm.


ev
br

Start by identifying the steps required.


am

-R

Step 1: Repeat until the total is more than 100.


Is value >= No
-C

Ask the user to input a number.


s

100? Step 2:
es

Step 3: Input the number.


y

Pr
op

Yes
Step 4: Add the number to the total.
ity
C

Stop Step 5: When total is more than 100, output the


total.
rs
w

Figure 4.35: Flowchart with condition-controlled loop 2.


ie

ve

Identify what type of symbols these steps need.


y
ev

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

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

Process – add number total

ity
C

Step 5: When total is more than 100, output the


total = 100
total.

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

Is total > No OUTPUT "Enter a


-R
100? number" OUTPUT "It took"
-C

& counter &


s

Yes "numbers" total = total – number


es

INPUT number
y

Pr
op

OUTPUT total
counter = counter + 1
Stop
ity
C

total = total + number


rs

Stop
w

Figure 4.37: Flowchart with control loop.


ie

ve

y
Figure 4.36: Flowchart-Totalling number with control
ev

op
ni

loop. 2 Convert the flowchart into pseudocode.


R

3 An algorithm needs to ask the user to


ge

input a number between 100 and 200.


w

Keep asking the user for the number until


ie
id

WORKED EXAMPLE 4.11 it is valid.


ev
br

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

total = 0 has to guess what this number is. They


es

keep guessing until they get it correct.


WHILE total < 100
y

The algorithm then outputs the number of


Pr
op

OUTPUT "Enter a number" guesses the user took.


INPUT number
ity
C

total = total + number Write a pseudocode algorithm for


rs
w

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

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

Command Description Example Stop


R

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

-R written without the boxes.


characterNum
in the string.
-C

WORKED EXAMPLE 4.12


es

mid(string, Returns mid("Hello",1,2)


y

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

Draw a flowchart for the algorithm.


numChars) startingChar
ie

ve

in the string Start by identifying the steps required.


y
ev

op
ni

upper(string) Converts upper("Hello") Step 1: Ask the user to input a word.


R

string to would give


C

Step 2: Input a word and store it in a variable.


ge

capitals "HELLO"
w

Step 3: Count the number of letters in the word.


ie
id

lower(string) converts lower("Hello")


string to would give Step 4: Set a counter to start with the letter at
ev
br

lowercase "hello" position 0.


am

-R

Step 5: Increase the counter by 2 each time through


Table 4.3: String manipulators. the loop.
-C

s
es

Step 6: Output the letter of the counter in the loop.


y

Pr

Step 7: Loop until the counter is greater than or


op

equal to the number of letters


ity
C

rs
w
ie

ve

y
ev

op
ni
R

C
e

w
g

ie
id

ev
br
am

-R

70
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

Find length of the word


counter = counter + 1

rs
w

Step 4: Set a counter to start with letter 0.


NEXT counter
ie

ve
Process set counter to be 0

y
ev

op
ni

Step 5: Increase the counter by 2 each time through


R

the loop.

C
Question
ge

Process, counter = counter + 2

w
19 What number is the first character in a string?

ie
id

Step 6: Output the letter of the counter in the loop.

ev
br

Get mid at position counter


PRACTICAL ACTIVITY 4.07
am

Step 7: Loop until the counter is greater than the


-R
number of letters. 1 Describe the stages in the flowchart in
-C

Loop until counter > length Figure 4.40.


es
y

2 An algorithm is needed to ask the user


Pr
op

Start
how many letters they want to enter. The
program should then ask the user to enter
ity
C

OUTPUT "Enter a that many letters, concatenate them and


rs
w

word"
output the result.
ie

ve

y
Draw a flowchart for the algorithm.
ev

INPUT word
op
ni

An algorithm takes the date of a month a


R

3
U

user was born, the name of the month, and


ge

wordLength = length(word)
w

the year. It creates a date of birth in the form


ie

DD/MM/YYYY and outputs this.


id

ev
br

counter = 0
Create a pseudocode algorithm for
am

-R

the problem.
-C

Is counter > No OUTPUT


es

wordLength? mid(wordLength,counter,1)
y

Pr
op

Yes
counter = counter + 2
ity
C

Stop
rs
w
ie

ve

Figure 4.39: Flowchart with mid command.


y
ev

op
ni
R

C
e

w
g

ie
id

ev
br
am

-R

71
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

function: a separate piece of code that has an

rs
w

first = mid(name,0,1) identifier and performs a task; it can be called


ie

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

Figure 4.41: Subroutine identifier.


ity
C

Stop
In this example, the subroutine is called OutputText.
rs
w

Note that the symbol for a subroutine is similar to a


ie

ve

Figure 4.40: Flowchart. process box but has a pair of parallel vertical lines at
y
ev

op
ni

each side. The algorithm starts at Start, the process box


R

tells the flowchart to go to Output. The output “Hello”


C

runs, and Stop tells the algorithm to go back to the first


ge

Subroutines
w

flowchart, where it runs Stop.


ie
id

A subroutine is a set of instructions that are independent.


ev
br

Start Output
They have an identifier (a name) and can be called from
am

-R

other parts of the program. When it finishes running,


control passes back to where it was called from.
-C

s
es

Sub: Output OUTPUT


Sub: Output
"Hello"
KEY WORD
y

Pr
op

subroutine: a set of instructions that have


ity
C

an identifier and that are independent from


the code; it is called from another part of the
rs
w

Stop Stop
program and returns control when it was finished
ie

ve

y
ev

op
ni

Figure 4.42: Flowchart calling subroutine.


R

There are two types of subroutine, a procedure and


C

a function. A procedure performs a task and returns


e

w
g

ie
id

ev
br
am

-R

72
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

outputMessage(). The brackets identify it as a value

s
es
subroutine. The procedure outputs the words "Hello
World" and then returns control.
y

Pr
op

PROCEDURE outputMessage()

ity
C

OUTPUT "Hello World" Sub:


OUTPUT total
calculate(number)

rs
w

ENDPROCEDURE
ie

ve

y
A procedure is called by using the name of the
ev

op
ni

procedure, for example,


R

C
INPUT x Stop Stop
ge

w
outputMessage()
Figure 4.43: Flowchart calling calculate subroutine.

ie
id

OUTPUT "Finished"

ev
br

Procedures are declared at the top of the program, and


am

-R Start calculate(value)
the main program beneath all the function calls.
-C

Parameters
es

You can send data to a subroutine; this data is called a


y

Pr

total = total +
op

parameter. INPUT number value


ity
C

KEY WORD
rs
w
ie

ve

parameter: a piece of data that is sent to a


y
ev

subroutine
op
ni

Sub: OUTPUT total


R

calculate(number)
C
ge

Flowchart
ie
id

In this example, the main flowchart (in Figure 4.44) calls


ev
br

the subroutine (in Figure 4.43), calculate and sends it Stop


the value in number. The function calculate renames
am

-R

this as value. It adds this to total and outputs it


Is number No
before returning control to the main program.
-C

> 10?
es
y

Pr
op

Yes
ity
C

rs
w
ie

ve

Stop
y
ev

op
ni
R

Figure 4.44: Flowchart with condition loop calling calculate


C

subroutine.
e

w
g

ie
id

ev
br
am

-R

73
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

named value and added to total before being output. calculate(number)

s
es
y

WORKED EXAMPLE 4.14

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

calls a subroutine to output the number to the power

op
ni

of itself. a Calculate the parameter to the power of itself


R

C
Draw a flowchart for this algorithm. Process number ^ number
ge

b Output the result.

w
Start by identifying the steps required.

ie
Output result
id

Step 1: User inputs a number.

ev
br

Step 2: Set the counter to 1. Draw the flowchart for the subroutines.
am

Step 3: Loop until the counter is the number


-R
multiply(number) powerOff(number)
entered.
-C

Step 4: If the number is even, call subroutine.


s
es

Step 5: Else if the number is odd, call subroutine. result = number * number result = number ^ number
y

Pr

Create a subroutine taking number as parameter.


op

a Multiply the parameter by itself.


ity
C

OUTPUT result OUTPUT result


b Output the result.
rs
w

Create a subroutine taking number as parameter.


ie

ve

a Calculate the parameter to the power of itself.


y
Stop Stop
ev

op
ni

b Output the result.


R

Figure 4.45: Subroutine flowcharts.


U

Identify what type of symbols these steps need.


ge

Step 1: User inputs a number Draw the flowchart for the main program.
ie
id

Input number store in variable


Start
ev
br

Step 2: Set counter to 1.


am

Process counter = 1
-R

INPUT number

Step 3: Loop until counter is the number entered.


-C

counter = 1
s

Loop until counter = number


es

counter = counter + 1
Step 4: If the number is even call subroutine
y

Pr

If counter is even call multiply()


op

Is counter > No Is counter No


powerOf(number)
Step 5: Else if the number is odd call subroutine
ity

number? mod 2 = 0?
C

else call powerof()


rs
w

Yes Yes
Create a subroutine taking number as parameter.
ie

ve

Stop multiply(number)
multiply(number)
y
ev

op
ni

a Multiply the parameter by itself.


R

Process number * number Figure 4.46: Flowchart calling multiple subroutines.


C
e

w
g

ie
id

ev
br
am

-R

74
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

result = number ^ number Input choice


ie

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

21 Why are subroutines used in programs? Stop


op
ni

22 What is a parameter?
R

Figure 4.48: Flowchart b.


C
ge

PRACTICAL ACTIVITY 4.08


ie
id

sub2
ev
br

1 Describe what the flowcharts a to d show.


am

-R

Start

OUTPUT "You
-C

chose 1"
es
y

sub1
Pr
op

ity
C

Stop
rs
w

Stop Figure 4.49: Flowchart c.


ie

ve

y
ev

Figure 4.47: Flowchart a.


op
ni
R

C
e

w
g

ie
id

ev
br
am

-R

75
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

INPUT num another construct

s
es
y

Pr
op

In this example (see the flowchart and the pseudocode)


result = num * 2 there is a nested loop; a WHILE loop inside a

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

3 Create a program that asks the user whether


they want to make word uppercase or
y

continue =
Pr
op

Stop upper(INPUT)
lowercase. Create a procedure to turn a
word uppercase and output it, and a second
ity
C

procedure to turn a word into lowercase and


rs
w

output it. Call the appropriate procedure for Is continue


No OUTPUT "Invalid option.
ie

ve

= "YES"? or
which choice the user inputs. continue ! = Loop again?"
y
ev

"NO"?
op
ni

Draw a flowchart for the algorithm. Yes


R

4 Convert the flowchart from part 3 into


ge

pseudocode.
w

Figure 4.51: Nested loop.


ie
id

continue = False
ev
br

WHILE continue = False


am

-R

Nested construct OUTPUT "Loop again?"


-C

A nested construct is one construct (selection or iteration)


s

continue = upper(INPUT)
inside of another construct (selection or iteration). For
es

WHILE continue != "YES" or continue !=


example, this could be a loop inside another loop, or a
y

Pr

"No"
op

selection statement inside a loop, or a selection statement


inside another selection statement. You can even have OUTPUT "Invalid option. Loop again?"
ity
C

multiple nested loops, with several selection statements continue = upper(INPUT)


rs
w

inside one loop. There is no end to the possibilities. ENDWHILE


ie

ve

ENDWHILE
y
ev

op
ni
R

C
e

w
g

ie
id

ev
br
am

-R

76
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

Step 2: Loop through all 10 results for each student.

ity
Is
C

Step 3: Ask for the student’s result. No result = 0 Is result


Yes
studentNumber
total = 0 > 10?
Step 4: Input the student’s result.
rs
w

> 100?
ie

ve
Step 5: Add the result to their total.

y
Yes No
ev

Step 6: Output the total at the end of each student’s

op
ni

results. OUTPUT "Enter" &


R

Stop studentNumber & "Is

C
Identify what type of symbols these steps need. result" result
ge

w
Step 1: Loop through all 100 students.

ie
id

Count-controlled loop INPUT

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

Step 4: Input the student’s result.


y

Pr
op

Input and store the result


result = result + 1
ity
C

Step 5: Add the result to their total.


rs

Process total = total + result


w
ie

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

WORKED EXAMPLE 4.17


ev
br
am

Convert the flowchart into pseudocode.


-R

Each loop is count controlled so replace with FOR loops.


-C

FOR studentNumber = 1 to 101


es

total = 0
y

Pr
op

FOR result = 1 to 11
OUTPUT "Enter " & studentNumber & "'s result " & result
ity
C

INPUT studentResult
rs
w

total = total + studentResult


ie

ve

NEXT result
ev

op
ni

OUTPUT "Total is" & total


R

ENDWHILE
e

w
g

ie
id

ev
br
am

-R

77
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

continue asking this until they say Yes.

Pr
op

Then the program should ask them how

ity
C

many numbers they want to add together.


The program should input that quantity of

rs
w

numbers, add them together and output


ie

ve
Finish
the result.

y
ev

op
ni

Draw a flowchart for the algorithm. Figure 4.53: Maze 4.


R

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

position in the alphabet. For example, ‘a’


FD2
would be output once, ‘b’ would be output
am

-R RT90
twice, and so on.
FD4
-C

RT90
s

Create a pseudocode algorithm for


es

the problem. FD5


y

LT90
Pr
op

FD1
RT90
ity
C

Editing an algorithm/flowchart FD2


rs
w

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

Edit the algorithm so it visits the yellow box and then


To do this, you need to work out what the algorithm
R

the Finish without visiting the green box.


C

does first. Do this by using test data to run the


ge

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

algorithm did, and what you need to make it do.


ev
br

FD2
RT90
am

-R

FD4
-C

Continue the algorithm from this point. There are


s
es

many different ways you could go, for example:


y

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

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

Figure 4.55: Flowchart-Total 10 numbers.

ev
br

username = username &


am

mid(colour,0,3)
-R
Change the flowchart so it inputs 20 numbers and
adds them together.
-C

25 This algorithm asks the user to input 50 numbers. It


es

OUTPUT
username finds the smallest number and multiplies it by itself
y

Pr

before outputting it.


op

smallest = 9999
ity
C

Stop
FOR counter = 0 to 50
rs
w

Figure 4.54: Flowchart-Create a username. INPUT number


ie

ve

y
ev

If number < smallest THEN


op
ni

Change the algorithm so that is gets the last three


smallest = number
R

letters of the colour instead of the first three.


C

ENDIF
ge

The algorithm can stay the same up to where the


w

letters are taken from the colour. This box was NEXT counter
ie
id

originally: username = username & mid(colour,0,3)


ev

answer = smallest * smallest


br

Instead it needs to get the last three, so it needs


am

OUTPUT answer
-R

changing to:
Change the algorithm so it finds the largest number
-C

username = username & mid(colour,


s

and multiplies this by itself before outputting it.


es

length(colour)–4,3)
y

Pr
op

Correcting an algorithm/
ity
C

flowchart
rs
w
ie

You may need to find an error in an algorithm such as


ve

a flowchart. To do this, you need to follow each step of


ev

op
ni

the algorithm, performing the actions it instructs to find


R

where it goes wrong.


C
e

w
g

ie
id

ev
br
am

-R

79
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

num1 > num3? num2 > num1?


R

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

total = num1 + total = num1 + total = num3 + total = num3 +


C

num2 num3 num2 num1


rs
w
ie

ve

y
ev

op
ni
R

OUTPUT total
ge

w
ie
id

ev
br

Stop
am

-R

Figure 4.56: Flowchart with error.


-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

80
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

= 10 + 5 Is num1 > num3? No total = num3 + num2 = 5 + 10


R

C
OUTPUT 15 – correct OUTPUT 15 – correct
ge

w
ie
id

ev
br

PRACTICAL ACTIVITY 4.10


am

-R
1 The algorithm should ask the user how many letters they want to enter, then let them enter that many
-C

letters. All the letters should be concatenated and output.


es

Find the error(s) in this algorithm.


y

Pr
op

Start
ity
C

rs
w

OUTPUT "Enter the


ie

number of letters"
ve

y
ev

op
ni
R

INPUT number
C
ge

w
ie
id

counter = 0
ev
br
am

-R
-C

Is count > No word = word


es

INPUT letter
number? & letter
y

Pr
op

Yes
ity
C

OUTPUT word
rs
w
ie

ve

y
ev

Stop
op
ni
R

Figure 4.57: Flowchart with errors.


C
e

w
g

ie
id

ev
br
am

-R

81
-C

s
es

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

Find the error(s) in the algorithm.

s
es
total = 1
y

previousNumber = 0

Pr
op

WHILE total < 1000

ity
C

INPUT number

rs
w

total = total + (number – previousNumber)


ie

ve
previousNumber = number

y
ev

ENDWHILE

op
ni
R

C
ge

w
REFLECTION

ie
id

What steps did you follow to plan an algorithm?

ev
1
br
am

2 How did you find errors in an algorithm?


-R
3 How did you work out how to edit an algorithm?
-C

s
es
y

Pr
op

ity
C

EXAM-STYLE QUESTIONS
rs
w

1 Identify two ways that an algorithm can be expressed. [2]


ie

ve

y
ev

2 Name the actions for these flowchart shapes.


op
ni
R

C
ge

w
ie
id

Figure 4.58: Flowchart symbols.


ev
br

[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

Copyright Material - Review Only - Not for Redistribution


ity
rs
ve

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

Is num > Yes


OUTPUT num * num
ie

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

Figure 4.59: Flowchart.


ie

ve

y
ev

op
ni

4 Change this algorithm so it outputs the 9-times table.


R

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

Figure 4.60: Flowchart.


op
ni
R

[3]
e

w
g

ie
id

ev
br
am

-R

83
-C

s
es

Copyright Material - Review Only - Not for Redistribution

You might also like