Note AcademyNote Academy

Computer Science · Oct / Nov 2025 · Paper 22 · Question 8

0478

As printed in the paper

The question as text

A program is required to test the fairness of the random number generator. The one-dimensional (1D) array } ¿~–•–·˜†Š is used to store 100 000 random integers. The two-dimensional (2D) array z~• €˜¿•–·˜†Š is used to store the integers 1 to 10, inclusive, and the frequency of each integer (the number of times each integer was generated). Write a program that meets the following requirements: • Generate 100 000 random integers between 1 and 10, inclusive. • Store each integer in the appropriate array. • Calculate the frequency of each of the integers 1 to 10 and store these values in the appropriate array. • Sort the contents of the counted number array in descending order of frequency. • Calculate the chance of generating each of the integers 1 to 10, rounded to four decimal places. Use the formula: chance = frequency / 100000 • Output each of the integers 1 to 10 in the order they are stored in the counted number array, along with the chance of generating them. The following is an example of an algorithm that sorts a 2D array in an ascending order: Œ ‘}Ÿy ¢£{y Œ x‹ Œ Œ|{šy Œ‹}  v ‘‹ ¡ Œ |†ˆ Š ª |† © vˆ Š ‘£y ‘v |†ˆ vŠ ‘ |†ˆ Š |†ˆ vŠ |† © vˆ vŠ |†ˆ Š |† © vˆ Š |† © vˆ vŠ ‘v |† © vˆ Š ‘ Œ ‘}Ÿy yxŒ y¨‘  yx¢£{y You must use pseudocode or program code and add comments to explain how your code works. All arrays, variables or constants used must be declared for this algorithm. All inputs and outputs must contain suitable messages.

Read automatically from the paper; the printed page above is exact.

Show the answer and mark scheme

Use the tables for AO2 and AO3 below to award a mark in a suitable band using a best fit approach, then add up the total. Marks are available for:

AO2 (maximum 9 marks)
AO3 (maximum 6 marks)

Data Structures required with names as given in the scenario:

Arrays or lists RandomNumber[], CountedNumber[]

Requirements (techniques)

R1 arrays and variables declared, then 100 000 random integers between 1 and 10, inclusive, generated and stored in an array, (array and variable declarations, iteration, random number generation, rounding and storage).

R2 frequency of each random integer counted and results stored in array, then results sorted into descending order (initialisation, selection, counting, sort, nested iteration, storage).

R3 calculation of the chance of each random integer occurring rounded to 4 d.p. and results output (iteration, rounding and output).

Example 15-mark answer in pseudocode

// array and variable declaration

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 between 1 and 10, inclusive,

// and store them in an array

FOR Index1 ← 1 TO 100000

RandomNumber[Index1] ← ROUND(RANDOM() * 9, 0) + 1

NEXT Index1

// initialising the CountedNumber array

FOR Index1 ← 1 TO 10

CountedNumber[Index1, 1] ← Index1

CountedNumber[Index1, 2] ← 0

NEXT Index1

// counting the frequency of each of the random integers

// and storing the frequencies in an array

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

// performing sort on frequencies, so the possible random numbers

// are stored in descending order of frequency

Swap ← TRUE

WHILE Swap DO

// sort is terminated when no swaps take place during a pass

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

// outputting the results, with chances for each random number

// calculated to 4 d.p.

// results in descending order of frequency

FOR Index1 ← 1 TO 10

OUTPUT "The chance of ", CountedNumber[Index1, 1], " occurring is: "

OUTPUT ROUND(CountedNumber[Index1, 2]/100000, 4)

NEXT Index1

Alternative answer for first 20 lines after declarations, where RandomNumber[] is populated and frequency counted in the same loop.

// initialising the CountedNumber array

FOR Index1 ← 1 TO 10

CountedNumber[Index1, 1] ← Index1

CountedNumber[Index1, 2] ← 0

NEXT Index1

// generate random integers in an array,

// counting and storing the frequencies in an array

FOR Index1 ← 1 TO 100000

Temp1 ← ROUND(RANDOM() * 9, 0) + 1

RandomNumber[Index1] ← Temp1

CountedNumber[Temp1, 2] ← CountedNumber[Temp1, 2] + 1

NEXT Index1

// performing sort on frequencies, so the possible random numbers

AO2:

Apply knowledge and understanding of the principles and concepts of computer science to a given context, including the analysis and design of computational or programming problems

1–3

4–6

7–9

Make a free account to save your answers and see your weak topics and ask the AI why a wrong answer is wrong.