## 8.2: Understanding Algorithms

## Exam Board:

## Eduqas / WJEC

## Specification:

## 2020 +

## What is an algorithm?

An algorithm is a set of instructions, presented in a logical sequence.

â€‹

In an exam you may be asked to read and understand an algorithm that has been written. To prove your understanding you may be asked to respond by actions such as listing the outputs of the algorithm, correcting errors or identifying an error within it.

â€‹

Programmers create algorithm designs as a method of planning a program before writing any code. This helps them to consider the potential problems of the program and makes it easier to start creating source code.

There are two main methods of defining algorithms:

## Defining Algorithms - Pseudocode & Flowcharts

## Pseudocode

Pseudocode is not a specific programming language but a more general method of describing instructions. It should be unambiguous, and it should not resemble any particular kind of programming language (e.g. Python or Java), so it can theoretically be turned into working code in any language.

â€‹

Generally, pseudocode can be written in any way that is readable and clearly shows its purpose. However, the Eduqas exam board advises that pseudocode for the programming exam should follow the conventions below:

Annotation

{Write your comment in curly brackets}

â€‹

Define data type

price is integer

firstname is string

â€‹

Declare a variable's value

set price = 100

set firstname = "Marcella"

â€‹

Input / output

output "Please enter your first name"

input firstname

Selection (must have indentation)

if firstname = "Steven" thenâ€‹

output "Hello" + firstname

elif firstname = "Steve" then

output "Please use full name"

else output "Who are you?"

end if

â€‹

Iteration (while loop)

while firstname != "Steven"

output "Guess my name."

input firstname

repeat

Iteration (for loop)

for i in range 10

input item

next i

â€‹

Define a subroutine

Declare Sub1

[Subroutine content indented]

End Sub1

â€‹

Call a subroutine

call Sub1

## Flowcharts

A flowchart can be used to visually represent an algorithm. The flowchart symbols are:

## Algorithm Examples

Below are two different methods for representing the same algorithm - a program to encourage people to buy items cheaply at a supermarket.

The program allows the price of items in a supermarket to be entered until the total reaches 100. The total price and the number of items entered are tracked as the program loops. Once the total reaches 100 or more, an if statement checks how many items have been entered and a different message is printed if there are 20 or more items, 30 or more items or less than 20 items.

Pseudocode

{This is a program to see how many items you can buy in a supermarket

before you spend over Â£100}

â€‹

total is integer, itemsentered is integer, itemprice is integer

set total = 0

set itemsentered = 0

â€‹

while total < 100

output "enter the price of the next item"

input itemprice

total = total + itemprice

itemsentered = itemsentered + 1

repeat

if itemsentered >= 20 then

output "You are on your way to saving money."

elif itemsentered => 30 then

output "You're a real money saver."

else output "Look for better deals next time."

end if

Flowchart

## Reading Algorithms

In an exam you may be asked to read an algorithm and prove your understanding, most commonly by listing the outputs.

â€‹

Start from the first line and follow the program line by line, recording the value of variables as you go.

â€‹

When you encounter a for loop, repeat the indented code as many times as stated in the range.

Example Algorithm:

Start NewProgram

number is integer

maxvalue is integer

â€‹

input maxvalue

â€‹

for i = 1 to maxvalue

output (i * i)

???????

â€‹

output 'program finished'

â€‹

End NewProgram

Example Questions:

1. List the outputs produced by the algorithm if the 'maxvalue' input is 5.

â€‹

2. State the code that has been replaced by '???????' and what the code's purpose is.

Example Answers:

1.

Outputs:

1

4

9

16

25

program finished

2.

Missing Code: next i

Purpose: Moves the loop to the next iteration.

## Previous Algorithm Exam Questions

2014 Exam Algorithm:

set counter = 0

output "Before the loop"

â€‹

repeat

set counter = counter + 1

output "Count is" counter

until counter = 3

â€‹

output "Loop has ended"

2014 Exam Question:

Write down all the outputs produced by the algorithm.

2015 Exam Algorithm:

Total is integer

Mean is real

i is integer

â€‹

startmainprog

â€‹

set Total = 0

for i = 1 to 5

set Total = Total + i

output "Total is", Total

next i

â€‹

set Mean = Total / 5

output "Mean is", Mean

â€‹

endmainprog

2015 Exam Question:

2016 Exam Algorithm 1:

M is integer

P is integer

i is integer

â€‹

startmainprog

â€‹

input M

â€‹

for i = 1 to 4

set P = i * M

output P

endfor

â€‹

endmainprog

2016 Exam Question 1:

Write down all the outputs produced by the algorithm when the value of M input is 3.

Write down all the outputs in the correct order produced by the algorithm.

2016 Exam Algorithm 2:

Total is ? {stores the total of the numbers input}

Mean is ? {stores the mean of the numbers input}

Count is ? {stores the loop control value}

â€‹

startmainprog

â€‹

set Total = 0 {initialise variables}

set Count = 0

â€‹

repeat

set Count = Count + 1

set Total = Total + Count

until Count = 20

â€‹

output "The total is ", Total

set Mean = Total / 20

output "mean is ", Mean

â€‹

endmainprog

2017 Exam Algorithm 1:

HighNum is integer

Total is integer

i is integer

â€‹

startmainprog

â€‹

input HighNum

â€‹

set Total = 0

â€‹

if HighNum < 1 then

output "Number not valid"

else

for i = 1 to HighNum

set Total = Total + i

output Total

endfor

endif

â€‹

endmainprog

2016 Exam Question 2:

State, giving a reason for each, the most suitable data type for the variables below:

a. Mean

b. Count

2017 Exam Question 1:

Write down all the outputs in the correct order produced by the algorithm for the input below:

a. Input is 0

b. Input is 4

2017 Exam Algorithm 2:

IF (X > 5) AND (Y < 7) THEN

OUTPUT true

ELSE

OUTPUT false

ENDIF

2017 Exam Question 2:

Write down the output for the following values of X and Y:

a. X = 2 and Y = 4

b. X = 5 and Y = 4

c. X = 6 and Y = 6

2019 Exam Algorithm:

Start addProc

number is integer

a is integer

total is integer

â€‹

if i = 1 to 5

output "Please enter next number"

input number

a = a + number

next i

â€‹

total = a

output "The total = ", total

â€‹

End addProc

2019 Exam Question:

1. Identify the error in this algorithm and suggest a suitable change to this code to address the error.

â€‹

2. Suggest a suitable change to address the inefficient use of memory in this algorithm.

## Questo's Questions

8.2 - Understanding Algorithms:

â€‹

Previous Exam Questions

1. Answer each of the previous exam questions on this page using pseudocode.