9.2 Algorithms

Bulk view disabled for Guests. View lessons individually.

Pseudocode Basics

1. Variables & Constants

In A-Level pseudocode, we must always DECLARE a variable and its DATATYPE before using it.

Standard Data Types:

INTEGER Whole numbers (e.g., 10, -5)
REAL Decimal numbers (e.g., 10.5, 3.14)
CHAR Single character (e.g., 'A')
STRING Multiple characters (e.g., "Hello World")
BOOLEAN Logic values (TRUE or FALSE)

DECLARE StudentName : STRING
DECLARE TotalScore : INTEGER

// Assignment uses the left arrow
StudentName ← "Moraa"
TotalScore ← 85

// Constants are declared once and cannot be changed
CONSTANT PI ← 3.14159

2. One-Dimensional (1D) Arrays

An array is a collection of data elements of the same type, accessed using an index.

// Format: DECLARE Name : ARRAY[Lower:Upper] OF DataType
DECLARE Scores : ARRAY[1:10] OF INTEGER

// Storing a value at index 1
Scores[1] ← 92

// Reading a value from index 1
OUTPUT Scores[1]

3. Two-Dimensional (2D) Arrays

Think of a 2D array as a grid or table with rows and columns. This is essential for more complex problem solving.

// Format: ARRAY[RowLower:RowUpper, ColLower:ColUpper]
DECLARE ChessBoard : ARRAY[1:8, 1:8] OF STRING

// Accessing Row 1, Column 1
ChessBoard[1, 1] ← "White Rook"
⚠️ Common Error: Array Bounds

In A-Level, we explicitly state the lower and upper bounds. If you declare ARRAY[1:5], trying to access Scores[0] or Scores[6] will result in an "Array Out of Bounds" error.

4. What are User-Defined Types?

A User-Defined Datatype is a custom data structure created by the programmer based on existing built-in types (Integer, Real, String, etc.). This allows you to group related data and make your algorithms more readable and organized.

1. Enumerated Type

A list of possible fixed values. It creates a restricted set of options, preventing invalid data entry.

Example: Days of the week, Cardinal directions, or Order status.

2. Composite (Record) Type

A collection of different attributes under a single name. Similar to a "row" in a database table.

Example: A "Student" record containing a Name, ID, and Date of Birth.

5. Enumerated Type Example

TYPE TDayOfWeek = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

DECLARE Today : TDayOfWeek
Today ← Monday
IF Today = Saturday OR Today = Sunday THEN
    OUTPUT "It is the weekend!"
ENDIF

6. Composite Type (Records) Example

Records are the most powerful user-defined types. You use the "dot notation" to access individual fields.

TYPE TStudent
    DECLARE Name : STRING
    DECLARE StudentID : INTEGER
    DECLARE IsEnrolled : BOOLEAN
ENDTYPE

// Using the record
DECLARE NewStudent : TStudent
NewStudent.Name ← "Alice Ngina"
NewStudent.StudentID ← 5501
NewStudent.IsEnrolled ← TRUE

7. Non-Composite: Pointer Type

A Pointer is a variable that stores the memory address of another variable. While less common in high-level pseudocode, it is vital for understanding how Linked Lists and Trees are built in A-Level Paper 3.

⚠️ Exam Note: Naming Conventions

In 9618 Pseudocode, custom types are often prefixed with a "T" (e.g., TStudent) to distinguish them from standard variables. Always remember to DECLARE the variable after you have defined the TYPE.