66 exam-style questions plus unlimited generated ones on this topic, at up to four difficulty levels, with full mark schemes. Notes and past-paper booklets are open to everyone. Practice needs a free account.
Or take a quick quiz: 10 questions on this subtopic in GCSE Computer Science (OCR), marked as you tap.
Arrays store many items of the same data type under one name, using an index to pick out each item; 2D arrays store data in rows and columns. Records group related fields about one item, which can have different data types. Expect code that loops through arrays and questions about records and fields in a table.
Key facts
Array: a fixed number of items of the same data type, under one identifier
Indexes start at 0: array names[5] has names[0] to names[4]
Access: names[2]; assign: names[2] = "Jo"
Loop through an array: for i = 0 to length − 1
2D array: two indexes, e.g. grid[2, 3]; use nested loops
Record: related fields about one item; fields can be different data types
Database table: row = record, column = field
Notes
1D arrays
diagram
An array stores a fixed number of items of the same data type under one identifier. It is a static structure: its size is fixed and cannot change while the program runs.
Each item (element) is picked out by its index. Indexes start at 0.
array scores[5] declares an array of 5 elements: scores[0] to scores[4].
A 1D array: 5 elements, indexes 0 to 4
scores[0] = 12 stores a value; print(scores[4]) outputs the last element.
Loop through every element:
total = 0
for i = 0 to 4
total = total + scores[i]
next i
2D arrays
diagram
A 2D array is like a table, with two indexes: array grid[3, 4] holds 3 × 4 = 12 elements. A 2D array can emulate a database table: one row per record, one column per field.
An element is accessed with both indexes, for example grid[2, 3]. The question tells you which index is the row and which is the column.
A 2D array: 3 × 4 = 12 elements, each picked out by two indexes
To visit every element, use two nested loops: one for each index.
Records
A record is a collection of related data items, called fields, about one thing.
The fields can have different data types. For example, a record for a film could have Title (string), Year (integer), Rating (real) and Subtitled (Boolean).
In a database table, each row is a record and each column is a field.
Difference: all elements of an array have the same data type, but the fields of a record can have different data types.
How to answer each type of question
State values or indexes in an array
1 mark eachGrade 4
Number the elements from 0.
Read off the element at the index given, or give the index of the value asked for.
Example. The array temps holds the values 14, 17, 9, 21, 12, in that order. (a) State the value of temps[3]. (b) State the index of the value 9. (c) State the value of temps[0] + temps[4]. [3]
Show the model answerHide the model answer
(a) 21
(1)
(b) 2
(1)
(c) 26
(1)
Write code to process a 1D array
4 marksGrade 6
Set any totals or counters to 0.
Loop from index 0 to the last index.
Inside the loop, update the total or counter using array[i].
Output the results after the loop.
Example. The array temps holds 7 daily temperatures as real numbers. Write code that outputs the mean temperature and the number of days above 20. [4]
Show the model answerHide the model answer
total = 0
hot = 0
for i = 0 to 6
total = total + temps[i]
if temps[i] > 20 then
hot = hot + 1
endif
next i
print(total / 7)
print(hot)
Loop through all 7 elements, index 0 to 6 (1); running total of the temperatures (1); counts the values above 20 (1); outputs the mean and the count after the loop (1).
Write code to process a 2D array
4 marksGrade 8
Use an outer loop for the first index and an inner loop for the second.
Reset a total at the start of the outer loop if you need one per row.
Output inside the outer loop but after the inner loop for a result per row.
Example. The 2D array sales stores the sales of 4 shops over 3 months. The first index (0 to 3) is the shop and the second index (0 to 2) is the month. Write code that outputs the total sales for each shop. [4]
Show the model answerHide the model answer
for shop = 0 to 3
total = 0
for month = 0 to 2
total = total + sales[shop, month]
next month
print(total)
next shop
Outer loop for the 4 shops (1); total reset to 0 for each shop (1); inner loop for the 3 months adding sales[shop, month] (1); outputs each shop's total after the inner loop (1).
Explain records and fields
2 marksGrade 5
Say that a record groups related data about one item.
Say that the fields can have different data types, unlike an array.
Example. A club stores each member's ID, name, age and whether they have paid. Explain why a record is a suitable way to store this data. [2]
Show the model answerHide the model answer
A record groups all the related data about one member together (1). Its fields can have different data types, such as a string for the name, an integer for the age and a Boolean for whether they have paid, which an array cannot do (1).
Don’t lose marks
Starting at index 1, or looping to n instead of n − 1, which goes past the end of the array.
Resetting a total inside the inner loop instead of the outer loop (or not resetting it at all).
Saying an array can hold different data types.
Mixing up records (rows) and fields (columns).
Using the wrong index order in a 2D array: check what the question says each index means.
More tips
Memory tricks
Array of size n: indexes 0 to n − 1. 'Size 5, last index 4.'
Nested loops for 2D arrays: the inner loop finishes all its passes for each single pass of the outer loop.
Record = one row about one thing; field = one column, one piece of data.
Exam technique
Write the index numbers above an array given in a question before answering.
Use the array name and index meanings exactly as the question defines them.
When asked for 'each' shop, row or player, you almost always need nested loops with a reset.
What each grade needs
What you need to be able to do, from the first marks up to the top grade.
Grade 4
Access an array element by its indexRemember the first element is at index 0, so names[2] is the third item.
Grade 5
Declare and assign values in an arrayUse array scores[5] to declare it, then scores[0] = 12 to assign a value.
Grade 5
Explain what a record isA group of related fields about one item, which can be of different data types.
Grade 6
Loop through a 1D arrayUse for i = 0 to length − 1 to total, count or search the items.
Grade 7
Use a 2D arrayAccess an element with two indexes, such as grid[2, 3].
Grade 8
Process a 2D array with nested loopsUse an outer loop for one index and an inner loop for the other.
Quick recall
Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.
A program contains this line of code:
colours = ["red", "green", "blue", "yellow"]
State the value of colours[2].
blue
Look at this program.
01 array colours[5]
02 colours[0] = "red"
03 colours[1] = "blue"
04 colours[4] = "green"
05 array gameboard[8, 8]
06 for r = 0 to 7
07 for c = 0 to 7
08 gameboard[r, c] = "-"
09 next c
10 next r
11 gameboard[0, 4] = "K"
State the number of elements in the array colours.
5
Complete the sentences about arrays. Write one word or phrase in each gap. An array has a ________ size once it has been declared.
fixed
Write the letters in the order the lines must run so that the program outputs 50. Give a reason for the position of line B.
B, C, A
Give two reasons why an array is better than ten separate variables.
One identifier holds all the marks, and a loop can use the index to process each one.
Describe what is meant by the index of an element in an array.
The number that shows the position of an element in the array.
Sample questions
Written for this site in the style of OCR exam questions. They are not taken from real past papers.
Question 1Easy4 marks
A program contains this line of code:
colours = ["red", "green", "blue", "yellow"]
(a) State the value of colours[2].
[1]
(b) State the index of "red" in the array.
[1]
(c) Write a line of code that changes "green" to "purple".
[1]
(d) Give one reason why an array is used rather than four separate variables.
[1]
Show the answer and mark scheme
(a)Answer: blue
blue
(b)Answer: 0
0
(c)
colours[1] = "purple"
(d)
the items can be processed using a loop
one identifier is used for all the related items
less code is needed / easier to add more items
Question 2Medium5 marks
This program should output the mean of the temperatures stored in the array temps. Three parts are missing.
01 temps = [12.5, 14.0, 9.5, 11.0, 13.5, 13.0, 10.5]
02 total = 0
03 for i = 0 to ....(1)....
04 total = ....(2)....
05 next i
06 average = ....(3)....
07 print(average)
(a) Write the missing code for each of the gaps (1) to (3).
[3]
(b) State the output of the completed program.
[1]
(c) Explain why the loop on line 03 must stop at 6 and not at 7.
[1]
Show the answer and mark scheme
(a)Answer: (1) 6 or temps.length - 1; (2) total + temps[i]; (3) total / 7 or total / temps.length
(1) 6 or temps.length - 1
(2) total + temps[i]
(3) total / 7 or total / temps.length
(b)Answer: 12
12 (allow 12.0)
(c)
the seven items have indexes 0 to 6, so temps[7] does not exist / would be out of range
Question 3Hard7 marks
A 2D array scores holds the scores of 5 students in 4 tests. The first index is the student number (0 to 4) and the second index is the test number (0 to 3).
(a) Write an algorithm that outputs, for each student, the student number and that student’s highest score. You must use either OCR Exam Reference Language or a high-level programming language that you have studied.
[6]
(b) Explain why highest must be reset at the start of each student.
[1]
Show the answer and mark scheme
(a)Answer:
for s = 0 to 4
highest = scores[s, 0]
for t = 1 to 3
if scores[s, t] > highest then
highest = scores[s, t]
endif
next t
print(str(s) + ": " + str(highest))
next s
outer loop for each student from 0 to 4
sets highest to that student’s first score (or a very low value) at the start of each student
inner loop through the tests
compares scores[student, test] with highest
updates highest when a larger score is found
outputs the student number and highest once for each student, after the inner loop
(b)
otherwise a higher score from an earlier student would be carried over and output for the next student