Skip to content
Cambridge AS & A Level9618

Arrays

Computer Science 9618 Chapter Notes

Last updated November 2026

ShareWhatsAppPost

Arrays notes as a printable PDF.

Read online free. Download a watermarked copy with a free account.

Read the notes

The full Arrays notes as text: skim, search, and jump between subtopics.

~16 min read

1. Arrays, index and bounds

An array is declared with an identifier, its bounds and the data type of the items it holds: `DECLARE Data : ARRAY[1:150] OF STRING`.

In `ThisArray[n] ← 42`, the variable n is the index (or subscript). The minimum and maximum values n may take are the lower bound and the upper bound: 1 and 150 for Data. `ARRAY[1:2, 1:50]` has two dimensions.

An index outside the bounds is not valid. A loop `FOR PCount ← 0 TO 49` used on an array declared from 1 to 50 is an error.

number of elements = upper bound - lower bound + 1

Examiner insight

“A common error was to give an actual data type such as ‘string’ as the answer.” “Weaker responses initialised the data with integers, or null values, instead of strings, or only gave a comment without actually creating an array structure.”

Worked example 13 marks

The following pseudocode represents part of the algorithm for a program.

Line numbers are for reference only.

``` 10 DECLARE Sheet4 : ARRAY[1:2, 1:50] OF INTEGER … 100 FOR PCount ← 0 TO 49 101 Sheet4[1, PCount] ← 0 102 Sheet4[2, PCount] ← 47 103 NEXT PCount ```

The pseudocode contains references to an array.

Complete the table by writing the answer for each row.

Answer
The dimension of the array
The name of the variable used as an array index
The number of elements in the array
  1. 1

    The dimension of the array: 2 (two pairs of bounds in the declaration) [1]

  2. 2

    The variable used as an array index: `PCount` [1]

  3. 3

    The number of elements in the array: 2 × 50 = 100 [1]

2. Choosing a 1D or 2D array

An array holds many values using a single identifier, and a loop can iterate through them by index. This simplifies the algorithm.

Use a 1D array for one list of values, such as 30 integers. Use a 2D array when each value is found by two indexes, a row and a column. In `DECLARE Data : ARRAY[1:150, 1:2] OF STRING` the rows come first: 150 rows by 2 columns.

An array has one data type, chosen to suit every element. Values that may include a decimal place need REAL: `DECLARE Data : ARRAY[1:120] OF REAL`. A 2D INTEGER array can hold candidate numbers in column 1 and marks in column 2, because both are integers.

number of elements = rows × columns

Examiner insight

“There was a more accessible mark available for stating that using an array should simplify the algorithm and make the solution easier to understand, but this was often not stated in the answer.”

Worked example 12 marks

Data is a 1D array of integers, containing 30 elements. All element values are unique.

An algorithm will output the index of the element with the smallest value.

The 30 data values could have been stored in separate variables rather than in an array.

Explain the benefits of using an array when designing a solution to this algorithm.

  1. 1

    It simplifies the algorithm: easier to write, understand, test and debug [1]

  2. 2

    It is possible to iterate through the values with a loop, as all 30 values are stored under a single identifier [1]

3. Processing a 1D array

Process a 1D array with a loop whose counter is the index: `FOR Index ← 1 TO 100` then `Data[Index]`. The loop runs from the lower bound to the upper bound.

Initialise every total or count before the loop. Decide the result after the loop, once all elements are processed. Use square brackets in the declaration and in every use of an element.

To test odd and even positions, use `Index MOD 2 = 0`. It is true for even index locations.

Examiner insight

“Most solutions ended with the output of a count variable, but in many cases, this had not been initialised before the loop.” “Some of the responses used square brackets in the declaration but then ordinary brackets in the assignment statement.”

Worked example 16 marks

A global 1D array Data contains 100 elements of type integer.

A function Check() will:

• total the element values in odd index locations (1, 3, 5 ... 97, 99)

• total the element values in even index locations (2, 4, 6 ... 98, 100)

• return one of three strings ‘Odd’, ‘Even’ or ‘Same’ to indicate which total is the greater, or whether the totals are the same.

Write pseudocode for the function Check().

  1. 1

    Function heading, ending and return type: `FUNCTION Check() RETURNS STRING` ... `ENDFUNCTION` [1]

  2. 2

    Declare the local variables as integers: `DECLARE Odd, Even, Index : INTEGER` [1]

  3. 3

    Initialise both totals: `Odd ← 0` and `Even ← 0` [1]

  4. 4

    Loop for 100 iterations: `FOR Index ← 1 TO 100` ... `NEXT Index` [1]

  5. 5

    ``` IF Index MOD 2 = 0 THEN Even ← Even + Data[Index] ELSE Odd ← Odd + Data[Index] ENDIF ``` [1]

  6. 6

    ``` IF Odd > Even THEN RETURN "Odd" ENDIF IF Even > Odd THEN RETURN "Even" ENDIF RETURN "Same" ``` [1]

4. Processing a 2D array

An element of a 2D array needs two indexes, row then column: `Data[Row, 2]`. To visit every element, put a loop over the columns inside a loop over the rows.

Check two conditions with one IF: a single IF statement using AND joins them. A test for either column value equal to the search string needs two full comparisons joined by OR.

A loop can step through every second row: `FOR Row ← 2 TO 150 STEP 2`.

Examiner insight

“Some candidates used a two- iteration loop to test each of the array values. This was not needed, and a simpler solution used two conditions joined by the AND operator.” “However, a common syntax structure error was to write this incorrect statement: Search = Data(Row, 1) OR Data(Row, 2)”

Worked example 13 marks

The global array is a 2D array, organised as 150 rows by 2 columns. It is declared in pseudocode as follows:

``` DECLARE Data : ARRAY[1:150, 1:2] OF STRING ```

Strings will only be counted if both of the following conditions are true:

• The current row is an even number.

• The search string exactly matches the value in either column.

Write pseudocode to check these conditions. Assume that the row index is contained in variable Row and the search string in variable Search.

  1. 1

    Test for row being an even number: `Row MOD 2 = 0` [1]

  2. 2

    Test for either column value equal to Search: `Data[Row, 1] = Search OR Data[Row, 2] = Search` [1]

  3. 3

    ``` IF Row MOD 2 = 0 AND (Data[Row, 1] = Search OR Data[Row, 2] = Search) THEN ``` [1]

5. Linear search

A linear search looks at each element in turn from the first to the last. Each element is compared with the search value.

In a function, return TRUE from inside the loop as soon as an element matches. Return FALSE only after the loop has ended, because only then has every element been compared with the search value.

The function returns the values asked for. It does not output a message.

Examiner insight

“A few candidates output ‘True’ and ‘False’, or another message, instead of the returning the required values.”

Worked example 16 marks

A program stores ten integers in a 1D array with the identifier arrayData.

A function, linearSearch(), takes an integer as a parameter and performs a linear search on arrayData to find the parameter value. It returns True if it was found and False if it was not found.

Write program code for the function linearSearch().

  1. 1

    Function with the correct identifier: `FUNCTION linearSearch(SearchValue : INTEGER) RETURNS BOOLEAN` ... `ENDFUNCTION` [1]

  2. 2

    Taking an integer search value as a parameter: `SearchValue : INTEGER` [1]

  3. 3

    Searching all ten elements (indexed 0 to 9, as in the mark scheme): `FOR Index ← 0 TO 9` ... `NEXT Index` [1]

  4. 4

    Comparing each element to the search value: `IF arrayData[Index] = SearchValue THEN` [1]

  5. 5

    Returning True if found: `RETURN TRUE` inside the IF [1]

  6. 6

    Returning False if not found: `RETURN FALSE` after the loop [1]

6. Bubble sort

A bubble sort makes repeated passes. On each pass it compares neighbouring elements and swaps a pair that is in the wrong order. To sort into ascending order, swap when the left element is greater.

An efficient bubble sort has a conditional outer loop including flag reset: set the flag to TRUE before each pass, and to FALSE when a swap happens. It also has reducing Boundary in the outer loop.

The inner loop compares row `Row` with row `Row + 1`, so it must stop one below the upper bound: `Boundary ← 999` for 1000 rows. Going on to 1000 would use the out-of-bounds index 1001. In a 2D array, a correct swap moves every column of the two rows.

Examiner insight

“MP4 was normally addressed successfully, but fewer solution identified the need to also swap the ‘other’ column value (MP5).” “Reducing the boundary counter inside the inner loop, rather than in the outer loop.”

Worked example 18 marks

A global 2D array Result of type INTEGER is used to store a list of exam candidate numbers together with their marks. The array contains 2000 elements, organised as 1000 rows and 2 columns.

Column 1 contains the candidate number and column 2 contains the mark for the corresponding candidate. All elements contain valid exam result data.

A procedure Sort() is needed to sort Result into ascending order of mark using an efficient bubble sort algorithm.

Write pseudocode for the procedure Sort().

  1. 1

    ``` PROCEDURE Sort() DECLARE Temp : INTEGER DECLARE NoSwaps : BOOLEAN DECLARE Boundary, Row, Col : INTEGER Boundary ← 999 ```

  2. 2

    ``` REPEAT ... UNTIL NoSwaps = TRUE ``` [1]

  3. 3

    ``` FOR Row ← 1 TO Boundary ... NEXT Row ``` [1]

  4. 4

    ``` IF Result[Row, 2] > Result[Row + 1, 2] THEN ``` [1]

  5. 5

    ``` FOR Col ← 1 TO 2 Temp ← Result[Row, Col] Result[Row, Col] ← Result[Row + 1, Col] Result[Row + 1, Col] ← Temp NEXT Col ``` [2]

  6. 6

    `NoSwaps ← TRUE` at the start of each pass, before the inner loop [1]

  7. 7

    `NoSwaps ← FALSE` inside the IF, when a swap is made [1]

  8. 8

    `Boundary ← Boundary - 1` in the outer loop, after the inner loop, then `ENDPROCEDURE` [1]

Recap

  • An array has a name, bounds and one data type. Elements = upper bound − lower bound + 1 (1D), rows × columns (2D).
  • Use a 1D array for one list and a 2D array for rows and columns. Rows come first.
  • Loop from the lower bound to the upper bound. Initialise totals before the loop and use the result after it.
  • Test two things in a 2D array with AND or OR between two complete comparisons.
  • Linear search: compare each element in turn, `RETURN TRUE` on a match, `RETURN FALSE` after the loop.
  • Bubble sort: compare `Row` and `Row + 1`, swap every column, reset the flag each pass and reduce the boundary in the outer loop.

End-of-chapter exercise

Test yourself on the whole chapter. Work through these before moving on.

  1. An array is declared as `DECLARE Temp : ARRAY[0:11] OF REAL`. State the lower bound, the upper bound and the number of elements.2 marks
    Answer
    1. Lower bound is 0 and upper bound is 11.
    2. Number of elements = 11 - 0 + 1 = 12.

    Trap: Taking the number of elements as 11 - 0 = 11 forgets to count both ends.

  2. An array is declared as `DECLARE Names : ARRAY[1:50] OF STRING`. Explain why the statement `Names[0] ← "Ali"` is not valid.2 marks
    Answer
    1. The lower bound is 1, so there is no element with index 0.
    2. The index 0 is out of bounds.

    Trap: Assuming that every array starts at index 0.

  3. A weather station records the rainfall in mm for each of the 7 days of one week. A reading can have a decimal part. Select a suitable data structure and write its declaration. Call the array Rainfall.2 marks
    Answer
    1. One list of 7 values needs a 1D array.
    2. `DECLARE Rainfall : ARRAY[1:7] OF REAL`

    Trap: INTEGER cannot hold a reading with a decimal part.

  4. A teacher stores the marks of 25 students in 6 tests. Write the declaration of a suitable array called Scores, with one row for each student.2 marks
    Answer
    1. Each mark is found by student and test, so use a 2D array.
    2. `DECLARE Scores : ARRAY[1:25, 1:6] OF INTEGER`

    Trap: Writing `ARRAY[1:6, 1:25]` swaps the rows and columns. Rows come first.

  5. The 1D array `Prices` has 8 elements of type REAL, indexed from 1. Write pseudocode to output the total of all the elements.4 marks
    Answer
    1. `DECLARE Total : REAL` and `DECLARE Index : INTEGER`.
    2. `Total ← 0` before the loop.
    3. `FOR Index ← 1 TO 8`, then `Total ← Total + Prices[Index]`, then `NEXT Index`.
    4. `OUTPUT Total` after the loop.

    Trap: Leaving out `Total ← 0` means the total starts from an unknown value.

  6. A 2D array `Scores` has 20 rows and 3 columns of type INTEGER. Each row holds the three marks of one student. Write pseudocode to output the total of the three marks for every student.5 marks
    Answer
    1. `FOR Row ← 1 TO 20` is the outer loop.
    2. `Total ← 0` inside the outer loop, before the inner loop.
    3. `FOR Col ← 1 TO 3` with `Total ← Total + Scores[Row, Col]`, then `NEXT Col`.
    4. `OUTPUT Total` after the inner loop and inside the outer loop, then `NEXT Row`.

    Trap: Setting `Total ← 0` once before both loops makes each total include all earlier students.

  7. The 1D array `Data` holds 7, 3, 9, 3, 5 in indexes 1 to 5. A linear search stops as soon as it finds the search value. State the index found and the number of comparisons when searching for 3. State the number of comparisons when searching for 8.3 marks
    Answer
    1. Searching for 3: 7 is compared, then 3 matches at index 2.
    2. So the index is 2 and there are 2 comparisons.
    3. Searching for 8: no element matches, so all 5 elements are compared. There are 5 comparisons.

    Trap: Giving index 4 (the last 3) or 5 comparisons for the value 3 ignores that the search stops at the first match.

  8. A bubble sort puts the array 5, 2, 4, 1 into ascending order. Each pass compares neighbours from the first pair onward. Write the array after the first pass and after the second pass.3 marks
    Answer
    1. First pass: 5 and 2 swap, 5 and 4 swap, 5 and 1 swap. Array is 2, 4, 1, 5.
    2. Second pass: 2 and 4 stay, 4 and 1 swap. Array is 2, 1, 4, 5.

    Trap: Stopping after one swap gives 2, 5, 4, 1. A pass goes on to the end of the array.

  9. Two 1D arrays of 30 elements, indexed 1 to 30, `Name` of type STRING and `Score` of type INTEGER, hold each student's name and score at the same index. Write an efficient bubble sort in pseudocode that puts the scores into descending order and keeps each name with its score.7 marks
    Answer
    1. Declare `TempScore`, `TempName`, `NoSwaps`, `Boundary` and `Index`, and set `Boundary ← 29`.
    2. `REPEAT` with `NoSwaps ← TRUE` at the start of each pass.
    3. `FOR Index ← 1 TO Boundary` with `IF Score[Index] < Score[Index + 1] THEN`.
    4. Swap the two scores using `TempScore`, and swap the two names using `TempName`.
    5. `NoSwaps ← FALSE` inside the IF.
    6. `Boundary ← Boundary - 1` after the inner loop and `UNTIL NoSwaps = TRUE`.
    7. ``` Boundary ← 29 REPEAT NoSwaps ← TRUE FOR Index ← 1 TO Boundary IF Score[Index] < Score[Index + 1] THEN TempScore ← Score[Index] Score[Index] ← Score[Index + 1] Score[Index + 1] ← TempScore TempName ← Name[Index] Name[Index] ← Name[Index + 1] Name[Index + 1] ← TempName NoSwaps ← FALSE ENDIF NEXT Index Boundary ← Boundary - 1 UNTIL NoSwaps = TRUE ```

    Trap: Using `>` sorts into ascending order, and swapping only the scores leaves each name beside the wrong score.

  10. A café stores its takings in a 2D array declared as `DECLARE Takings : ARRAY[1:4, 1:7] OF REAL`: one row for each of 4 weeks and one column for each of 7 days. Write pseudocode that, for each week, outputs the week number and the number of the first day in that week with takings greater than 200, or the word "None" if there is no such day. The search of a week must stop as soon as the day is found.7 marks
    Answer
    1. Declare `Week` and `Day` as INTEGER and `Found` as BOOLEAN.
    2. `FOR Week ← 1 TO 4` is the outer loop.
    3. `Found ← FALSE` and `Day ← 1` inside the outer loop, before the search of each week.
    4. `WHILE Day <= 7 AND Found = FALSE`: a conditional loop stops at the first match.
    5. `IF Takings[Week, Day] > 200 THEN Found ← TRUE ELSE Day ← Day + 1`, so `Day` keeps the day found.
    6. After the WHILE loop, output `Week` with `Day` if `Found` is TRUE, otherwise `Week` with "None".
    7. ``` DECLARE Week, Day : INTEGER DECLARE Found : BOOLEAN FOR Week ← 1 TO 4 Found ← FALSE Day ← 1 WHILE Day <= 7 AND Found = FALSE IF Takings[Week, Day] > 200 THEN Found ← TRUE ELSE Day ← Day + 1 ENDIF ENDWHILE IF Found = TRUE THEN OUTPUT Week, " ", Day ELSE OUTPUT Week, " None" ENDIF NEXT Week ```

    Trap: A count-controlled loop over all 7 days runs past the first match, and setting `Found` and `Day` only once, before the outer loop, carries week 1's result into every later week.

Say this, not that

Wording that loses the mark, against the wording the mark scheme credits.

Loses the markGets the mark
`Data(Row, 1)` or `RNum(Index)` in an assignment, after square brackets in the declaration`Data[Row, 1]` and `RNum[Index]`: square brackets throughout
The extra information needed to declare an array is ‘string’The data type (of the item to be stored)
`Search = Data(Row, 1) OR Data(Row, 2)``Data[Row, 1] = Search OR Data[Row, 2] = Search`
Output ‘True’ or ‘False’ from the search function`RETURN TRUE` when found and `RETURN FALSE` after the loop

Frequently asked questions

How do you work out the number of elements in an array?
For a 1D array, subtract the lower bound from the upper bound and add 1. For example, an array declared ARRAY[1:50] has 50 elements, and the lower bound 0 with upper bound 11 gives 12. For a 2D array, multiply the rows by the columns, so ARRAY[1:2, 1:50] has 2 × 50 = 100 elements.
What is the difference between a 1D array and a 2D array?
A 1D array holds one list of values under one identifier, such as 30 integers. A 2D array holds values found by two indexes, a row and a column, and the rows come first. ARRAY[1:150, 1:2] has 150 rows by 2 columns. An array holds one data type, chosen to suit every element.
How does a linear search work in a function?
A linear search compares each element in turn, from the first to the last, with the search value. It returns TRUE from inside the loop as soon as an element matches. It returns FALSE only after the loop has ended, because only then has every element been compared. It returns the values asked for and does not output a message.
How do you process every element of a 1D array?
Use a loop whose counter is the index, from the lower bound to the upper bound, such as FOR Index ← 1 TO 100 then Data[Index]. Initialise every total or count before the loop, and decide the result after the loop. Use square brackets in the declaration and in every use of an element.
How does a bubble sort work, and why reduce the boundary?
A bubble sort makes repeated passes, comparing neighbouring elements and swapping any pair in the wrong order. The inner loop compares Row with Row + 1, so it must stop one below the upper bound: Boundary ← 999 for 1000 rows. An efficient version resets a flag to TRUE before each pass and reduces the boundary in the outer loop.
What mistake do students make when testing two columns of a 2D array?
They write an incomplete or round-bracket test, such as Search = Data(Row, 1) OR Data(Row, 2). Examiners reported this as a common syntax error. Use square brackets and two full comparisons joined by OR, for example Data[Row, 1] = Search OR Data[Row, 2] = Search, inside an IF that tests the even row with AND.

Free notes are just the start.

Unlock every Workbook and Chapter at a Glance, and generate your own worksheets and predicted papers.

Explore plans

Related chapters