OCR GCSE Computer Science sample Paper 2, Question 8(e) and (g): a trace table, a function and a flowchart

OCR J277/02 sample assessment material, Question 8(e) and (g): trace a while loop that subtracts a growing number, complete a function that converts hours and minutes to minutes, and rewrite a flowchart as a program. Worked through, with everything to run.

Past paper questionOCR J277/02Sample Paper 214 marksTrace table

The last parts of Section B in OCR's sample assessment material for GCSE Computer Science Paper 2 (J277/02) are a 4 mark trace table (e), and part (g), which has a function to complete (4 marks) and a flowchart to turn into code (4 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 (e): the trace table

x starts at 15 and y at 0. While x is greater than 0: add 1 to y, then take y off x. After the loop, print y.

The amount taken off grows each pass: 1, then 2, then 3.

x y output
15 0
14 1
12 2
9 3
5 4
0 5
5

When x reaches 0 the condition x > 0 is false, so the loop ends and 5 is output. One mark for the first row, one for rows 2 and 3, one for rows 4 to 6, and one for the output being the only value in its column.

Part (g)(ii): the function

Hours and minutes are input. Complete the main program's call and the function that returns the total in minutes.

hours = input("Please enter number of hours played")
minutes = input("Please enter number of minutes played")
finalTotal = totalMins(hours, minutes)
print(finalTotal)

function totalMins(hours, minutes)
    total = (hours * 60) + minutes
    return total
endfunction

The four marks: the call with the two variables; the parameters used inside the function; the calculation; the total returned.

Part (g)(iii): the flowchart as a program

The flowchart inputs the minutes played, and outputs one message if it is more than 120 and another if not.

minutes = input("Enter minutes played")
if minutes > 120 then
    print("You played games for too long!")
else
    print("You are under your time limit!")
endif

One mark each for the input, the comparison with 120, and each of the two outputs in the right branch.

Where the marks are lost

  • Taking 1 off x every time. y grows, so the amount subtracted grows. Trace it line by line.
  • Writing 5 and 4 in the output column. Only the final print(y) outputs anything.
  • hours + minutes * 60. It is the hours that are multiplied by 60. The mark scheme was corrected on exactly this point.
  • print(total) inside the function. The main program prints. The function must return.
  • Pseudocode in part (g)(iii). It is Section B: real code only.

Run it

All three parts. The robot drives y centimetres at the end of the trace.

The trace prints six rows and outputs 5. Then enter 2 hours and 15 minutes for 135, which is over the limit.
The program
from bugbot import *
connect()

x = 15
y = 0
print("x y")
print(x, y)
while x > 0:
    y = y + 1
    x = x - y
    print(x, y)
print("output", y)
forward(60, distance=y)

def totalMins(hours, minutes):
    total = (hours * 60) + minutes
    return total

hours = int(input("Please enter number of hours played: "))
minutes = int(input("Please enter number of minutes played: "))
finalTotal = totalMins(hours, minutes)
print(finalTotal)

if finalTotal > 120:
    led("red")
    print("You played games for too long!")
else:
    led("green")
    print("You are under your time limit!")
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(e)?

x goes 15, 14, 12, 9, 5, 0. y goes 0, 1, 2, 3, 4, 5. The output is 5.

What is the answer to Question 8(g)(ii)?

finalTotal = totalMins(hours, minutes), and a function totalMins(hours, minutes) that sets total to hours * 60 + minutes and returns it.

How do I turn a flowchart decision into code?

The diamond becomes an if with its condition. The Yes branch goes under the if, the No branch under the else, and both carry on together after the endif.

More from this paper

Every OCR J277 question we have worked · Guide: Trace tables explained

Learn it step by step

  1. F13.2 Trace tables Exam preparation
  2. F4.2 Parameters and return values Functions and structured code
  3. F5.2 Flowcharts Algorithms
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.