Programming languages come with routines that are already written and tested, so you do not have to write them yourself. The 0478 syllabus names four: MOD, DIV, ROUND and RANDOM. Learn exactly what each one returns and the classic jobs they do.
A library routine is a pre-written, pre-tested subroutine that comes with a programming language. DIV(a, b) returns the whole-number quotient of a divided by b, with the fractional part discarded. MOD(a, b) returns the remainder. ROUND(x, places) returns x rounded to that number of decimal places. RANDOM() returns a random number between 0 and 1 inclusive.
In the lab, choose MOD, DIV and ROUND. It turns a number of minutes into hours and minutes; try 135, then other values.
DECLARE Minutes : INTEGER INPUT Minutes OUTPUT DIV(Minutes, 60), " hours and ", MOD(Minutes, 60), " minutes"
With the input 135: 135 = 2 × 60 + 15, so the output is 2 hours and 15 minutes.
MOD(Number, 2) = 0 is TRUE for even numbers.MOD(Year, 4) = 0 is TRUE when Year divides exactly by 4.MOD(Number, 10) is the last digit; DIV(Number, 10) removes it.The same idea in Python (for comparison only: the exam uses pseudocode)
minutes = int(input()) print(minutes // 60, "hours and", minutes % 60, "minutes") # // is DIV, % is MOD
DIV(472, 100), MOD(472, 10) and MOD(DIV(472, 10), 10).MOD gives the remainder, not the decimal part: MOD(7, 2) is 1, not 0.5. DIV(7, 2) is 3, not 3.5.
ROUND takes a value (or an expression) and the number of decimal places to round it to.
DECLARE Price : REAL DECLARE Total : REAL Price ← 2.499 Total ← ROUND(Price * 3, 2) OUTPUT Total
2.499 × 3 = 7.497, which rounds to 7.50 at 2 decimal places, so the output is 7.5.
The same idea in Python (for comparison only: the exam uses pseudocode)
price = 2.499 total = round(price * 3, 2) print(total) # 7.5
ROUND(3.14159, 3) and ROUND(18.5 / 4, 1).ROUND returns a new value; it does not change the variable unless you assign the result back, e.g. Price ← ROUND(Price, 2).
Four quick checks. Each correct answer earns XP and records this skill on your profile.
What does MOD(17, 5) return?
What does DIV(17, 5) return?
In the lab's MOD, DIV and ROUND program, the input is 135. Which line is output first?
Which expression gives a random whole number from 1 to 6?
RANDOM() returns a real number from 0 to 1. Multiply it to stretch the range, round it to get a whole number, and add to shift the range up. The lab's RANDOM program rolls a dice five times.
DECLARE Roll : INTEGER
DECLARE Count : INTEGER
FOR Count ← 1 TO 5
Roll ← ROUND(RANDOM() * 5, 0) + 1 // a whole number from 1 to 6
OUTPUT "Roll ", Count, ": ", Roll
NEXT Count
| Expression | Smallest | Largest |
|---|---|---|
| RANDOM() | 0 | 1 |
| RANDOM() * 5 | 0 | 5 |
| ROUND(RANDOM() * 5, 0) | 0 | 5 (whole numbers) |
| ROUND(RANDOM() * 5, 0) + 1 | 1 | 6 |
The same idea in Python (for comparison only: the exam uses pseudocode)
import random roll = random.randint(1, 6) # Python's library routine for a whole number 1 to 6
ROUND(RANDOM() * 6, 0) gives 0 to 6, which is seven possible values, not a dice roll. Check the smallest and largest values of your expression.
Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
Which condition is TRUE when Number is even?
What is the value of DIV(100, 7) + MOD(100, 7)?
What does ROUND(2.0 / 3.0, 2) return?
What is a library routine?
This skill is now recorded on your profile. Keep the chain going.