Determining the purpose of simple algorithms
When given an algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs., 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 鈥榙ry run鈥 the algorithm to see what is taking place.
Dry running an algorithm means to assign the values to variableA memory location within a computer program where values are stored. of an algorithm and to do any processing that takes place without translating it into code.
Trace tables
trace tableUsed when testing a program to record changes in variable values as code executes. 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
Num | Number | Output |
1 | 5 | 5 |
2 | 10 | |
3 | 15 | |
4 | 20 | |
5 | 25 | |
6 | 30 | |
7 | 35 | |
8 | 40 | |
9 | 45 | |
10 | 50 |
Num | 1 |
---|---|
Number | 5 |
Output | 5 |
Num | 2 |
---|---|
Number | |
Output | 10 |
Num | 3 |
---|---|
Number | |
Output | 15 |
Num | 4 |
---|---|
Number | |
Output | 20 |
Num | 5 |
---|---|
Number | |
Output | 25 |
Num | 6 |
---|---|
Number | |
Output | 30 |
Num | 7 |
---|---|
Number | |
Output | 35 |
Num | 8 |
---|---|
Number | |
Output | 40 |
Num | 9 |
---|---|
Number | |
Output | 45 |
Num | 10 |
---|---|
Number | |
Output | 50 |
Visual inspection
Some algorithms follow a pattern that can be recognised. Many of these are referred to as standard algorithmA commonly agreed programming solution to a specific problem. and often follow a set pattern for searching for or sorting dataUnits of information. In computing there can be different data types, including integers, characters and Boolean. Data is often acted on by instructions.. Sometimes it is clear what this is just by looking at the pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding.. 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 鈥渇ound鈥
ENDIF
ENDFOR