OCR GCSE Computer Science sample Paper 2, Question 8(b) to (d): validation and a 2D array

OCR J277/02 sample assessment material, Question 8(b), (c) and (d): complete a range check with OR, plan its tests, index a two-dimensional array of minutes played, and refine five repeated lines into a loop. Worked through, with the programs to run.

Past paper questionOCR J277/02Sample Paper 210 marksWrite a program

Question 8 is the whole of Section B in OCR's sample assessment material for GCSE Computer Science Paper 2 (J277/02). This page works through parts (b), (c) and (d): a validation check and its tests, indexing a 2D array, and turning repeated lines into a loop. Together they are 10 marks.

We do not copy the paper here. Open it beside this page: OCR J277/02 sample question paper and mark scheme (PDF). The mark scheme is in the same document.

Part (b)(i): the range check (3 marks)

Minutes played must be from 0 to 300 inclusive. The skeleton reads mins and starts if mins < 0 ...... mins ...... then, with an output line to complete.

mins = input("Enter minutes played: ")
if mins < 0 or mins > 300 then
    print("Invalid input")
endif

The three gaps: or, > 300 (or >= 301), and print. This is Section B, so it must be real code: "greater than" in words gets nothing.

Part (b)(ii): the test plan (2 marks)

Test data Test type Expected result
25 Normal Value accepted
350 Invalid Invalid input message displayed
300 Boundary Value accepted

Any invalid value over 300 (or below 0) gets the first mark. 300 is inside the range, so it is accepted.

Part (c): index the 2D array (1 mark)

minsPlayed holds students across and days down, and the paper shows print(minsPlayed[3,2]) for Dan (column 3) on Wednesday (row 2). So the first index is the student and the second is the day. Stuart (0) on Friday (4):

print(minsPlayed[0,4])

In Python: minsPlayed[0][4].

Part (d): refine five lines into a loop (4 marks)

The given program adds minsPlayed[2,0] to minsPlayed[2,4] on five separate lines. The refined version:

total = 0
for day = 0 to 4
    total = total + minsPlayed[2, day]
next day
print(total)

The four marks: total set to 0 and printed at the end; a loop; a loop that runs five times (0 to 4); adding the array element using the loop counter as the index.

Where the marks are lost

  • and in the range check. No number is both below 0 and above 300.
  • mins > 300 on its own would be the same as the paper, which is one gap short.
  • Reading the array indexes the wrong way. The paper's example tells you the order: student first, then day.
  • for day = 1 to 5. Indexes run from 0 to 4.
  • Keeping minsPlayed[2, 0] inside the loop. The counter must replace the 0, or the same day is added five times.

Run it

The array from the paper, the validation check, Stuart's Friday, and the loop for student 2 (Victoria).

Try 350 for the check: Invalid input. Stuart played 100 minutes on Friday. Victoria's week adds up to 75.
The program
from bugbot import *
connect()

mins = int(input("Enter minutes played: "))
if mins < 0 or mins > 300:
    led("red")
    print("Invalid input")
else:
    led("green")
    print("Value accepted")

# students across (Stuart, Wes, Victoria, Dan), days down (Mon to Fri)
minsPlayed = [[0, 180, 200, 60, 100],
              [60, 60, 30, 10, 35],
              [30, 0, 0, 15, 30],
              [45, 60, 20, 15, 45]]

print("Stuart on Friday:", minsPlayed[0][4])

total = 0
for day in range(0, 5):
    total = total + minsPlayed[2][day]
print("Victoria's week:", 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 sample Paper 2 Question 8(b)(i)?

if mins < 0 or mins > 300 then print("Invalid input").

What is the answer to Question 8(d)?

Set total to 0. Loop a counter from 0 to 4, adding minsPlayed[2, counter] to total each time. Print total after the loop.

Why is a loop more efficient than five repeated lines?

The loop is shorter, works for any number of days by changing one number, and cannot have a typing slip on one of the five lines. It is the standard way to process every element of an array.

More from this paper

Every OCR J277 question we have worked

Learn it step by step

  1. F6.1 Defensive design and validation Robust programs
  2. F3.5 Two-dimensional arrays Strings, lists and records
  3. F2.5 Count-controlled loops: for Decisions and loops
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.