topic badge

12.09 Algorithms in code

Lesson

Introduction

Previously we have learnt how to represent an algorithm (a sequence of steps) using a flow chart diagram. Each step of an algorithm can also be represented by a set of worded instructions called a line of code. There are many different representations of code, called programming language. These include Python, Scratch, Java, and C. We use programming language to communicate with computers to give them the instructions that we want them to perform.

A programming language usually consists of

  • Statements - which give an instruction for the action - written as capital letters

  • Expressions - which represent a value

Pseudocode and its common notations

Before translating an algorithm into a specific programming language, the steps of the algorithm can be written as pseudocode. Pseudocode is easy to read coding language that can’t be used by a computer but can be easy to follow for humans to check and make sure the program will work before writing into a specific computer language.

There is no strict set of standard notations for pseudocode, but some of the most widely recognised are:

  • \text{INPUT} - indicates a user will be inputting something

  • \text{OUTPUT} or \text{PRINT} - indicates that an output will appear on the screen

  • \text{SET} - assign an initial value to an expression

  • \text{IF - THEN - ELSE} - a decision (selection) in which a choice is made

Any instructions that occur inside a decision are usually indented.

This image shows a flow chart. Ask your teacher for more information.

This slgorithm which adds two numbers can be written in pseudocode as shown below.

1
START
2
SET a = 2
3
SET b = 3
4
PRINT a + b
5
END

Examples

Example 1

Look at the pseudocode below.

1
START
2
SET a = 73
3
SET b = 44
4
SET c = a + b
5
PRINT c
6
END

What value is printed at the end of the code?

Worked Solution
Create a strategy

Ass a and b to find the value of c.

Apply the idea
\displaystyle c\displaystyle =\displaystyle 73+44Add a=73 and b=44
\displaystyle =\displaystyle 117Evaluate

117 is printed at the end.

Example 2

Consider the rectangle shown in the image. The dimensions of the rectangle are W metres and L metres.

A rectangle with width labelled as W and length labelled as L.
a

What does the following pseudocode calculate?

1
START
2
SET W = 3
3
SET L = 6
4
SET A = W * L
5
PRINT A "square metres"
6
END

Note: the symbol * represents multiplication.

A
Volume
B
Units
C
Area
D
Perimeter
Worked Solution
Create a strategy

Consider how A is being calculated.

Apply the idea

The pseudocode PRINTs the value of A which is found by multiplying the length and width of the rectangle. This is how the area of the rectangle can be found.

The answer is option C.

b

What value is printed at the end of the code?

Worked Solution
Create a strategy

Follow the pseudocode using the given values of W and L.

Apply the idea

In step 2 W is set to equal 3, and in step 3 L is set to equal 6.

In step 4 we multiply these values together to find A:

\displaystyle A\displaystyle =\displaystyle 3\times 6Multiply W=3 and L=6
\displaystyle =\displaystyle 18Evaluate

In step 5 the following will be printed: 18\text{ square metres}

Idea summary

Common notations for pseudocode:

  • \text{INPUT} - indicates a user will be inputting something

  • \text{OUTPUT} or \text{PRINT} - indicates that an output will appear on the screen

  • \text{SET} - assign an initial value to an expression

  • \text{IF - THEN - ELSE} - a decision (selection) in which a choice is made

Make a decision

When a decision is to be made between two alternatives in code, we can use the \text{IF – THEN – ELSE} statement.

Examples

Example 3

Look at the pseudocode below:

1
START
2
SET a = 8
3
IF a / 2 IS integer
4
THEN PRINT "a is even"
5
ELSE PRINT "a is odd"
6
END IF
7
END

What statement is printed at the end of the code? Note: the symbol / represents division.

A
a \text{ is even}
B
a \text{ is odd}
Worked Solution
Create a strategy

Follow the pseudocode for the value of a.

Apply the idea
\displaystyle a\displaystyle =\displaystyle 8Step 2
\displaystyle \dfrac{a}{2}\displaystyle =\displaystyle 4Divide a by 2 for step 3

Since \dfrac{a}{2}=4 is an integer, the code would take us to Step 4 and print: "a is even".

So the answer is option A.

Idea summary

\text{IF – THEN – ELSE} statements are used for making a decision between two alternatives.

What is Mathspace

About Mathspace