# Pseudocode

## Introduction

Pseudocode is a tool for representing algorithms without the use of a particular programming language and related syntax.

It is written in a combination of plain English and common symbols, and describes, in a detailed step-by-step manner, the processes used in the algorithm.

There are some basic principles to be followed in using pseudocode which include:

• Have only one statement per line.
• Use indentation to show the hierarchy of processes within an algorithm such as repeating sections and conditional decisions.
• End nested processes with an end keyword (end if, end while).

### The constructs of pseudocode

#### Entering values into an algorithm

Values can be entered into an algorithm as part of the algorithm name or as input within the algorithm.

#### Assigning values to variables

A variable is a string of one or more letters that acts as a placeholder that can be assigned different values. For example:

x ← 5 means ‘assign the value 5 to the variable x’.

product ← 3 means ‘assign the value 3 to the variable product’.

xx +1 means ‘assign the value x+ 1 to the variable x’.

#### Decisions in pseudocode

if – then blocks provide a means of making decisions within an algorithm. Certain instructions are only followed if a condition is true.

if condition is true  then

end if

We can expand this process by specifying alternative instructions if the condition is false.

if condition is true  then

else

end if

#### An example for finding the smaller of two numbers a and b.

Algorithm: minimum of two numbers

input a, b

if a ≤ b then
print a
else
print b

end if

Further decisions can be added inside if – then blocks to include more conditions are nested (indented) within the previous block.

if first condition is true then
else if second condition is true then
else
end if

#### Repetition

Loops are used to repeat a process. How many repetitions (iterations) take place is controlled by either specifying and counting the number of times (a for loop) or by specifying a condition which must be met for the process to continue, otherwise it ends (a while loop). An example of each of these approaches is shown below:

For loops provide a means of repeatedly executing the same set of instructions in a controlled way. A counter (i) is increased by one, each time through the loop.

for i from 1 to n
end for

Note: We will follow the convention that `from 1 to n’ is inclusive. For example:
for i from 1 to 3, i takes values 1, 2 and 3,

Algorithm: First 5 perfect squares

for i from 1 to 5
print i2
end for

While loops provide another means of repeatedly executing the same set of instructions in a controlled way. This is achieved by performing iterations as long as some condition remains true.

while condition is true
end while

Algorithm: Perfect squares less than 1000

x ← 1

while x2 < 1000
xx + 1
print (x – 1)2
end while

### Functions

Are sections of pseudocode that can be used to complete a specific task. Once a function is defined, it can be used (called) within another algorithm. A function takes one or more input values and returns an output value.

define function_name(input for function):
return output

define factorial(n):

product ← 1
for i from 1 to n
productproduct × i
end for
return product

### Examples

#### Numerical integration using trapezium method

##### Algorithm
definef(x)
return (enter required function rule)
sum ← 0
a ← lowest x-value
b ← highest x-value
nnumber of trapeziums
h ← (b-a)/n
lefta
righta + h
for i from 1 to n
strip ← 0.5(f(left) + f(right)) x h
sumsum + strip
leftleft + h
rightright + h
end for
print sum

The trapezium method algorithm gives an approximation of
The function being considered is first defined.
For example, f(x) = x2, choose a = 1 and b = 4 to approximate
n is the number of trapeziums chosen.
In the for loop the area of each trapezium is calculated as you move from left to right and added to sum.
The final value of sum is printed.

The trapezium algorithm may be implemented by using a function structure.
The function f must be defined first.

define  Trap(a, b, n)
(Insert algorithm as shown)
return (sum)

The advantage of this is that it may be implemented simply by changing the values of the variables.

#### Estimate of the long-term average for the number of rolls to get a six

##### Algorithm

sum ← 0
for i from 1 to 1000

outcome ← 0
count ← 0
while outcome ≠ 6
outcomerandominteger(1, 6)
countcount + 1
end while
sumsum + count
end for
print sum/1000

Many devices have a random number function. We use the function:
randominteger(1, 6) to return one of the numbers 1,2,3,4,5,6 randomly.

The while loop continues until the value 6 is given by randominteger(1, 6). The variable count  gives the number of iterations before a 6.

The for loop repeats this 1000 times. The variable sum gives the total of the 1000 values of count.

The print statement is for the average

#### Bisection Method for finding x-intercepts

##### Algorithm
define f(x)
return(enter required function rule)
a ← lower guess
b ← upper guess
c ← (a + b)/2
t ← tolerance
if f(a) × f(b) > 0 then
print "Incorrect initial guesses"

else

while b - a > 2×t

if f(af(c) < 0 then
bc
else
ac
end if
c ← (a+b)/2
end while
print(c)
end if

Function f(x) is chosen.
For example:
define f(x)

return x2 – 2.

Good choices for a and b are a = 1
and a = 2. (f(1) < 0 and f(2) > 0.)

If this is the choice we go to the else section of the algorithm and the outer if statement has done its job.

The while loops requires that we continue the iteration until the desired accuracy is reached.

The if statement inside the algorithm ensures that we continue to choose values for which there is a sign difference. For each iteration an average of the new pair is determined by the if statement is calculated.

#### Euler’s method for differential equations

Consider a differential equation

Define a function euler(x0, y0, h, xfin) where h is the step size and xfin is the value at which we want to approximately solve the differential equation.

##### Algorithm
define function( x, y )
return(enter required function rule)

define euler(x0, y0, h, xfin)

yy0
x x0

while x < xfin

yy + h x f(x, y)
xx + h
end while
return (xfin, y)

The differential equation is
For example f(x,y) = xy.

x0 is the initial x value and y0 the correspomding y value. Let h = 0.1

If you start at (1,2) the next ordered pair is (1.1, 2.2) and the following (1.2, 2.442)

The iteration continue for the required number of iterations.

## Content from the areas of study where pseudocode can be used

### Mathematical Methods

• piecewise defined functions
• bisection (and using the mean of the endpoints as a point estimate)
• numerical evaluation of limits for derivatives
• Newton’s method for polynomials (Units 1 and 2) and other functions (Units 3 and 4)
• Polynomial approximations to transcendental functions
• Counting in a probability context
• Simple simulations for probability
• Optimisation of integer valued functions
• Simulations for sampling distributions, central limit theorem
• Numerical integration, left and right interval values, trapezium method point estimate

### Specialist Mathematics

• Investigation of number properties, sequences, partial sums and products
• Numerical integration – Reimann sums for areas, volumes and surface areas of solids of revolution, length of segments of a curve
• Vector operations including scalar product, cross product, projections
• Numerical solution of differential equations including Euler’s method
• Sums of random variables
• Sample distributions for means

### Sample Questions

1The algorithm shown on the right will print the value

A16

B24

C28

D30

E32

a ← 2
while a < 20
a ← 2a
end while
print(a)

2The algorithm shown on the right will print the value

A15

B17

C19

D21

E23

sum ← 2
for x from 1 to 3

for y from 1 to 2
sum = sum + x + y
end for
end for
print(sum)

3

For the algorithm shown opposite, the value of c when count = 3, is

A2.125

B2.15625

C2.1875

D2.25

E2.5

define f(x)
return (2ex – 17)
a ← 2
b ← 3
c ← (a+b)/2
count = 0

while b - a > 0.002

count = count + 1

if f(af(c) < 0 then
bc
else
a ← c
end if
c ← (a+b)/2
end while

4

Three dice are rolled at the same time. The algorithm shown opposite generates the sample space for this experiment.
The print statement when count = 4 gives (4, x). The value of x is

A4

B5

C6

D7

E8

count ← 0
for i from 1 to 6

for j from 1 to 6

for k from 1 to 6

countcount + 1
print(count, i + j + k)
end for
end for
end for

5a For the code opposite give the resulting printout.

for i from 1 to 4
print i3 + 2
end for

b For the code opposite give the resulting printout.

for i from 1 to 5

if i2 < 10
print i2
else
printi2 + 2
end if
end for

6

The surface area, S, of a rectangular prism box with the top open and fixed volume V ,
is given by where the side lengths take only integer values. Assume x and y are the side lengths of the base of the box.

For a given value of V, describe the algorithm using pseudocode to find the integer valued side lengths that maximise the surface area of the box.