Cambridge O Level Computer Science · Syllabus 2210 · Programming
Array
What is Array?
A fixed-length structure of elements of identical data type, held under one identifier and accessed by consecutive index numbers, so that one loop can process many related values.
This definition is part of the Programming chapter in Cambridge O Level Computer Science.
Array in context
A program is a stored sequence of instructions that manipulates data held in named storage. Topic 8 asks you to do four things with that idea. First, name and type your data correctly: a variable whose value may change, a constant whose value may not, each with one of the five data types INTEGER, REAL, CHAR, STRING, BOOLEAN. Second, control the order in which instructions run using only three structures — sequence, selection (IF, CASE) and iteration (FOR, WHILE, REPEAT). Third, break a long solution into procedures and functions so it stays readable and testable. Fourth, hold many related values in an array and hold them beyond the end of the run in a file. Everything else in this chapter is detail hanging off those four moves.
Sequence means statements are executed one after another, in the order written, unless a control structure changes that order. INPUT <identifier> takes a value supplied by the user and stores it in the named variable. OUTPUT <value(s)> displays one or more values, separated by commas. Assignment is written <identifier> ← <value>: the expression on the right is evaluated first, and the single result is then stored in the variable on the left, replacing whatever was there. The identifier on the left must be a variable — it may be an element of a data structure such as an array — and the value must be of the same data type as the variable.
A maintainable program is one that another programmer — or you, six months later — can read, understand and safely change. The syllabus names the ingredients: meaningful identifiers for variables, constants, arrays, procedures and functions; relevant and appropriate use of the commenting feature provided by the language; and the use of procedures and functions to break the solution into named pieces. Consistent indentation and the removal of unnecessary repeated code follow from those. The syllabus lists it as a learning outcome of its own, 8.1.8, so it is something you can be asked about directly as well as something to practise in every answer you write.
An array is a fixed-length structure of elements of identical data type, held under one identifier and reached by consecutive index numbers. Its purpose is to let one loop handle many related values: without arrays, thirty marks need thirty separate variables and thirty copies of every statement. It is good practice to state the lower bound explicitly, because that bound defaults to 0 in some systems and 1 in others; generally a lower bound of 1 is used. Square brackets hold the indices, and in ordinary statements only one index value is used for each dimension.
A two-dimensional array is declared with two pairs of bounds: DECLARE <identifier> : ARRAY[<l1>:<u1>, <l2>:<u2>] OF <data type>. It is usually pictured as a grid of rows and columns. A single element is selected with one index value for each dimension, written in the declared order: NoughtsAndCrosses[2,3]. Visiting every element needs nested iteration — one loop for the first dimension and one for the second — and which loop is on the outside decides whether you travel row by row or column by column.
Variables and arrays live in main memory, which is volatile and is released when the program ends — so anything held only there is lost. A file is stored on secondary storage, so the data still exists after the program has finished and can be read again on a later run, by this program or another one. It is good practice to explicitly open a file, stating the mode of operation, before reading from or writing to it. The two modes are READ, for data to be read from the file, and WRITE, for data to be written to it — and in WRITE mode a new file will be created and any existing data in the file will be lost. A file should be opened in only one mode at a time, and files should be closed when they are no longer needed.
Common mistakes with Array
- M23. “Array indexes always start at 1.” TruthThe syllabus states that the first index can be zero or one. It is good practice to state the lower bound explicitly, because it defaults differently in different systems; generally a lower bound of 1 is used. DoRead the bound off the declaration and copy it into the loop, rather than assuming.
- M24. “The last index is the number of elements.” TruthTrue only when the lower bound is 1. ARRAY[0:7] has eight elements and a last index of 7. Number of elements= upper bound − lower bound + 1, because the bounds are inclusive.
- M25. “Rows and columns are interchangeable in a 2D array.” TruthThe first index and the second index are fixed by the declaration. On ARRAY[1:4, 1:3], writing Sales[Product, Week] asks for elements such as Sales[3,4], which is out of range. To change directionswap which loop is outermost, and leave the brackets alone.
- M26. “Data in a file disappears when the program finishes.” TruthExactly backwards. Variables and arrays live in main memory and are lost. A file is on secondary storage precisely so the data survives the end of the program. That is the purpose— and it is what an 8.3.1 question is asking you to explain.
Examiner tips on Array
- Read the closed lists as a promise in both directions. You will never be asked for a FOR EACH loop, a SWITCH, a string .split(), an APPEND file mode or a fourth parameter — none of those is in Topic 8. Equally, every single item that is listed is fair game, including the ones students skip: ^, <>, NOT, ROUND, RANDOM, 2D arrays and writing a line of text to a file.

