Question 8

OCR Computer Science June 2024 Computational thinking, algorithms and programming (J277-02)GCSE10 marks

On the specification

(a)
2.3 Producing robust programs
(b)
2.2 Programming fundamentals
Question text

8 An algorithm stores the position of a character on a straight line as an integer. A user can move the character left or right. The following algorithm: • generates one random number between 1 and 512 (inclusive) to store as the position • prompts the user to input a direction to move (left or right) • takes a direction as input until a valid direction is input. p = random(1, 512) print("The position is ", p) a = "" while a != "left" and a != "right" a = input("Enter direction, left or right") endwhile (a) Describe two ways to improve the maintainability of the algorithm. 1 2 [4] (b) If the character moves left, 5 is subtracted from the position. If the character moves right, 5 is added to the position. The position of the character can only be between 1 and 512 inclusive. The function moveCharacter(): • takes the direction (left or right) and current position as parameters • changes position based on direction • sets position to 1 if the new position is less than 1 • sets position to 512 if the new position is greater than 512 • returns the new position. Complete the function moveCharacter() function moveCharacter(direction, position) endfunction [6] We advise you to spend approximately 40 minutes on Section B. Some questions require you to respond using either the OCR Exam Reference Language or a high-level programming language you have studied. These are clearly shown.