Skip to content

OCR A Level · Free tool

OCR A Level H446 Pseudocode Runner

Run OCR A Level Computer Science H446 pseudocode online, with functions, procedures, arrays and strings. Free, in your browser, no sign-up.

OCR H446
main.ocr
Ready

H446 pseudocode rules

OCR A Level pseudocode with byRef/byVal parameters, recursion and openRead/openWrite files.
  • Assign with = and compare with == or !=.
  • Parameters can be marked name:byRef or name:byVal.
  • Files: openRead, openWrite, readLine(), writeLine(), endOfFile() and close().
  • Arrays are zero-based: array board[3, 3].

H446 example programs

Every example runs exactly as shown. Open one in the editor to change it and run it again.

Bubble sort with byRef

A procedure sorts an array passed byRef using a do ... until loop.

SortingSubroutinesArrays

Open in editor
// Bubble sort an array passed by reference
procedure bubbleSort(items:byRef)
  n = items.length
  do
    swapped = false
    for i = 0 to n - 2
      if items[i] > items[i + 1] then
        temp = items[i]
        items[i] = items[i + 1]
        items[i + 1] = temp
        swapped = true
      endif
    next i
    n = n - 1
  until swapped == false
endprocedure

numbers = [29, 3, 17, 8, 42, 1]
bubbleSort(numbers)
for i = 0 to numbers.length - 1
  print(numbers[i])
next i
Output
1
3
8
17
29
42

openRead and openWrite

Reads marks.txt with openRead and writes the passing marks with openWrite.

File handlingLoops

Open in editor
// Copy passing marks into a new file
reader = openRead("marks.txt")
writer = openWrite("passed.txt")
passed = 0
while NOT reader.endOfFile()
  line = reader.readLine()
  if int(line) >= 40 then
    writer.writeLine(line)
    passed = passed + 1
  endif
endwhile
reader.close()
writer.close()
print(str(passed) + " marks written to passed.txt")
Output
3 marks written to passed.txt

Two-dimensional array

Fills a 3 by 3 array with a times table using nested for loops and prints each row.

ArraysLoops

Open in editor
// Nested loops over a 2D array
array grid[3, 3]
for row = 0 to 2
  for col = 0 to 2
    grid[row, col] = (row + 1) * (col + 1)
  next col
next row

for row = 0 to 2
  line = ""
  for col = 0 to 2
    line = line + str(grid[row, col]) + " "
  next col
  print(line)
next row
Output
1 2 3 
2 4 6 
3 6 9