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

Q117 · 2210/23/O/N/25 Q10

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

Q1172210/23/O/N/25original Q10Structured · 15 marks
Question 10 from 2210/23/O/N/25

Worked solution

Plan: input and check the names, fill NumberGenerated[player, number], compare each pair to give points, break any tie with extra random numbers, then output the players in order. Comment the code and use clear names.

// input and validate the player names (presence check)
OUTPUT "Please enter the name of player 1 "
INPUT Player1
WHILE Player1 = "" DO
    OUTPUT "You must enter a name, try again "
    INPUT Player1
ENDWHILE
OUTPUT "Please enter the name of player 2 "
INPUT Player2
WHILE Player2 = "" DO
    OUTPUT "You must enter a name, try again "
    INPUT Player2
ENDWHILE
// start both totals at 0
Points[1] ← 0
Points[2] ← 0
// generate 100 random numbers from 1 to 6 for each player
FOR X ← 1 TO 2
    FOR Y ← 1 TO 100
        NumberGenerated[X, Y] ← ROUND(RANDOM() * 5, 0) + 1
    NEXT Y
NEXT X
// compare each pair of numbers and award points
FOR X ← 1 TO 100
    IF NumberGenerated[1, X] > NumberGenerated[2, X]
      THEN
        Points[1] ← Points[1] + 2
      ELSE
        IF NumberGenerated[1, X] < NumberGenerated[2, X]
          THEN
            Points[2] ← Points[2] + 2
          ELSE
            // same number: 1 point each
            Points[1] ← Points[1] + 1
            Points[2] ← Points[2] + 1
        ENDIF
    ENDIF
NEXT X
// if the totals are equal, generate new numbers until they differ
IF Points[1] = Points[2]
  THEN
    REPEAT
        P1 ← ROUND(RANDOM() * 5, 0) + 1
        P2 ← ROUND(RANDOM() * 5, 0) + 1
    UNTIL P1 <> P2
    IF P1 > P2
      THEN
        Points[1] ← Points[1] + 2
      ELSE
        Points[2] ← Points[2] + 2
    ENDIF
ENDIF
// output the winner first, then the other player
IF Points[1] > Points[2]
  THEN
    OUTPUT "First place is ", Player1, " with a total of ", Points[1], " points"
    OUTPUT "Second place is ", Player2, " with a total of ", Points[2], " points"
  ELSE
    OUTPUT "First place is ", Player2, " with a total of ", Points[2], " points"
    OUTPUT "Second place is ", Player1, " with a total of ", Points[1], " points"
ENDIF

Both player names input with suitable prompts [1]

Validation loop for each name (for example a presence check) with an error message [1]

Loops that run 100 times for each of the two players [1]

Random whole numbers from 1 to 6 generated (RANDOM and ROUND) [1]

Each number stored in the correct place in NumberGenerated[] [1]

Points totals for both players set to 0 and stored [1]

Each pair compared and 2 points given to the higher number [1]

Equal numbers give 1 point to each player [1]

If the totals are equal, a loop generates new numbers until they are different [1]

The player with the higher new number gets 2 more points [1]

The winner's name and total output first, with a suitable message [1]

The other player's name and total output second, with a suitable message [1]

Code is fully commented to explain how it works [1]

Meaningful identifier names used throughout [1]

The solution is in a logical order and meets all the requirements [1]

Other correct solutions also score.

KEY IDEAUse loops for input checks and for generating and comparing the numbers, selection for awarding points, and a tie-break loop that repeats until the players differ.
TOTAL 15 marks

Official mark scheme

Mark scheme for 2210/23/O/N/25 question 10
VIDEO
▶ Topic lesson

A lesson on this topic · by Mr Bulmer's Learning Zone. 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.