2.1.2

Designing, creating and refining algorithms

GCSE Computer Science 2.1.2: inputs, processes and outputs; structure diagrams; creating and refining algorithms in pseudocode, flowcharts and reference language; identifying common errors; and trace tables.

About 22 minutes to read

Inputs, processes and outputs

Before an algorithm can be written, the problem has to be defined. The standard first step is to identify three things:

• Inputs — the data the system receives, whether typed by a user, read from a file or taken from a sensor • Processes — the calculations, decisions and manipulations carried out on that data • Outputs — what the system produces: something displayed, printed, saved or sent to a device

For a program that works out a customer's change: the inputs are the total cost and the amount paid; the process is subtracting one from the other, and checking whether enough money was given; the output is the change due, or a message that the payment is short.

A common trap is listing something as an input when it is actually stored in the program. A price looked up from the program's own list is not an input — it was not received from outside.

Structure diagrams

A structure diagram shows decomposition visually. The whole problem sits in a box at the top, and below it are the sub-problems it breaks into, each of which may break down further. It is a hierarchy read top to bottom.

You are required to produce simple diagrams showing the structure of a problem, and the subsections and their links to other subsections — so the links matter as much as the boxes, and every box must connect to its parent.

Figure: A structure diagram for a cash machine, written as a hierarchy
Drawn properly this is boxes joined by lines, but the hierarchy is what is being marked: the whole problem on top, its subsections beneath, each linked to its parent.
Structure diagram
a diagram showing decomposition visually, with the whole problem at the top and the sub-problems it breaks into below, read as a hierarchy top to bottom
Decomposition
breaking a problem down into smaller sub-problems that can be solved separately

Flowcharts

A flowchart expresses an algorithm as a diagram. Each symbol has a fixed meaning, and using the wrong shape loses marks even when the logic is right.

The six symbols named in the specification: terminal, input/output, process, decision, sub program, and the lines joining them.

How to read them:

• Terminal — the rounded box at the start and end, labelled START or STOP • Input/Output — the parallelogram, for anything entered or displayed • Process — the rectangle, for a calculation or an assignment • Decision — the diamond, containing a question with two labelled branches, Yes and No • Sub program — the rectangle with vertical bars at the sides, for a task defined elsewhere • Line — an arrow showing the direction of flow

The decision symbol is where loops come from: if one branch leads back to an earlier point, the steps in between repeat until the condition changes — a condition-controlled loop drawn as a diagram.

Flowchart
a diagram expressing an algorithm, in which each symbol has a fixed meaning — terminal, input/output, process, decision and sub program

Pseudocode and reference language

The specification asks you to create, interpret, correct, complete and refine algorithms — five different demands, and papers use all of them. Algorithms may be given as pseudocode, as flowcharts, or in a high-level language or Exam Reference Language.

Exam Reference Language is close enough to ordinary pseudocode that no new syntax has to be memorised, but knowing the common forms saves time.

Figure: Common forms in Exam Reference Language

Nesting means putting one structure inside another, and the specification names it explicitly for both selection and iteration.

Nested selection is an if inside an if — checking a second condition only when the first was true. Nested iteration is a loop inside a loop, which is how a two-dimensional array is processed: the outer loop moves through the rows and the inner loop moves through each column of that row.

When writing nested code, indent every level. The indentation is not decoration — it is what makes it possible to see which endif or which next belongs to which structure, and unindented nested code is where most logic errors hide.

Identifying common errors

You are expected to identify syntax and logic errors in code and suggest fixes.

A syntax error breaks the rules of the language, so the program will not run at all: a missing endif, a misspelled keyword, an unclosed quotation mark or bracket.

A logic error runs perfectly but produces the wrong answer: using + where * was meant, a loop running one time too many, a condition using > when it should be >=, or a total not reset to zero before it is accumulated.

When asked to suggest a fix, say what is wrong and what it should be — “line 4 uses = instead of ==, so it should read if answer == 5 then” earns the mark that “there is an error on line 4” does not.

Common mistake

An off-by-one error in a loop is a logic error, not a syntax error. The code runs; it just processes the wrong number of items. Syntax errors stop the program running at all — that is the test to apply.

Syntax error
an error that breaks the rules of the language, so the program will not run at all
Logic error
an error in which the program runs perfectly but produces the wrong answer

Trace tables

A trace table follows an algorithm by hand, recording the value of every variable after each step. It is how logic errors are found without running the code, and you must be able to both create and use them.

The method: one column per variable, plus a column for any output. Work through one line at a time, and write a new row every time a value changes. Do not skip iterations of a loop — the point is to see what happens on each pass.

Worked example

Complete a trace table for this algorithm: total = 0 for i = 1 to 4 total = total + i if total > 5 then print(total) endif next i

  1. Set up columns for i, total and output, and record the starting value total = 0.
  2. First pass: i = 1, total becomes 0 + 1 = 1. Is 1 > 5? No, so nothing is printed.
  3. Second pass: i = 2, total becomes 1 + 2 = 3. Is 3 > 5? No.
  4. Third pass: i = 3, total becomes 3 + 3 = 6. Is 6 > 5? Yes, so 6 is printed.
  5. Fourth pass: i = 4, total becomes 6 + 4 = 10. Is 10 > 5? Yes, so 10 is printed.
  6. The loop ends because i has reached 4.

Answer: The rows are (i, total, output): start (–, 0, –), then (1, 1, –), (2, 3, –), (3, 6, 6), (4, 10, 10). The algorithm outputs 6 and then 10.

Common mistake

Filling in only the final values of the variables is not a trace table. The marks are for showing every change as it happens, one row per update — an examiner cannot see that you followed the loop if you only write the last row.

Trace table
a table used to follow an algorithm by hand, recording the value of every variable after each step, in order to find logic errors without running the code

Before you practise

  • Define a problem by identifying its inputs, processes and outputs.
  • A structure diagram shows the whole problem on top with its subsections beneath, each linked to its parent.
  • Flowchart symbols: terminal (rounded), input/output (parallelogram), process (rectangle), decision (diamond), sub program (barred rectangle), and lines with arrows.
  • A decision with a branch leading backwards is how a loop is drawn.
  • You may be asked to create, interpret, correct, complete or refine an algorithm.
  • Nesting puts one selection or iteration inside another; indent every level.
  • A syntax error stops the program running; a logic error runs but gives the wrong result.
  • When suggesting a fix, say what is wrong and what it should be.
  • A trace table has one column per variable plus output, and one row per change — including every loop iteration.

Now practise it

Reading is the easy half. Answer questions on 2.1.2 Designing, creating and refining algorithms and find out which parts you actually know.