Fundamentals of algorithms - AQADetermining the purpose of simple algorithms

Algorithms are step-by-step plans for solving problems. They can be designed using pseudo-code and flowcharts.

Part ofComputer ScienceComputational thinking and problem solving

Determining the purpose of simple algorithms

When given an , there are a number of ways to determine what the purpose of the algorithm is. Sometimes it is clear as the algorithm is simple; however, at other times it is useful to ‘dry run’ the algorithm to see what is taking place.

Dry running an algorithm means to assign the values to of an algorithm and to do any processing that takes place without translating it into code.

Trace tables

enable the variable values in an algorithm to be recorded as the algorithm is dry run. For example, using the times table algorithm below, a table could be created showing the value of the variables num and number as the program runs:

num ← USERINPUT
FOR number ← 1 TO 10 OUTPUT number * num
ENDFOR
NumberNumOutput
155
210
315
420
525
630
735
840
945
1050
Number1
Num5
Output5
Number2
Num
Output10
Number3
Num
Output15
Number4
Num
Output20
Number5
Num
Output25
Number6
Num
Output30
Number7
Num
Output35
Number8
Num
Output40
Number9
Num
Output45
Number10
Num
Output50

Visual inspection

Some algorithms follow a pattern that can be recognised. Many of these are referred to as and often follow a set pattern for searching for or sorting . Sometimes it is clear what this is just by looking at the . For example, the algorithm below follows a recognisable pattern for searching through each letter of a word and checking if the letter entered matches. This would be a useful decomposed part of a hangman game.

guess ← USERINPUT
FOR i ← 0 TO LEN(word) IF word[i] = guess THEN OUTPUT “found” ENDIF
ENDFOR