Question 1 Report
Study the following pseudocode that converts a number of seconds into hours, minutes and seconds.
DECLARE TotalSeconds : INTEGER
DECLARE Hours : INTEGER
DECLARE Minutes : INTEGER
DECLARE Seconds : INTEGER
INPUT TotalSeconds
Hours ← TotalSeconds DIV 3600
TotalSeconds ← TotalSeconds MOD 3600
Minutes ← TotalSeconds DIV 60
Seconds ← TotalSeconds MOD 60
OUTPUT Hours, ":", Minutes, ":", Seconds(a) Complete the trace table for the input TotalSeconds = 7384. [4]
| Step | TotalSeconds | Hours | Minutes | Seconds |
|---|---|---|---|---|
| Input | ||||
| Hours calc | ||||
| Remaining secs | ||||
| Minutes calc | ||||
| Seconds calc |
(b) State the output. [1]
(c) Explain the difference between the DIV and MOD operators. [1]
(a) The algorithm converts a total number of seconds into hours, minutes, and remaining seconds using DIV (integer division) and MOD (remainder):
| Step | TotalSeconds | Hours | Minutes | Seconds |
|---|---|---|---|---|
| Input | 7384 | - | - | - |
| Hours calc | 7384 | 7384 DIV 3600 = 2 | - | - |
| Remaining secs | 7384 MOD 3600 = 184 | 2 | - | - |
| Minutes calc | 184 | 2 | 184 DIV 60 = 3 | - |
| Seconds calc | 184 | 2 | 3 | 184 MOD 60 = 4 |
The key insight is that DIV extracts the whole units (hours or minutes) and MOD gives the remainder that still needs to be broken down further. [4]
(b) The output is: 2:3:4 (2 hours, 3 minutes, 4 seconds). [1]
(c) DIV performs integer division, returning only the whole number part and discarding the remainder. For example, 7384 DIV 3600 = 2 (not 2.051...). MOD returns only the remainder after integer division. For example, 7384 MOD 3600 = 184. Together, they allow decomposition of a large value into its component parts. [1]
Everything you need to excel in your exams