Everything from the 5 lessons of this topic on a few pages: the key idea of each lesson, the diagram to remember, the points to learn and the traps examiners set. Read it, cover it, test yourself.
A data type decides which values a variable can store and which operations are allowed on it. 9618 pseudocode uses INTEGER, REAL, CHAR, STRING, BOOLEAN and DATE. A record is a composite (user-defined) data type that holds a set of related data items, called fields, which can be of different data types, all under one identifier. It is defined with TYPE ... ENDTYPE and each field is accessed with dot notation, such as Pupil1.YearGroup.
TYPE Name, a DECLARE line per field, ENDTYPE; then DECLARE a variable of that type.Variable.Field; in an array of records, Array[Index].Field."Phone number: INTEGER because it is a number" loses the mark. Digits that are an identifier, not a quantity, belong in a STRING.
TYPE ... ENDTYPE only defines the type. You still need DECLARE ThisBook : BookRecord before any data can be stored. Writing BookRecord.Title ← "Dune" uses the type name, not a variable, and earns no mark.
The index belongs to the array, not the field: Library[12].Copies, never Library.Copies[12].
An array is a fixed-length data structure of elements that all have the same data type, held under one identifier. Each element is reached by its index (subscript). The lower bound is the index of the first element and the upper bound the index of the last, so ARRAY[1:30] has 30 elements. A 1D array is a list with one index; a 2D array is a table reached by [row, column]. Arrays are processed one element at a time with a FOR loop; a 2D array needs nested loops.
| i | Scores[i] | Scores[i] > Max | Max | MaxIndex |
|---|---|---|---|---|
| 12 | 1 | |||
| 2 | 31 | TRUE | 31 | 2 |
| 3 | 7 | FALSE | ||
| 4 | 31 | FALSE | ||
| 5 | 25 | FALSE | ||
| 6 | 9 | FALSE |
Output: Highest 31 at position 2. Because the test is > and not >=, the first position of a tied maximum is kept.
DECLARE X : ARRAY[1:n] OF type or ARRAY[1:r, 1:c] OF type.Start the maximum at the first element, Max ← Scores[1], not at 0: if every value were negative, a start of 0 would give the wrong answer.
Keep the index order the same as the declaration. If the array is [1:3, 1:4], then Marks[4, 2] is out of range: there is no row 4.
Do not mix data types in one array. "A 2D array with a column of names and a column of marks" would need every element to be STRING, so the marks could not be used in arithmetic without conversion.
A linear search compares the target with each element in turn, from the first, and stops when it is found or the end of the array is reached. It works on unsorted data; in the worst case it makes n comparisons. A bubble sort compares each pair of adjacent elements and swaps them (using a temporary variable) if they are in the wrong order. After each pass the largest unsorted value has reached its final place. The improved version uses a flag to stop as soon as a pass makes no swaps and shortens each pass by one.
Index <= 8 is FALSE and the loop stops: 8 comparisons.Ann found at position 6 after 6 comparisons; (b) Tom not found after 8 comparisonsDo not put OUTPUT "not found" inside the loop: it would be output for every element that does not match. Decide only after the loop has finished, using the Found flag.
Writing the swap as Nums[j] ← Nums[j + 1] then Nums[j + 1] ← Nums[j] copies one value into both elements. Always use a temporary variable.
Set Swapped ← FALSE at the start of every pass, inside the REPEAT. Setting it once before the loop means a single swap in pass 1 keeps it TRUE for ever.
Data in variables is held in RAM and is lost when the program ends. A file is stored on secondary storage, so the data persists and can be used the next time the program runs or by another program. A text file is a sequence of lines, each read or written as a string, in order. A file is opened in one mode: READ, WRITE (creates a new file; any existing data is lost) or APPEND (adds after the existing data). Use READFILE, WRITEFILE, the EOF function and CLOSEFILE.
2 of 5 names start with AWHILE NOT EOF(file), READFILE file, StringVar, process, ENDWHILE."Files are needed to store data" is too vague. Say that variables are lost when the program ends, so the data must be stored permanently in a file to be used again later.
READFILE reads one line into a STRING variable. If a line holds a number, it is still text until it is converted, so do not declare the READFILE variable as INTEGER.
Always CLOSEFILE when you have finished. An examiner checks that every OPENFILE has a matching CLOSEFILE, with the same file name, after the loop rather than inside it.
An abstract data type (ADT) is a collection of data together with a set of operations on that data. A stack is last in, first out (LIFO): items are pushed onto and popped from the top, tracked by a top pointer. A queue is first in, first out (FIFO): items join at the rear and leave from the front; a circular queue reuses freed spaces. A linked list is a set of nodes, each holding data and a pointer to the next node, with a start pointer, a null pointer at the end and a free list of unused nodes. All three can be implemented using arrays.
| Operation | TopOfStack | Stack (bottom to top) | Returned |
|---|---|---|---|
| start | −1 | empty | |
| PUSH 4 | 0 | 4 | |
| PUSH 9 | 1 | 4, 9 | |
| PUSH 2 | 2 | 4, 9, 2 | |
| POP | 1 | 4, 9 | 2 |
| PUSH 7 | 2 | 4, 9, 7 | |
| POP | 1 | 4, 9 | 7 |
| POP | 0 | 4 | 9 |
Do not say a pop "deletes" the value from the array. Only the pointer changes; the old value is simply overwritten by the next push.
In a circular queue, Rear = upper bound does not mean the queue is full: Rear can wrap round. Test fullness with the count (or by comparing the pointers carefully).
When inserting, set the new node's pointer before changing the previous node's pointer. Doing it the other way round loses the address of the rest of the list.