CAMBRIDGE IGCSE COMPUTER SCIENCE
Winter 2025 · Paper 22 · Question 8 · 15 marks
← 0478 CS Hub Paper 22 · Winter 2025 0478 / Paper 2 Question 8 15 MARKS
15-mark question lab · start here

From 100 000 random integers to a fairness report.

Build the solution in a sensible order: understand the two arrays, declare everything the question demands, generate and store the random integers, count each frequency, adapt the given ascending sort to descending, calculate every chance to four decimal places and output the sorted results with suitable messages. Learn the reasoning before revealing the complete program.

1D + 2D arraysRandom generationFrequency countingAdapting a given sortRounding to 4 dp15 marks

Learning targets

By the end, you should be able to explain the algorithm before writing it.

Lesson overview

01 · Understand

Identify the 1D RandomNumber[] of 100 000 generated integers, the 2D CountedNumber[] of integer-plus-frequency rows, and the given ascending snippet that must be adapted to descending.

02 · Construct

Generate and store 100 000 integers in the range 1 to 10, count each frequency into column 2, and adapt the bubble sort so the highest frequency finishes in row 1.

03 · Complete

Calculate chance = frequency / 100000 rounded to four decimal places, and output each integer with its chance, in stored order, with suitable messages and full declarations.

!

How Cambridge awards the marks: the mark scheme gives AO2 a maximum of 9 marks and AO3 a maximum of 6 marks. It lists requirements R1–R5, but does not assign a separate fixed mark total to each R requirement. The stage-to-mark links in this guide show alignment, not invented stage marks.

Source question

Read the whole question first

Do not begin coding after the first paragraph. The direction of the sort, the exact divisor 100 000 and the demand for declarations all affect the structure of your solution.

Question 8 · 15 marks
Cambridge IGCSE Computer Science · 0478/22 · Question 8

A program is required to test the fairness of the random number generator.

The one-dimensional (1D) array RandomNumber[] is used to store 100 000 random integers. The two-dimensional (2D) array CountedNumber[] 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:

F ← TRUE
WHILE F DO
    F ← FALSE
    FOR I ← 1 TO 9
        IF A[I, 2] > A[I + 1, 2]
           THEN
              T1 ← A[I, 1]
              T2 ← A[I, 2]
              A[I, 1] ← A[I + 1, 1]
              A[I, 2] ← A[I + 1, 2]
              A[I + 1, 1] ← T1
              A[I + 1, 2] ← T2
              F ← TRUE
        ENDIF
    NEXT I
ENDWHILE

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.

Before you move on

These are the five required techniques identified by the Cambridge mark scheme.

Mark-scheme map
R1

Generate 100 000 random integers between 1 and 10, inclusive, and store each one in RandomNumber[].

R2

Calculate the frequency of each integer 1 to 10 and store it in column 2 of CountedNumber[], beside the integer in column 1.

R3

Sort CountedNumber[] into descending order of frequency by adapting the given ascending snippet — flip the comparison.

R4

Calculate chance = frequency / 100000 for each integer, rounded to four decimal places.

R5

Output each integer with its chance, with suitable messages, in the order the rows are stored after the sort.

Use the tabs along the top to work through the seven stages. The complete solution is kept in its own tab, and the interactive visualisation is the final tab.