9618/21/O/N/24 question 6: worked solution (October/November 2024, paper 21)

Show as plain text

Cambridge AS Level Computer Science 9618 · 10.2 Arrays Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4. Q30 9618/21/O/N/24 original Q6 Structured · 9 marks 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 IDEA Use 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 VIDEO ▶ Whole paper Video solution of the whole paper · by Pseudocode Pro. Tap the picture to open it on YouTube.