Summary — What This Chapter Covers
- Pseudocode data types: INTEGER, REAL, CHAR, STRING, BOOLEAN, DATE (plus ARRAY and FILE).
- A record groups items of different data types under one identifier: TYPE … ENDTYPE.
- Array terms: index, lower bound, upper bound. 1D and 2D arrays are declared with ARRAY[lower:upper] OF type.
- Bubble sort repeatedly compares and swaps neighbours; linear search checks each element in turn.
- Text files keep data after the program ends: OPENFILE, READFILE, WRITEFILE, EOF, CLOSEFILE.
- An ADT is a collection of data plus a set of operations on it. Stack (LIFO), queue (FIFO) and linked list are ADTs.
- Each ADT can be implemented using arrays with pointers.
1. Data Types
Data types and records
Choosing the right type
Choose the type that matches how the data is used.
| Type | Holds | Example data |
|---|
| INTEGER | A whole number | Number of students, a year: 2026 |
| REAL | A number with a fractional part | Price 4.75, temperature −2.5 |
| CHAR | Exactly one character, in single quotes | Grade 'A', a gender code |
| STRING | Zero or more characters, in double quotes | Name "Huda", a phone number "07912345678" |
| BOOLEAN | TRUE or FALSE | IsMember, Paid |
| DATE | A valid calendar date | DateOfBirth 02/01/2009 |
Telephone numbers, postcodes and ID codes with leading zeros are STRING: they are not used in calculations and a leading 0 would be lost as an INTEGER.
2. Records
Data types and records
Different types under one identifier
A record is a user-defined composite data type that holds several related items, of different data types, under one identifier. It keeps all the data about one thing together, so a whole record can be passed to a procedure, stored in an array, or written to a file as one unit.
TYPE BookRecord
DECLARE ISBN : STRING
DECLARE Title : STRING
DECLARE Price : REAL
DECLARE OnLoan : BOOLEAN
ENDTYPE
DECLARE Book : BookRecord
DECLARE Library : ARRAY[1:3] OF BookRecord
Book.ISBN ← "9780306406157"
Book.Title ← "Maths"
Book.Price ← 12.50
Book.OnLoan ← TRUE
Library[1] ← Book
Worked Example
Library[1..3] hold (Maths, 12.50, on loan), (Physics, 20.00, not on loan), (CS, 15.00, on loan). Write pseudocode to output the number of books on loan and their total value.
3. Arrays
Arrays
Index, bounds, 1D and 2D
An array is a fixed-size collection of elements of the same data type, under one identifier. Each element is accessed by its index (subscript). The lower bound is the index of the first element and the upper bound the index of the last. A 1D array suits a single list (the names in a class); a 2D array suits a table (marks for each student in each test), accessed as Array[row, column].
DECLARE Names : ARRAY[1:30] OF STRING
DECLARE Marks : ARRAY[1:3, 1:3] OF INTEGER
DECLARE Row, Column, RowTotal : INTEGER
FOR Row ← 1 TO 3
RowTotal ← 0
FOR Column ← 1 TO 3
RowTotal ← RowTotal + Marks[Row, Column]
NEXT Column
OUTPUT "Student ", Row, " total: ", RowTotal
NEXT Row
4. Bubble Sort
Arrays
5. Linear Search