Cambridge AS Level Computer Science 9618 · 11.1 Programming Basics

Q37 · 9618/23/O/N/25 Q6

Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 1 of 3.

Q379618/23/O/N/25original Q6Structured · 9 marks
Question 6 from 9618/23/O/N/25

Worked solution

(a)

Write a list of test numbers chosen so that every possible check digit (0 to 9) is produced. [1]

Run Generate() on each and check each result matches the expected value. [1]

(b)

Turn the number into a string. The last digit is the check digit. Add up all the other digits and compare their sum MOD 10 with the check digit.

FUNCTION CheckNumber(Number : INTEGER) RETURNS BOOLEAN
    DECLARE Total, Index, CheckDigit : INTEGER
    DECLARE NumString : STRING
    Total ← 0
    NumString ← NUM_TO_STR(Number)
    CheckDigit ← STR_TO_NUM(RIGHT(NumString, 1))
    FOR Index ← 1 TO LENGTH(NumString) - 1
        Total ← Total + STR_TO_NUM(MID(NumString, Index, 1))
    NEXT Index
    IF Total MOD 10 <> CheckDigit THEN
        RETURN FALSE
    ENDIF
    RETURN TRUE
ENDFUNCTION

Function heading with name, parameter and return type [1]

NUM_TO_STR used to convert the number [1]

Check digit taken from the last digit [1]

Loop for the length of the string minus 1 [1]

Digits added correctly in the loop, with Total set to 0 before it [1]

Check digit calculated from the sum (Total MOD 10) [1]

Compared with the last digit and the correct BOOLEAN returned [1]

Another way: no string, take the last digit with MOD 10, then use a WHILE loop with MOD and DIV on the rest.

KEY IDEASplit off the last digit as the check digit, add up the other digits, and compare the sum MOD 10 with it.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/23/O/N/25 question 6
VIDEO
▶ Topic lesson

A lesson on this topic · by ZAINEMATICS. 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.