Library Routines: ROUND and RANDOM

A library routine performs a predefined operation. Use its documented parameters and returned value instead of rebuilding it.

Rounding a value or expression

ROUND(value, places) returns a value rounded to the requested number of decimal places. The first argument can be an identifier or expression; places is an integer. For example, ROUND(14.0 / 3, 2) gives 4.67. ROUND(4.67, 0) gives 5. Rounding is different from discarding fractional digits.

Random values

RANDOM() takes no arguments. Cambridge specifies a real result in the range 0 to 1 inclusive. Do not predict a particular next value. If R is in that range, R * 10 lies between 0 and 10, and ROUND(R * 10, 0) is a whole number between 0 and 10. Rounded endpoints are not equally likely: do not describe this as a fair die.

DECLARE Reading : REAL
Reading <- RANDOM()
OUTPUT ROUND(Reading * 10, 0)

Worked example: cost per item

A batch costs 17.0 and contains 6 items. Divide first: 17.0 / 6. Round the result to two decimal places: 2.83. Do not round the total before dividing if that loses necessary precision.

Independent application

Predict the minimum and maximum possible result of ROUND(RANDOM() * 4 + 2, 0). Explain why running the program once cannot establish that the range is correct.

AI Socratic Tutor

All-Access
Sign in or create an account. AI Tutor is an All-Access benefit.
Sign Up

Need guidance? Ask a question or use the hint below.

AI Socratic Tutor is an All-Access Benefit

Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.

You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.

Independent Practice: Unit cost

+30 XP

Read a real batch cost followed by a positive integer item count. Output the cost per item rounded to two decimal places. A whole-number result may be printed without trailing zeroes.

Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.

solution.pseudo
All-Access Required

Interactive exercises for Module 2 and beyond require an active plan.

○ Recurring decimal
○ Already exact
○ Round upwards