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

follow these instructions

end if

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

if condition is true  then

follow these instructions

else

follow these instructions

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 thenprintelse 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 thenfollow these instructions
else if second condition is true thenfollow these instructions
elsefollow these instructionsend 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 fromto n
follow these instructionsend 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 fromto
print i2end 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 trueend while

Algorithm: Perfect squares less than 1000

x ← 1

while x2 xx
printx2end 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_namefollow these instructionsreturn

 

define factorial(n):

product
for i fromto nproductproduct i end forreturn product

Examples

Numerical integration using trapezium method

Algorithm
definefxreturnenter required function rulesum
ax
bx
nnumber of trapeziums
hban
lefta
rightah
for i fromto n
stripffh
sumsumstrip
leftlefth
rightrighth
end for
print sum
Comments

The trapezium method algorithm gives an approximation of complex equation
The function being considered is first defined.
For example, f(x) = x2, choose a = 1 and b = 4 to approximate complex equation
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
coun
while outcome
outcomerandominteger
countcount
end while
sumsumcountend for
print sum
Comments

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 fx
returnenter required function rulea
b
cab
t
if fafbthen
print

else

while b - a > 2×t

if fafcthen
bcelse
acend if
cab
end while
print(c)
end if
Comments

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) 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 complex 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
returnenter required function rule

define euler(x0, y0, h, xfin)

yy0
x x0

while x xfin

yyhfxy
xxhend whilereturnxfiny
Comments

The differential equation is complex equation
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

Answer: E

a
while aaaend while
printa

 

2The algorithm shown on the right will print the value

 

A15

B17

C19

D21

E23

Answer: E

sum ← 2
for x from 1 to 3

for y fromtosumsumxyend forend for
print(sum)

 

3

ccount

 

A2.125

B2.15625

C2.1875

D2.25

E2.5

Answer: C

define fx
returnexa
b
cab
count

while b - a > 0.002

count = count + 1

if fafcthen
bcelse
end if
cabend while

 

4

countxx

 

A4

B5

C6

D7

E8

Answer: C

count ← 0
for i from 1 to 6

for j from 1 to 6

for k from 1 to 6

countcount
printcountijk end for
end forend for

 

5a For the code opposite give the resulting printout.

 

for i fromtoprint i3end for

 b For the code opposite give the resulting printout.

 

for i from 1 to 5

if i2print i2else
printi2end ifend for

 

6

The surface area, S, of a rectangular prism box with the top open and fixed volume V ,
is given by complex equation 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.