Skip to content

Cambridge AS & A Level · Free tool

Cambridge A Level 9618 Pseudocode Compiler

Run Cambridge International AS & A Level Computer Science 9618 pseudocode online, including procedures, functions, records and file handling. Free, in your browser, no sign-up.

Cambridge 9618
main.9618
Ready

9618 pseudocode rules

Cambridge AS & A Level pseudocode with records (TYPE), BYREF parameters and text files.
  • Assign with ← (type <-); constants use CONSTANT Name = value.
  • Procedures use CALL; parameters can be BYVAL or BYREF.
  • Records are declared with TYPE ... ENDTYPE; files with OPENFILE, READFILE, WRITEFILE and EOF.
  • String functions include MID, LEFT, RIGHT, LENGTH, TO_UPPER, NUM_TO_STR and STR_TO_NUM.

9618 example programs

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

BYREF procedures and functions

A BYREF Swap procedure changes the caller's variables, and a FUNCTION returns the larger value.

SubroutinesInput and output

Open in editor
// BYREF parameters let a procedure change the caller's variables
PROCEDURE Swap(BYREF A : INTEGER, B : INTEGER)
  DECLARE Temp : INTEGER
  Temp <- A
  A <- B
  B <- Temp
ENDPROCEDURE

FUNCTION Max(X : INTEGER, Y : INTEGER) RETURNS INTEGER
  IF X > Y
    THEN
      RETURN X
  ENDIF
  RETURN Y
ENDFUNCTION

DECLARE First : INTEGER
DECLARE Second : INTEGER

INPUT First
INPUT Second
OUTPUT "Before: ", First, " ", Second
CALL Swap(First, Second)
OUTPUT "After: ", First, " ", Second
OUTPUT "Larger: ", Max(First, Second)
Inputs
4, 9
Output
Before: 4 9
After: 9 4
Larger: 9

Records with TYPE

Declares a record TYPE, stores records in an array and finds the student with the highest mark.

RecordsArrays

Open in editor
// A record type and an array of records
TYPE TStudent
  DECLARE Name : STRING
  DECLARE Mark : INTEGER
ENDTYPE

DECLARE Class : ARRAY[1:3] OF TStudent
DECLARE Index : INTEGER
DECLARE Best : INTEGER

Class[1].Name <- "Amir"
Class[1].Mark <- 71
Class[2].Name <- "Bella"
Class[2].Mark <- 86
Class[3].Name <- "Chen"
Class[3].Mark <- 64

Best <- 1
FOR Index <- 2 TO 3
  IF Class[Index].Mark > Class[Best].Mark
    THEN
      Best <- Index
  ENDIF
NEXT Index

OUTPUT "Top student: ", Class[Best].Name, " (", Class[Best].Mark, ")"
Output
Top student: Bella (86)

Reading and writing text files

Reads scores.txt line by line until EOF and writes the passing scores to a new file.

File handlingLoops

Open in editor
// Copy passing scores from one text file to another
DECLARE Line : STRING
DECLARE Score : INTEGER
DECLARE Count : INTEGER

Count <- 0
OPENFILE "scores.txt" FOR READ
OPENFILE "passed.txt" FOR WRITE
WHILE NOT EOF("scores.txt")
  READFILE "scores.txt", Line
  Score <- STR_TO_NUM(Line)
  IF Score >= 50
    THEN
      WRITEFILE "passed.txt", Line
      Count <- Count + 1
  ENDIF
ENDWHILE
CLOSEFILE "scores.txt"
CLOSEFILE "passed.txt"

OUTPUT Count, " scores copied to passed.txt"
Output
3 scores copied to passed.txt

Counting vowels

A FUNCTION loops through a string with MID and uses CASE OF to count the vowels.

StringsSelectionSubroutines

Open in editor
// Count the vowels in a sentence
FUNCTION CountVowels(Text : STRING) RETURNS INTEGER
  DECLARE Index : INTEGER
  DECLARE Count : INTEGER
  DECLARE Letter : CHAR
  Count <- 0
  FOR Index <- 1 TO LENGTH(Text)
    Letter <- TO_UPPER(MID(Text, Index, 1))
    CASE OF Letter
      'A' : Count <- Count + 1
      'E' : Count <- Count + 1
      'I' : Count <- Count + 1
      'O' : Count <- Count + 1
      'U' : Count <- Count + 1
    ENDCASE
  NEXT Index
  RETURN Count
ENDFUNCTION

DECLARE Sentence : STRING
INPUT Sentence
OUTPUT "Vowels: ", CountVowels(Sentence)
OUTPUT "First six letters: ", LEFT(Sentence, 6)
Inputs
Pseudocode is fun
Output
Vowels: 7
First six letters: Pseudo

Recursive factorial

A recursive FUNCTION with a base case, called inside a FOR loop.

RecursionSubroutines

Open in editor
// Recursion: N! = N * (N - 1)!
FUNCTION Factorial(N : INTEGER) RETURNS INTEGER
  IF N <= 1
    THEN
      RETURN 1
  ENDIF
  RETURN N * Factorial(N - 1)
ENDFUNCTION

DECLARE Index : INTEGER
FOR Index <- 1 TO 6
  OUTPUT Index, "! = ", Factorial(Index)
NEXT Index
Output
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720