Cambridge O Level Computer Science 2210 · 7.9 Writing and amending algorithms

Q89 · 2210/22/M/J/21 Q2

Worked solution from the Algorithm design and problem-solving Paper 2 Algorithms, Programming and Logic booklet, Part 8 of 8.

Q892210/22/M/J/21original Q2Structured · 8 marks
Question 2 from 2210/22/M/J/21

Worked solution

(a)

Use a loop for 500 numbers, check each is a whole number from 1 to 999, and keep the largest and smallest.

Large ← 0
Small ← 1000
FOR Count ← 1 TO 500
    REPEAT
        OUTPUT "Enter a whole number between 1 and 999"
        INPUT Number
    UNTIL Number >= 1 AND Number < 1000 AND Number = Number DIV 1
    IF Number < Small
      THEN
        Small ← Number
    ENDIF
    IF Number > Large
      THEN
        Large ← Number
    ENDIF
NEXT Count
Range ← Large - Small
OUTPUT "Largest number is ", Large
OUTPUT "Smallest number is ", Small
OUTPUT "Range of numbers is ", Range

Large and Small given starting values before the loop [1]

A loop that runs 500 times [1]

Input of a number with a prompt [1]

A working range check for 1 to 999 [1]

The largest number selected [1]

The smallest number selected [1]

Any six points score. Also credited: a check for a whole number, working out the range, and outputting the largest, smallest and range with a message.

(b)

Write the action and the method:

Action: reduce the number of numbers entered [1]

Method: decrease the final value of the loop [1]

Another answer that scores both marks: remove the need to input values by using random numbers or a previously filled array.

KEY IDEASet the largest and smallest before the loop, then update them with an IF inside the loop.
TOTAL 8 marks

Official mark scheme

Mark scheme for 2210/22/M/J/21 question 2
VIDEO
▶ Whole paper

Video solution of the whole paper · by CS BY AQIB KHAN. Tap the picture to open it on YouTube.

Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.