IBDP Computer Science B2.4 Programming algorithms HL Paper 1 - New Syllabus
Question
A team of four high-school students decided to create a computer program for younger students to help them learn to count and recognize whole numbers from 1 to 25.
A table partially completed with numbers from 1 to 25 is given to a younger student. The table consists of five rows and five columns (see Figure 2). The younger student must enter the remaining numbers. Each number from 1 to 25 should be entered only once.
| 24 | 1 | |||
| 7 | 14 | |||
| 4 | 6 | 22 | ||
| 10 | 21 | 3 | ||
| 11 | 25 | 9 |
Each of the four students is responsible for creating several sub-programs that will be included in the program.
The sub-program inputInRange() should return a valid input number.
This will ensure that the younger student’s input is a number in the range 1–25 inclusive. If the input number is not in this range, an appropriate message is output. The process will be repeated until a valid number is input.
For example:
For the sub-program call N = inputInRange(), if the following appears on the screen:
Then the value of N would be 15 after execution.
inputInRange(). \( [4] \)In the program, the table is stored as a static two-dimensional array.
Figure 3a
The two-dimensional array is correctly completed.
| [0] | [1] | [2] | [3] | [4] | |
|---|---|---|---|---|---|
| [0] | 2 | 24 | 1 | 8 | 15 |
| [1] | 23 | 5 | 7 | 14 | 16 |
| [2] | 4 | 6 | 13 | 20 | 22 |
| [3] | 10 | 12 | 19 | 21 | 3 |
| [4] | 11 | 18 | 25 | 17 | 9 |
Figure 3b
The two-dimensional array is not correctly completed.
| [0] | [1] | [2] | [3] | [4] | |
|---|---|---|---|---|---|
| [0] | 12 | 24 | 1 | 8 | 15 |
| [1] | 23 | 5 | 7 | 14 | 16 |
| [2] | 4 | 6 | 13 | 20 | 22 |
| [3] | 10 | 12 | 19 | 21 | 3 |
| [4] | 11 | 18 | 25 | 2 | 9 |
The sub-program isCorrectlyCompleted(MAT) accepts the two-dimensional array MAT and outputs an appropriate message to indicate if it has been correctly filled.
For Figure 3a, the table has been correctly completed. For Figure 3b, the table has not been correctly completed.
The description of the algorithm for the isCorrectlyCompleted(MAT) sub-program is as follows:
- Initialize a one-dimensional array,
FLAGS, with zero \(0\) values. - Visit all elements of the
MATarray.- Use the number stored in the
MATarray to match up the index in theFLAGSarray. - Assign one \(1\) to the
FLAGSarray at that index.
- Use the number stored in the
- Inspect elements of the
FLAGSarray.- If all elements in the
FLAGSarray are one \(1\), output a message saying that the table has been completed correctly; otherwise, output a message saying that the table has not been completed correctly.
- If all elements in the
For example, the FLAGS array for the MAT array shown in Figure 3a is:

And the FLAGS array for the MAT array shown in Figure 3b is:

isCorrectlyCompleted(MAT) sub-program as described. \( [8] \)Most-appropriate topic code (CED):
▶️ Answer/Explanation
(a) Three advantages of using sub-programs are:
• Modularity: The program can be divided into smaller, easier-to-understand parts, making the overall program easier to organize.
• Independent development: Each sub-program can be developed, tested and debugged independently, allowing the four students to work on different parts at the same time.
• Code reuse: A sub-program can be called multiple times, reducing duplication and making the program easier to maintain.
Other valid advantages include easier debugging, clearer program structure, reduced code size and easier maintenance when a sub-program needs to be changed.
(b) The input must be repeatedly requested until a value between 1 and 25 inclusive is entered. One valid pseudocode solution is:
inputInRange()
K = input('Enter a number: ')
loop while (K < 1) or (K > 25)
output('The number is not in the range 1 to 25.')
output('Please try again.')
K = input('Enter a number: ')
end loop
output('Your number is valid.')
return K
end inputInRange()The loop continues while the input is outside the valid range. Once \(1 \leq K \leq 25\), the value is returned.
(c) The FLAGS array has 25 elements indexed from 0 to 24. Each value in MAT is used to determine an index in FLAGS. Since the numbers are from 1 to 25, the index is calculated as MAT[R][C] - 1.
isCorrectlyCompleted(MAT)
// FLAGS contains 25 values indexed from 0 to 24
// initialize FLAGS
loop K from 0 to 24
FLAGS[K] = 0
end loop
// match the values in MAT with indexes in FLAGS
loop R from 0 to 4
loop C from 0 to 4
INDEX = MAT[R][C] - 1
FLAGS[INDEX] = 1
end loop
end loop
// inspect all elements in FLAGS
F = true
loop K from 0 to 24
if FLAGS[K] = 0
then F = false
end if
end loop
if F = true
then
output('The table has been completed correctly')
else
output('The table has not been completed correctly')
end if
end isCorrectlyCompleted()The algorithm works because every number from 1 to 25 must occur exactly once. If a number is missing, its corresponding element in FLAGS remains \(0\), so the table is identified as incomplete.
Alternative valid approach: Count the number of \(1\) values in FLAGS. If the count is \(25\), output that the table has been completed correctly; otherwise, output that it has not been completed correctly.
