Worked solution from the Algorithm design and problem-solving Paper 2 Algorithms, Programming and Logic booklet, Part 8 of 8.
Plan: declare the arrays, fill RandomNumber, count the frequencies, sort the 2D array (swapping both columns together), then work out and output each chance. Add comments and use meaningful names. Change the example sort to descending by using < instead of >.
// declare arrays and variables
DECLARE RandomNumber : ARRAY[1:100000] OF INTEGER
DECLARE CountedNumber : ARRAY[1:10, 1:2] OF INTEGER
DECLARE Index1 : INTEGER
DECLARE Index2 : INTEGER
DECLARE Swap : BOOLEAN
DECLARE Temp1 : INTEGER
DECLARE Temp2 : INTEGER
// generate 100000 random integers from 1 to 10 and store them
FOR Index1 ← 1 TO 100000
RandomNumber[Index1] ← ROUND(RANDOM() * 9, 0) + 1
NEXT Index1
// set up CountedNumber: column 1 holds the integer, column 2 its frequency
FOR Index1 ← 1 TO 10
CountedNumber[Index1, 1] ← Index1
CountedNumber[Index1, 2] ← 0
NEXT Index1
// count how many times each integer was generated
FOR Index1 ← 1 TO 10
FOR Index2 ← 1 TO 100000
IF RandomNumber[Index2] = CountedNumber[Index1, 1]
THEN
CountedNumber[Index1, 2] ← CountedNumber[Index1, 2] + 1
ENDIF
NEXT Index2
NEXT Index1
// bubble sort into descending order of frequency
Swap ← TRUE
WHILE Swap = TRUE DO
// stop when a full pass makes no swap
Swap ← FALSE
FOR Index1 ← 1 TO 9
IF CountedNumber[Index1, 2] < CountedNumber[Index1 + 1, 2]
THEN
Temp1 ← CountedNumber[Index1, 1]
Temp2 ← CountedNumber[Index1, 2]
CountedNumber[Index1, 1] ← CountedNumber[Index1 + 1, 1]
CountedNumber[Index1, 2] ← CountedNumber[Index1 + 1, 2]
CountedNumber[Index1 + 1, 1] ← Temp1
CountedNumber[Index1 + 1, 2] ← Temp2
Swap ← TRUE
ENDIF
NEXT Index1
ENDWHILE
// output each integer with its chance, rounded to 4 decimal places
FOR Index1 ← 1 TO 10
OUTPUT "The chance of ", CountedNumber[Index1, 1], " occurring is: "
OUTPUT ROUND(CountedNumber[Index1, 2] / 100000, 4)
NEXT Index1Arrays and variables declared with suitable data types, including RandomNumber[] and CountedNumber[] [2]
CountedNumber set up with the integers 1 to 10 and frequencies starting at 0 [1]
A loop that runs 100000 times generating random integers from 1 to 10 (RANDOM and ROUND) [1]
Each random integer stored in RandomNumber[] [1]
Frequency of each integer counted (loop with a selection test) [1]
Frequencies stored in CountedNumber[] [1]
Sort in descending order using a loop that repeats until no swaps are made [1]
Comparison uses the frequency column and the swap moves both the integer and its frequency [2]
Chance worked out as frequency / 100000 [1]
Chance rounded to 4 decimal places [1]
All ten integers output in stored order with their chance and a suitable message [1]
Code is fully commented to explain how it works [1]
Meaningful identifier names used throughout [1]
Other correct methods score, for example counting the frequencies in the same loop that generates the numbers.
▶ Whole paper
Video solution of the whole paper · by MAK CS. 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.