Computer ScienceExam code: 0984

Programming Concepts

Programming

CIE pseudocode supports the standard arithmetic operators, plus two functions specific to integer division.

OperationPseudocodePython
Addition++
Subtraction--
Multiplication**
Division//
Exponentiation (raised to power)^**
Integer division (quotient)DIV(a, b)//
Modulus (remainder)MOD(a, b)%

CIE pseudocode uses these operators: +, -, *, / and the exponentiation operator ^, plus the two integer-division operations DIV and MOD, which are written as function calls: DIV(a, b) and MOD(a, b). The ^ operator raises a number to a power (for example 2 ^ 3 = 8, and X ^ 3 for "X cubed").

Common exam question

Recognising counting, totalling and average statements

Question: Link each pseudocode statement to its use (counting, totalling, finding an average, a conditional statement or calling a procedure), or give the line numbers of the statements in an algorithm that do the totalling and form the loops (3–4 marks).

Asked in 2 of the 17 papers, one mark per line or per answer. Counting adds one to the same variable each time: Count ← Count + 1. Totalling adds a value other than 1 to a running total: Total ← Total + Value. Finding an average divides a sum by the number of values: Mean ← (A + B + C) / 3. An IF line is the conditional statement and a CALL line uses a procedure. In the matching version one use is left over, so do not force every label onto a statement. In the line-number version, FOR … NEXT is the count-controlled loop and REPEAT … UNTIL the post-condition loop.

DIV: integer division

DIV(a, b) returns the whole-number part of a divided by b, throwing away any remainder.

DIV(17, 5) returns 3      // 17 ÷ 5 = 3 remainder 2
DIV(20, 4) returns 5      // 20 ÷ 4 = 5 exactly
DIV(9, 2) returns 4       // 9 ÷ 2 = 4 remainder 1

Useful for splitting things into equal groups: "how many full minutes are in 130 seconds?" → DIV(130, 60) returns 2.

MOD: remainder

MOD(a, b) returns the remainder after a is divided by b.

MOD(17, 5) returns 2      // 17 ÷ 5 = 3 remainder 2
MOD(20, 4) returns 0      // 20 ÷ 4 = 5 remainder 0
MOD(9, 2) returns 1       // 9 ÷ 2 = 4 remainder 1

Useful for a classic check: "is a number divisible by 2?" → MOD(Number, 2) = 0 is true for even numbers.

Worked example

Using DIV and MOD together to split a total

A checkout system stores a total price in whole pence. Convert 503 pence into pounds and pence.

Solution:

  • Whole pounds = DIV(503, 100) = 5 (integer quotient: how many complete hundreds fit)
  • Remaining pence = MOD(503, 100) = 3 (the remainder after removing those complete hundreds)
  • Result: £5.03

The same pattern applies any time a total must be split into a larger unit and a leftover: DIV(TotalItems, GroupSize) gives the number of complete groups; MOD(TotalItems, GroupSize) gives what is left over.

ROUND: rounding to a number of decimal places

ROUND(value, places) returns value rounded to places decimal places.

ROUND(3.14159, 2) returns 3.14   // two decimal places
ROUND(8.6, 0) returns 9          // zero decimal places: nearest whole number

value should be a real number and places a whole number. ROUND is useful for tidying up money or measurements before they are output.

RANDOM(): a random number

RANDOM() returns a random real number between 0 and 1 inclusive. It takes no arguments.

To get a random whole number in a range, scale the result and round it. For example, a random whole number from 0 to 6 is ROUND(RANDOM() * 6, 0). RANDOM() is the standard way to add chance to a program, such as rolling a dice or shuffling.

Quick check

4 short questions on this note. See what has stuck, and where to look if something hasn't.

About 2 minutes

Build on this topic