OCR GCSE Computer Science June 2023 Paper 2, Question 6(f): totalling from a 2D array
OCR J277/02 June 2023, Question 6(f): name the process that turns an integer into a string, then write a program that totals the seconds for one date from a 2D string array. Model answers, the six marks explained, and the program to run.
Question 6(f) is the last question of the OCR GCSE Computer Science Paper 2 sat on 25 May 2023 (J277/02). Part (i) is one word (1 mark). Part (ii) is a 6 mark program that searches a two-dimensional array and adds up the matching rows. Searching a 2D array with a loop is a standard pattern, so it is worth having ready.
We do not copy the exam paper here. Open it beside this page: OCR June 2023 J277/02 question paper (PDF). When you have finished, check the mark scheme too.
The question in short
The alarm log has seven events. Each has a date, a sensor ID, a sensor type and a length in seconds. They are held in a 2D string array. The paper tells you that element [1, 1] contains "MS1".
Part (i). An array holds one data type, so the lengths have to be turned into strings before they are stored. Name that process.
Part (ii). Write a program that asks the user for a date, totals the seconds for the events on that date, and outputs the total in a message that includes the date, such as: Sensors were activated for 40 seconds on 05/02/2023.
Part (i)
Casting. The mark scheme refuses "conversion" and refuses examples such as str() on their own. It wants the name.
Plan before you write
This is a running total with a condition on it.
- Input the date.
- Set a total to 0.
- Loop over all seven events, indexes 0 to 6.
- If the event's date (column 0) matches, add its length (column 3) to the total.
- After the loop, output the message.
One detail: the lengths are stored as strings. To add them up they must be cast back to integers.
A model answer
In OCR Exam Reference Language:
date = input("Enter a date")
total = 0
for count = 0 to 6
if arrayEvents[count, 0] == date then
total = total + int(arrayEvents[count, 3])
endif
next count
print("Sensors were activated for " + str(total) + " seconds on " + date)
In Python a 2D array is a list of lists, and the two index numbers have their own brackets:
date = input("Enter a date: ")
total = 0
for count in range(7):
if arrayEvents[count][0] == date:
total = total + int(arrayEvents[count][3])
print("Sensors were activated for", total, "seconds on", date)
Where the six marks are
- inputting the date and storing it;
- reaching all seven events, indexes 0 to 6;
- using selection;
- comparing the date entered with the date in the array (element 0);
- adding the length (element 3) to the total when they match;
- outputting the total and the date in a message, at the end.
The mark scheme accepts the two index numbers either way round, accepts events as the array name, and does not punish an off by one in a Python loop. It does not ask for the cast, but without it real code would fail, so put it in.
Where the marks are lost
- Looping 1 to 7. The array is 0-indexed. The mark scheme says it must be 0 to 6.
- No
total = 0.total = total + ...fails iftotaldoes not exist yet. - Outputting inside the loop only. The message must come out at the end, once the total is complete.
- Leaving the date out of the message. The question asks for it, and shows you an example.
- Comparing the wrong column. The date is element 0 and the length is element 3. Write the column numbers above the table on the paper.
Run it
The program in Python with the table from the paper. The robot drives 1 cm for every second in the total.
The program
from bugbot import *
connect()
arrayEvents = [["05/02/2023", "WS2", "Window", "38"],
["05/02/2023", "MS1", "Motion", "2"],
["06/02/2023", "DS3", "Door", "1"],
["06/02/2023", "MS2", "Motion", "3"],
["06/02/2023", "MS1", "Motion", "2"],
["07/02/2023", "WS1", "Window", "24"],
["07/02/2023", "DS1", "Door", "1"]]
date = input("Enter a date: ")
total = 0
for count in range(7):
if arrayEvents[count][0] == date:
total = total + int(arrayEvents[count][3])
print("Sensors were activated for", total, "seconds on", date)
if total > 0:
forward(60, distance=min(total, 60))
Now change it
Take the int() out and run it. Python stops with an error, because it will not add a string to a number. Now change total = 0 to total = "" as well and run it again with 05/02/2023. What is the "total", and why?
Answer
It prints 382. With two strings, + joins them end to end: "38" and "2" make "382". That is concatenation, not addition, and it is why the cast back to an integer matters.Questions
What is the answer to OCR J277 June 2023 Paper 2 Question 6(f)(ii)?
Input a date and set total to 0. Loop count from 0 to 6. If arrayEvents[count, 0] equals the date, add int(arrayEvents[count, 3]) to total. After the loop, print the total and the date in a message.
What is casting?
Casting is changing a value from one data type to another, such as the integer 38 to the string "38" with str(), or back again with int().
How do you index a 2D array in OCR Exam Reference Language?
With both index numbers in one pair of square brackets, separated by a comma, such as arrayEvents[1, 1]. In Python each index has its own brackets: arrayEvents[1][1].
More from this paper
- Question 5(c): An adding game: three random questions and a score 6 marks
- Question 6(b): Sound an alarm when armed AND (door OR window) 4 marks
- Question 6(e): A procedure with two parameters that writes to a text file 6 marks
Every OCR J277 question we have worked
Learn it step by step
- F3.5 Two-dimensional arrays Strings, lists and records
- F1.8 Data types and casting Programming basics
- F13.4 Programming questions Exam preparation
This is our own explanation of a published exam question. It is not written or endorsed by OCR, and the question paper and mark scheme remain OCR's copyright. Read them on OCR's site with the links on this page.