SLIDE 1 / 10
CSZone.co.uk
OCR H446 · Component 2 · 2.2.1

Writing and
Tracing Algorithms

OCR A Level Computer Science · cszone.co.uk
H446 SpecA Level
Learning Objectives

By the end of this topic you will be able to:

Write algorithms in OCR H446 pseudo-code using correct syntax
Trace algorithms and produce a trace table showing variable states
Use flow charts and pseudo-code to represent algorithms
Identify and correct errors in given algorithms
OCR Pseudo-Code Syntax

H446 Pseudo-Code Reference

Assignment & I/O
x ← 5
name ← input("Enter name: ")
print("Hello " + name)
Selection
if x > 0 then
  print("positive")
elif x = 0 then
  print("zero")
else
  print("negative")
endif
For Loop
for i ← 1 to 10
  print(i)
next i
While Loop
while x > 0
  x ← x - 1
endwhile

do
  x ← x + 1
until x = 10
Subroutines in Pseudo-Code

Functions and Procedures

Function (returns a value)
function square(n)
  return n * n
endfunction

result ← square(5)
Procedure (no return)
procedure greet(name)
  print("Hello " + name)
endprocedure

call greet("Aisha")
Arrays in OCR pseudo-code: nums ← [4,7,2,9,1] — 0-indexed in OCR. Access: nums[0] = 4. Length: len(nums).
Trace Tables

Tracing an Algorithm

total ← 0
for i ← 1 to 4
  total ← total + i
next i
print(total)
itotalOutput
start0
11
23
36
41010
When tracing, record the value of each variable after each assignment. Show the output separately. Work line by line — never skip steps even if they seem obvious.
Identifying Errors

Types of Errors in Algorithms

Syntax Error
Code does not conform to the rules of the language. Program will not run/compile. E.g. missing endif, wrong operator. In pseudo-code, using = for assignment instead of could be flagged.
Logic Error
Program runs but produces wrong output. The algorithm design is flawed. E.g. using > instead of >=, off-by-one errors, wrong formula. Hardest to detect — must be found by testing.
Runtime Error
Program crashes during execution. E.g. division by zero, index out of range, calling a function that doesn't exist. Often caused by unexpected input values.
Flowcharts

Representing Algorithms as Flowcharts

Flowchart Symbols
▬ Rounded rectangle = Start/End (terminal)
▭ Rectangle = Process (assignment, calculation)
◇ Diamond = Decision (yes/no condition)
▱ Parallelogram = Input/Output
→ Arrows = flow of control
When to Use
Flowcharts are useful for visualising control flow clearly. OCR exam questions may ask you to: draw a flowchart from pseudo-code; convert a flowchart to pseudo-code; identify errors in a flowchart. Always label decision branches Yes/No.
Loops in flowcharts: a diamond with one branch looping back creates a while or do-until loop. The position of the decision relative to the process body determines which type it represents.
Exam Practice
OCR H446 Style · 4 marks
Trace the following algorithm for the input array [3, 1, 4, 2] and complete the trace table showing the values of i, j, and the array after each swap.

for i ← 0 to 2
  for j ← 0 to 2-i
    if arr[j] > arr[j+1] then
      swap arr[j] and arr[j+1]
    endif
  next j
next i
[4 marks]
4
Pass i=0: j=0: 3>1 → swap → [1,3,4,2]; j=1: 3<4 → no swap; j=2: 4>2 → swap → [1,3,2,4]
Pass i=1: j=0: 1<3 → no swap; j=1: 3>2 → swap → [1,2,3,4]
Pass i=2: j=0: 1<2 → no swap
Result: [1, 2, 3, 4] — this is Bubble Sort. 1 mark per correct pass.
Common Mistakes

Don't Lose Marks

!
Not recording every variable change in a trace table — students often skip iterations where no visible change happens (e.g. a failed condition). OCR mark schemes require every step to be shown. Record the variable state after EVERY iteration of the loop, even if it didn't change.
!
Off-by-one errors in loop bounds — confusing to 4 (runs 4 times: 1,2,3,4) with to 3 (runs 3 times). In OCR H446 pseudo-code, for i ← 1 to n is inclusive of both ends. Get this wrong in a trace and all subsequent values will be incorrect.
!
Confusing logic errors and runtime errors — a logic error produces wrong output but doesn't crash; a runtime error crashes the program during execution. Students frequently call any incorrect output a "runtime error" — be precise: if it produces wrong output without crashing, it's a logic error.
2.2.1e Complete
Well done! ✓
Writing and Tracing Algorithms
Return to lesson to continue