OCR GCSE Computer Science June 2022 Paper 2, Question 4(c): total and average
OCR J277/02 June 2022, Question 4(c): write an algorithm that asks how many numbers there will be, inputs that many, and outputs their total and average. Model answers in OCR reference language and Python, the six marks explained, and the program to run.
Question 4(c) of the OCR GCSE Computer Science Paper 2 sat on 27 May 2022 (J277/02) asks for a classic: a running total in a count-controlled loop, then an average. It is worth 6 marks. Earlier parts of the question show a program that adds five numbers using five variables, and this part is the cure.
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
Write an algorithm to:
- ask the user how many numbers they want to enter, and read that value;
- repeatedly take a number as input until that many have been entered;
- calculate and output the total;
- calculate and output the average.
A model answer
In OCR Exam Reference Language:
quantity = int(input("How many numbers?"))
total = 0
for count = 1 to quantity
number = int(input("Enter a number"))
total = total + number
next count
print(total)
print(total / quantity)
In Python:
quantity = int(input("How many numbers? "))
total = 0
for count in range(quantity):
number = float(input("Enter a number: "))
total = total + number
print(total)
print(total / quantity)
Where the six marks are
- inputting the quantity with a message, and storing it;
- an attempt at a loop;
- a loop that repeats the number of times that was input;
- inputting a number inside the loop and adding it to a total;
- working out the average as total divided by quantity;
- outputting both the total and the average.
The mark scheme does not insist on total = 0, and it allows a loop that is one out. Put the total = 0 in anyway: real code fails without it.
Where the marks are lost
- No prompt. The first bullet says ask the user. An
input()with no message does not ask anything, and that mark needs the message. - A loop that runs a fixed number of times.
for count = 1 to 5ignores what the user typed. - The average inside the loop. It is worked out once, after all the numbers are in.
total = number. That replaces the total. It must betotal = total + number.- Dividing by the wrong thing. The average is the total divided by how many numbers there were, not by
countor by 2.
Part (a) of the same question
The program on the paper uses the variables a to f and five near-identical lines. Two ways to make it more maintainable, for a mark each: add comments, use sensible variable names, use a loop, or put the code into a subroutine. Indentation is refused, because there is nothing in that program to indent.
Run it
Type how many numbers, then the numbers. The robot drives the average in centimetres (up to 60).
The program
from bugbot import *
connect()
quantity = int(input("How many numbers? "))
total = 0
for count in range(quantity):
number = float(input("Enter a number: "))
total = total + number
print("Total", total)
average = total / quantity
print("Average", average)
forward(60, distance=max(0, min(average, 60)))
Now change it
Enter 0 for the quantity. Python stops with ZeroDivisionError, because the average of no numbers is 0 divided by 0. Add validation so that the program keeps asking until the quantity is at least 1.
Answer
After reading quantity, add a loop: while quantity < 1, ask for it again. That is a range check with only a lower limit.Questions
What is the answer to OCR J277 June 2022 Paper 2 Question 4(c)?
Input the quantity with a prompt. Set total to 0. Loop quantity times, inputting a number and adding it to total. After the loop, output total and total divided by quantity.
How do you calculate an average in a program?
Add all the values into a running total inside a loop, then divide the total by the number of values after the loop has finished.
Why is a for loop right for this question?
Because the number of repeats is known before the loop starts: the user has just entered it. A count-controlled loop repeats a set number of times.
More from this paper
- Question 2(b): Design a flowchart that decides who gets a half-price meal 5 marks
- Question 5(b): Validate a booking with three checks, and plan the tests 8 marks
- Question 5(c): Write a function that returns a price, then call it 7 marks
- Question 5(e): Car park charges, repeated until 0 hours is entered 6 marks
Every OCR J277 question we have worked
Learn it step by step
- F2.7 Loop patterns Decisions and loops
- F2.5 Count-controlled loops: for Decisions and loops
- F13.4 Programming questions Exam preparation
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.