OCR GCSE Computer Science June 2022 Paper 2, Question 5(d): two logic errors in a loop

OCR J277/02 June 2022, Question 5(d): a loop that should count the people in nine hotel rooms starts at index 1 and resets its total on every pass. Both logic errors explained, with the broken and fixed program to run.

Past paper questionOCR J277/02June 2022 Paper 22 marksFix the code

Question 5(d) of the OCR GCSE Computer Science Paper 2 sat on 27 May 2022 (J277/02) shows a five line program with two logic errors and asks how to remove them. It is worth 2 marks, one for each error.

We do not copy the exam paper here. Open it beside this page: OCR June 2022 J277/02 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

A hotel has nine rooms, numbered 0 to 8. An array called room stores how many people are in each: 2, 1, 3, 2, 1, 0, 0, 4, 1.

The program should count the people in the hotel. It loops count from 1 to 8. Inside the loop it sets total = 0, then adds room[count] to total. After the loop it prints total.

Find the errors

The loop starts at 1. Room 0 exists, and two people are in it. The loop should run from 0 to 8.

total = 0 is inside the loop. So the total is wiped on every pass, just before one room is added. Whatever is printed at the end is only the last room. total = 0 must come before the loop starts.

Corrected:

total = 0
for count = 0 to 8
    total = total + room[count]
next count
print(total)

How to find errors like these

Trace two passes. count is 1: total becomes 0, then 1. count is 2: total becomes 0, then 3. The 1 has gone. Two passes are enough to see that the total never builds up.

Then check the ends of the loop against the ends of the array. The array starts at 0. The loop starts at 1. That is the other error.

Where the marks are lost

  • "Move total = 0 outside the loop." The mark scheme refuses this wording. Outside could mean after the loop, which is still wrong. Say before the loop.
  • "Move it to the top of the loop." It is already there. It must leave the loop.
  • Changing 8 to 9. The last room is index 8, and an OCR for loop includes its end value. The end of the loop is right.

Run it

FIXED = False is the program on the paper. It prints 1, which is the number of people in room 8 and nothing else. The robot drives 1 cm for each person it counted.

FIXED = False prints 1: only the last room. FIXED = True prints 14, the real number of guests.
The program
from bugbot import *
connect()

# False is the program on the paper. True is the corrected one
FIXED = False

room = [2, 1, 3, 2, 1, 0, 0, 4, 1]

if FIXED:
    total = 0
    for count in range(0, 9):
        total = total + room[count]
else:
    for count in range(1, 9):
        total = 0
        total = total + room[count]
print(total)
forward(60, distance=total)
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What is the answer to OCR J277 June 2022 Paper 2 Question 5(d)?

Change the loop so that it starts at 0, and move total = 0 to before the loop starts.

Why must a running total be set to zero before the loop?

The loop adds to the total on every pass. If the total is reset inside the loop, everything added so far is lost each time, and only the last value survives.

What is an off by one error?

A loop that starts or stops one place away from where it should, such as starting at index 1 when the array starts at index 0. The program runs, but misses an item or reads one that is not there.

More from this paper

Every OCR J277 question we have worked

Learn it step by step

  1. F6.4 Debugging logic errors Robust programs
  2. F3.4 Iterating over a list Strings, lists and records
Open the lessons

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.