EEasy IGCSELabTracker

On a phone the notes, quizzes and tracker work fine. The 3D models and the practice lab are made for a laptop.

Specification points 8.2.1–8.2.3 ·

declaring and using 1D and 2D arrays, variables as indexes, and writing to and reading from arrays with (nested) iteration.

1What an array is and why it is used

Definition

Array — a fixed-length data structure that stores elements of the same data type under one identifier; each element is accessed by its index (its position).

Why use an array instead of separate variables (Name1, Name2 … Name30):

  • one identifier holds a whole list or table of related data;
  • a variable can be used as the index, so one loop processes every element in a few lines of code, however many elements there are;
  • the data can then be searched, sorted or totalled (→ Topic 7).

All elements have the same data type, so different data about the same items goes in separate arrays that share an index: Names[3] and Marks[3] belong to the same student.

1D array (a list) DECLARE Names : ARRAY[1:5] OF STRING index "Ali" 1 "Ben" 2 "Cara" 3 "Dev" 4 "Eve" 5 Names[3] is "Cara" 2D array (a table): [row, column] DECLARE Scores : ARRAY[1:3, 1:4] OF INTEGER column 1 column 2 column 3 column 4 row 1 12 9 15 11 row 2 8 14 10 17 row 3 13 7 16 6 Scores[2, 4] is 17 Nested loop order: row 1, columns 1–4, then row 2 …
Figure 1. A 1D array has one index; a 2D array has two, always [row, column].

Fill the array with a loop

Loading the interactive…

2Declaring and using arrays

DECLARE Names : ARRAY[1:30] OF STRING           // 1D: 30 elements
DECLARE Scores : ARRAY[1:3, 1:4] OF INTEGER     // 2D: 3 rows × 4 columns = 12 elements
Names[1] ← "Ali"                                // write one element
Scores[2, 4] ← 17                               // row 2, column 4
OUTPUT Names[Position]                          // variable as the index
Names[N + 1] ← Names[N]                         // expression as the index
  • The declaration gives the identifier, the lower and upper bound of each index, and the data type.
  • The first index can be 0 or 1 (ARRAY[0:9] also has 10 elements). Cambridge normally uses 1 — always read the bounds in the declaration and make loops match them.
  • Each statement uses one element. A whole array is filled or processed with a loop.

3Writing to and reading from arrays with iteration

A 1D array needs one loop; the loop variable is the index.

FOR Index ← 1 TO 30
    OUTPUT "Enter name ", Index
    INPUT Names[Index]                  // write a value into the array
NEXT Index

FOR Index ← 1 TO 30
    OUTPUT Names[Index]                 // read a value from the array
NEXT Index

A 2D array needs nested loops: the outer loop chooses the row, the inner loop visits every column of that row (the order shown by the arrows in Figure 1).

FOR Row ← 1 TO 3
    RowTotal ← 0
    FOR Column ← 1 TO 4
        INPUT Scores[Row, Column]                   // write
        RowTotal ← RowTotal + Scores[Row, Column]   // read
    NEXT Column
    OUTPUT "Total for row ", Row, " is ", RowTotal
NEXT Row

To initialise an array, assign a starting value to every element in the same kind of loop, e.g. Scores[Row, Column] ← 0.

Traps
  • An index outside the declared bounds (e.g. Names[31] or Names[0] for ARRAY[1:30]) is an error — check loop start and end values against the bounds.
  • 2D indexes are always [row, column]; the inner loop's variable goes second.
  • Reset a row total inside the outer loop but outside the inner loop, as above.

Quick check

11 multiple-choice questions. Pick an answer to see whether it is right and why.

  1. Which statement about an array is correct?

  2. How many elements does DECLARE Grid : ARRAY[1:5, 1:6] OF INTEGER create?

  3. How many elements does DECLARE Codes : ARRAY[0:12] OF STRING create?

  4. Temps holds 14, 9, 21 and 17 at indexes 1 to 4. After N ← 2, what is the value of Temps[N + 1]?

  5. Items holds "pen", "ink" and "cap" at indexes 1 to 3. What does it hold after Items[3] ← Items[1]?

  6. The 2D array Seats holds 4, 7, 1 in row 1 and 8, 2, 6 in row 2. What is the value of Seats[2, 1]?

  7. After DECLARE Marks : ARRAY[1:20] OF INTEGER, which statement causes an error?

  8. Nums holds 3, 8, 5 and 6 at indexes 1 to 4. Total ← 0 is followed by a loop FOR Index ← 2 TO 4 containing Total ← Total + Nums[Index]. What is Total after the loop?

  9. A loop FOR Row ← 1 TO 5 contains a loop FOR Column ← 1 TO 2, which contains INPUT Table[Row, Column]. How many values are input?

  10. A loop FOR Row ← 1 TO 2 contains a loop FOR Column ← 1 TO 3, which contains INPUT Grid[Row, Column]. Which element receives the third value input?

  11. Nested loops add up each row of a 2D array in RowTotal and output it after each row. Where must RowTotal ← 0 be written?

Exam-style questions

Write your answer, then show the answer and compare. Nothing here is marked for you: you decide how close you were. What you write is saved in this browser.

Recall and understanding

1

Define the term array.

2 marks
2

Explain why a programmer would store 30 marks in an array rather than in 30 separate variables.

2 marks
3

Write declarations for: (a) Temperatures, holding 7 real values (b) Seats, a table of 10 rows and 20 columns of Boolean values.

2 marks
4

Marks holds 12, 7, 19, 4, 15 at indexes 1–5. State the value of (a) Marks[3] (b) Marks[Marks[2] - 4].

2 marks

Tracing and errors

5

State the contents of Grid after this code runs.

FOR Row ← 1 TO 2
    FOR Col ← 1 TO 3
        Grid[Row, Col] ← Row * Col
    NEXT Col
NEXT Row
3 marks
6

DECLARE Scores : ARRAY[1:10] OF INTEGER is followed by FOR Index ← 0 TO 10 and INPUT Scores[Index]. Identify the error and correct it.

2 marks

Writing pseudocode

7

Write an algorithm to input 7 temperatures into Temperatures and output their average.

4 marks
8

Write an algorithm to set every element of Seats (question 3b) to FALSE.

3 marks
9

Names[1:30] and Marks[1:30] store each student's name and mark at the same index. Write an algorithm to output the name of every student with a mark of 80 or more.

4 marks

How well do you know this?

Rate each specification point. It is saved to your tracker as a new revision round.

All points:
  • 8.2.1Declare and use one-dimensional (1D) and two-dimensional (2D) arrays
  • 8.2.2Understand the use of arrays
  • 8.2.3Write values into, and read values from, an array using iteration

Settings

Your subjects

Untick the subjects you don't take. They leave the menu, the home page and the tracker. Your ratings are kept, so you can bring a subject back at any time.

Page style

Colours