A factory produces food items. The items must be used within a certain number of days after their production date. The number of days is known as the shelf life. It is different for each type of item but is always a whole number in the range 1 to 21 (inclusive). The latest date that an item can be used is called the 'use-by' date. A program is needed to produce labels which show the 'use-by' date. Part of the program is a function GetDate() which will: • take two parameters: a production date and a value representing the shelf life • return the corresponding ‘use-by' date. The program contains a global 1D array DaysInMonth of type integer which stores the number of days in each month (index 1 is January): Index | Value ---|--- 1 | 31 2 | 28 3 | 31 4 | 30 11 | 30 12 | 31 Note: Leap years are not considered
- (a)An algorithm uses the array DaysInMonth to calculate a 'use-by' date. An alternative design would involve the use of multiple selection statements. An array-based technique is often used when there is a large number of different values to check and where no pattern exists. One advantage of using an array-based technique is the speed of execution compared to the use of multiple selection statements. Give two other advantages of using an array for this type of operation rather than a solution based on multiple selection statements. 1 2[2]
- (b)Complete the pseudocode for the function GetDate(). Date functions from the insert should be used in your solution. FUNCTION GetDate(ProductionDate : DATE, ShelfLife : INTEGER) RETURNS DATE ENDFUNCTION[7]
Mark scheme
Marking points from the official mark scheme (Oct/Nov 2024). The mark or mark type is shown in brackets.
- [1] (a) Fewer lines of code are needed
- [1] (a) The program is simpler/ less complex // Program is easier to design / code / maintain / modify / test / debug
- [1] (a) Direct access to days in a month / data (using month number as index) // Can use index / month to (directly) access days in month / data
- [1] (b) Declare three variables of TYPE INTEGER to store DD, MM and YY
- [1] (b) Correct use of date functions from Insert to extract three integer values representing DD, MM and YY and use/assign to a variable
- [1] (b) Add parameter ShelfLife to DD and use / assign result to a variable
- [1] (b) Extract LastDay from DaysInMonth using MM as Index
- [1] (b) Test for new DD after end of month / DaysInMonth[MM] ... and if DD > DaysInMonth[MM]: MM is incremented by 1 and DD is set to DD – DaysInMonth[MM]
- [1] (b) If MM = 13 / MM > 12 then increment YY and set MM to 1
- [1] (b) Use of SETDATE to assign value to NewDate, NewDate must be delared as a type DATE
- [1] (b) Return date
- 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
Write your answer and get it marked
On Oracle Prep you can write or photograph your own answer and have it marked by AI against this mark scheme, with feedback on what you missed.
Sign up free →