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.

Figure 2: Example table partially filled with numbers
 241  
  714 
46  22
10  213
11 25 9

Each of the four students is responsible for creating several sub-programs that will be included in the program.

(a) List three advantages of using sub-programs in this situation. \( [3] \)

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:

Enter a number:
32
The number is not in the range from 1 to 25. Please try again.
Enter a number:
0
The number is not in the range from 1 to 25. Please try again.
Enter a number:
15
Your number is valid.

Then the value of N would be 15 after execution.

(b) Construct an algorithm in pseudocode for the sub-program inputInRange(). \( [4] \)

In the program, the table is stored as a static two-dimensional array.

Figure 3: Two examples of data

Figure 3a
The two-dimensional array is correctly completed.

 [0][1][2][3][4]
[0]2241815
[1]23571416
[2]46132022
[3]101219213
[4]111825179

Figure 3b
The two-dimensional array is not correctly completed.

 [0][1][2][3][4]
[0]12241815
[1]23571416
[2]46132022
[3]101219213
[4]11182529

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 MAT array.
    • Use the number stored in the MAT array to match up the index in the FLAGS array.
    • Assign one \(1\) to the FLAGS array at that index.
  • Inspect elements of the FLAGS array.
    • If all elements in the FLAGS array 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.

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:

(c) Construct the algorithm in pseudocode for the isCorrectlyCompleted(MAT) sub-program as described. \( [8] \)

Most-appropriate topic code (CED):

(a) B2.3: Programming constructs
(b) B2.3: Programming constructs
(c) B2.2: Data structures | B2.4: Programming algorithms
▶️ Answer/Explanation
Detailed solution

(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.

Scroll to Top