7.3 Explaining the Purpose of an Algorithm

Question Bank · 5 Questions

Objectives: Students should be able to —

  • 1 Explain the processes involved in an algorithm.
  • 2 Explain the purpose of an algorithm.

Reading Algorithms & Explaining Their Purpose

Flowchart Algorithm (Q1):

Flowchart diagram

(a) Trace table for test data 9, 7, 3, 12, 6, 4, 15, 2, 8, 5:

A B C X Output
00100
199
277
333
41212
56
64
71515
822
98
105
15   2

(Only changed values are recorded in each row — blanks mean the variable keeps its previous value.)

(b) Purpose of the algorithm:

The algorithm selects and outputs the Largest and the Smallest numbers from a list of 10 positive numbers.

(c) Rewrite the algorithm using pseudocode:

A ← 0 B ← 0 C ← 100 REPEAT INPUT X IF X > B THEN B ← X ELSE IF X < C THEN C ← X ENDIF ENDIF A ← A + 1 UNTIL A = 10 OUTPUT B, C

Flowchart Algorithm (Q2):

FLOWCHART
Flowchart diagram

(a) Identify the process in the algorithm:

  • Ask to input the number of guesses the user wants to make.
  • Ask to enter the guessed word and store it in the variable G.
  • If the guessed word G is equal to the word W, then output a congratulatory message for the user's success.
  • If they don't match, then subtract the number of guesses by 1 and ask to guess again, until the number of guesses becomes 0.
  • If the number of guesses becomes 0, then output a failure message.

(b) Purpose of the algorithm:

To find out whether the user can guess the word within the number of guesses he/she believes he/she can.

Pseudocode Algorithm (Q3):

W ← 0 X ← 0 Y ← 100 Z ← 0 REPEAT INPUT Mark IF Mark <> 999 THEN REPEAT IF Mark < 0 OR Mark > 100 THEN INPUT Mark ENDIF UNTIL Mark >= 0 AND Mark <= 100 IF Mark > X THEN X ← Mark ENDIF IF Mark < Y THEN Y ← Mark ENDIF Z ← Z + Mark W ← W + 1 ENDIF UNTIL Mark = 999 OUTPUT X, Y, Z

(a) Trace table for test data 78, 34, 22, -4, 98, 16, 734, 88, 999:

W X Y Z Mark Output
00100078
1787878
23411234
32213422
-4— (rejected, re-input)
49823298
51624816
734— (rejected, re-input)
633688
99998   16   336

(b) Purpose of the algorithm:

  • Accept only the integer within the range between 0 and 100 (inclusive).
  • Find the maximum and minimum value; calculate the running total sum of values within the range of 0 and 100.
  • Output the maximum, minimum and running total sum of the values within the range.

(c) Rewrite the algorithm as a flowchart:

Flowchart diagram

Pseudocode Algorithm (Q4):

INPUT X WHILE X > 15 DO T1 ← X DIV 16 T2 ← X MOD 16 CASE T2 OF 10 : OUTPUT A 11 : OUTPUT B 12 : OUTPUT C 13 : OUTPUT D 14 : OUTPUT E 15 : OUTPUT F OTHERWISE OUTPUT T2 ENDCASE X ← T1 ENDWHILE CASE X OF 10 : OUTPUT A 11 : OUTPUT B 12 : OUTPUT C 13 : OUTPUT D 14 : OUTPUT E 15 : OUTPUT F OTHERWISE OUTPUT X ENDCASE

(a) Trace table for the two input values:

(i) Input = 37

X T1 T2 Output
37
255
22

Output read in order: 5 then 2 → reading right-to-left = 25₁₆ = 37₁₀

(ii) Input = 191

X T1 T2 Output
191
1115F
11B

Output read in order: F then B → reading right-to-left = BF₁₆ = 191₁₀

(b) Purpose of the algorithm:

  • Convert a denary number into hexadecimal.
  • Output each digit of the hexadecimal number from right to left (i.e. least-significant digit first).

Flowchart Algorithm (Q5):

FLOWCHART
Flowchart diagram
STOP STOP

(a) Trace table for input data 5,4,6,2,1, 9,3,2,1,6, 7,6,1,5,1, 0,0,0,0,0:

V W X Y Z A B Output
54621561Valid
93216407Invalid
76151616Invalid
0000000Valid

Worked example (Row 1): V=5, W=4, X=6, Y=2 → A = 2(5) + 3(4) + 4(6) + 5(2) = 10 + 12 + 24 + 10 = 56; B = 56 MOD 11 = 1; Z = 1 → B = Z → Valid.

(b) Purpose of this flowchart:

  • Calculate the check-digit for the first four input digits (V, W, X, Y).
  • Compare the check-digit with the fifth input digit (Z).
  • If the check-digit is equal to the fifth input digit, then output the message "Valid"; else output the message "Invalid".

Revision: Key Computing Terms

Statement Key Term
A diagram or sequence of steps that solves a problem — used to identify the processes that an algorithm performs.Algorithm
The series of steps / actions an algorithm carries out on its input data (e.g. assignment, selection, iteration, input, output).Process
What an algorithm is designed to achieve — the problem it solves or the result it produces.Purpose
A table used to record the values of all variables after each step of an algorithm, to test its correctness.Trace Table
The process of manually following an algorithm step-by-step, recording variable values in a trace table to verify its logic.Dry Run
A set of values used as input to test whether an algorithm produces the expected output.Test Data
A predefined function that performs integer division, giving the quotient (e.g. 10 DIV 3 = 3).DIV
A predefined function that performs integer division, giving the remainder (e.g. 10 MOD 3 = 1).MOD
A digit calculated from a number's other digits (using a formula such as A MOD 11) and used to verify the number's integrity.Check Digit
A technique that rejects inputs outside an expected range (e.g. < 0 or > 100) and asks for re-input.Range Check / Validation
A base-16 number system using digits 0–9 and letters A–F (where A=10, B=11, C=12, D=13, E=14, F=15).Hexadecimal
A base-10 number system using digits 0–9 — also called decimal.Denary