OCR GCSE Computer Science June 2024 Paper 2, Question 8: the moveCharacter function

OCR J277/02 June 2024, Question 8: two ways to make an algorithm more maintainable, then complete a function that moves a character left or right by 5 and keeps it between 1 and 512. A model answer, the six marks explained, and a robot that obeys it.

Past paper questionOCR J277/02June 2024 Paper 210 marksWrite a program

Question 8 of the OCR GCSE Computer Science Paper 2 sat on 21 May 2024 (J277/02) is the biggest question in Section A. Part (a) asks how to make a short algorithm easier to maintain (4 marks). Part (b) asks you to complete a function (6 marks).

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

The question in short

A character stands on a line at a position from 1 to 512. The algorithm on the paper picks a random starting position, stores it in p, and keeps asking for a direction, stored in a, until the user types left or right.

Part (a): two ways to improve maintainability

Each way is 2 marks: name it, then say what it does or give an example from this algorithm.

  • Meaningful identifiers. p and a say nothing. position and direction show what each variable stores.
  • Comments. A note above the loop saying that it repeats until a valid direction is entered helps the next programmer to follow the code.
  • Constants. Store 512 as a constant such as MAXPOSITION, so that it only has to be changed in one place.
  • Subroutines. Put the input loop in a function such as getDirection(), so that it can be reused.

Indentation is refused, because the algorithm is already indented. "Makes it easier to understand" on its own is too vague: say which method you mean, and what it makes easier.

Part (b): complete moveCharacter()

The function takes the direction and the current position. Left subtracts 5. Right adds 5. The result is kept between 1 and 512, and returned.

The header and endfunction are printed for you. You write the middle:

function moveCharacter(direction, position)
    if direction == "left" then
        position = position - 5
    elseif direction == "right" then
        position = position + 5
    endif
    if position < 1 then
        position = 1
    elseif position > 512 then
        position = 512
    endif
    return position
endfunction

Order matters. Move first, then clamp. If you check the limits before moving, the move can push the position back outside them.

Where the six marks are

  • using both parameters, with no extra inputs and nothing that overwrites them;
  • using selection;
  • checking for "left" and subtracting 5;
  • checking for "right" and adding 5;
  • keeping the position between 1 and 512 inclusive;
  • returning the new position.

Where the marks are lost

  • Asking for input inside the function. The direction arrives as a parameter. direction = input() throws it away, and loses the first mark.
  • print(position). The question says returns. Printing is not returning.
  • No quotation marks round left and right. They are strings. Without quotes they are variables that do not exist.
  • Clamping before moving. See above. The mark scheme withholds the limit mark for this.
  • A loop. Nothing repeats. One call is one move.

Run it

The function in Python, tested at both edges, and then used for real: type a direction and the robot slides 5 cm that way.

The three tests print 95, 1 and 512. Then type left or right and the robot moves 5 cm.
The program
from bugbot import *
connect()

def moveCharacter(direction, position):
    if direction == "left":
        position = position - 5
    elif direction == "right":
        position = position + 5
    if position < 1:
        position = 1
    elif position > 512:
        position = 512
    return position

print(moveCharacter("left", 100))
print(moveCharacter("left", 3))
print(moveCharacter("right", 510))

position = 256
direction = ""
while direction != "left" and direction != "right":
    direction = input("Enter direction, left or right: ")
position = moveCharacter(direction, position)
print("The position is", position)
if direction == "left":
    left(60, distance=5)
else:
    right(60, distance=5)
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 2024 Paper 2 Question 8(b)?

If direction is "left" subtract 5 from position, else if it is "right" add 5. Then if position is less than 1 set it to 1, else if it is more than 512 set it to 512. Return position.

What makes a program maintainable?

Meaningful variable names, comments, indentation, subroutines and constants. Each makes the code easier for another programmer to read, understand and change.

What is the difference between a parameter and an input?

A parameter is a value passed into a subroutine by the code that calls it. An input is typed by the user while the program runs. A function that is given its data as parameters should not ask for it again.

More from this paper

Every OCR J277 question we have worked · Guide: Logic gates and truth tables explained

Learn it step by step

  1. F4.2 Parameters and return values Functions and structured code
  2. F1.5 Comments and readable code Programming basics
  3. F2.3 else, elif and Boolean operators 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.