← Computer Science

Arrays: storing lists of data

An array stores many values of the same type under one name, reached by an index number. This lesson explains why an array beats a pile of separate variables, how to declare and index 1D and 2D arrays the Cambridge way, and how to walk through an array with a FOR loop.

Slide 1 / 4

Why an array beats separate variables

One array replaces many separate variablesSeparate variablesmark1 = 40mark2 = 55mark3 = 70...mark30 = 48One array: Marks40[1]55[2]70[3]48[4]process them allin one loop
  • To store 30 test marks with plain variables you would need mark1, mark2, mark3 and so on up to mark30, which is clumsy and hard to process.
  • An array stores all those values under one name, and you pick out each value using an index number in square brackets.
  • Because the values share one name, you can process them all in a single loop that changes only the index.
  • Every element of an array must be the same data type, for example all INTEGER or all STRING.

Check you've got it

0/4

1. After these statements, what value is stored in Scores[2]? Scores[1] <- 40 Scores[2] <- 55 Scores[3] <- 70

2. In an array declared as ARRAY[1:3, 1:4] OF INTEGER, what does Grid[2,3] refer to?

3. What value is output by this code? DECLARE nums : ARRAY[1:4] OF INTEGER nums[1] <- 2 nums[2] <- 4 nums[3] <- 6 nums[4] <- 8 total <- 0 FOR i <- 1 TO 4 total <- total + nums[i] NEXT i OUTPUT total

4. What is the main advantage of using an array instead of 30 separate variables to hold 30 marks?

Practise exam-style questions on this

Type your answer, see your mark against the mark scheme in seconds. Free, 5 a day.

Start practising →

Teaching this topic?

Mark your whole class's answers and see who's stuck, for £19 a month.

See the class plan →

Free revision content. Multiple-choice answers are marked instantly. Written answers are marked by AI on /free-mark. Independent, not endorsed by Cambridge International.