Cambridge AS Level Computer Science 9618 · 10.2 Arrays

Q30 · 9618/21/O/N/24 Q6

Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.

Q309618/21/O/N/24original Q6Structured · 9 marks
Question 6 from 9618/21/O/N/24

Worked solution

(a)

Write any two of these:

Fewer lines of code are needed [1]

The program is simpler, so it is easier to design, code, maintain, modify, test or debug [1]

Other points that also score: the days can be accessed directly using the month number as the index.

(b)

Get the day, month and year. Add the shelf life to the day. If the day is past the end of the month, move to the next month and take away the days in the month. If the month goes past 12, move to January of the next year.

FUNCTION GetDate(ProductionDate : DATE, ShelfLife : INTEGER) RETURNS DATE
    DECLARE NewDate : DATE
    DECLARE DD, MM, YY, LastDay : INTEGER
    DD ← DAY(ProductionDate)
    MM ← MONTH(ProductionDate)
    YY ← YEAR(ProductionDate)
    DD ← DD + ShelfLife
    LastDay ← DaysInMonth[MM]
    IF DD > LastDay THEN
        MM ← MM + 1
        DD ← DD - LastDay
        IF MM = 13 THEN
            MM ← 1
            YY ← YY + 1
        ENDIF
    ENDIF
    NewDate ← SETDATE(DD, MM, YY)
    RETURN NewDate
ENDFUNCTION

Three INTEGER variables declared for DD, MM and YY [1]

Date functions DAY, MONTH and YEAR used to get the three values [1]

ShelfLife added to DD [1]

LastDay taken from DaysInMonth using MM as the index [1]

Tests DD > LastDay, then adds 1 to MM and sets DD to DD minus the days in the month [1]

If MM is 13, sets MM to 1 and adds 1 to YY [1]

Uses SETDATE to make a DATE (declared as type DATE) and returns it [1]

KEY IDEAUse an array indexed by month number to avoid a long chain of IF statements, and handle the roll over to the next month and year.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/21/O/N/24 question 6
VIDEO
▶ Whole paper

Video solution of the whole paper · by Pseudocode Pro. 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.