2.1.2 Pseudo Code
2.1.2 Pseudo Code
2 Pseudo code
Introduction to Algorithm
An algorithm is a sequence of steps for solving a problem.
In general, an 'algorithm' is the name given to a defined set of steps used to
complete a
task.
For instance you could define an algorithm to make a cup of tea. You start by
filling the
kettle, and then place a tea bag in the cup and so on.
In computer terms, an algorithm describes the set of steps needed to carry out a
software task. This mini-web takes you through the topic of algorithm
The concept of a program
A program is a sequence of instructions or programming language statements
written to
make a computer perform certain tasks.
Well-structured programs require a programming language to support the
following
program constructs:
➢ sequence
➢ selection
➢ iteration
A computer’s processor can only run a computer program in the form of a file of machine
code, which is a sequence of binary codes representing instructions for the
processor.
The instruction set for a family of processors is the machine language in which
machine
code is written for that family of processors.
When machine code runs, the processor repeatedly:
➢ Fetches an instruction from internal memory
➢ Decodes the instruction
➢ Executes the instruction.
Constant:
Constant is memory location where a value can be stored but the stored value
remaining
same during execution.
It is good practice to use constants if this makes the pseudo code more readable,
as an
identifier is more meaningful in many cases than a literal. It also makes the
pseudo code
easier to update if the value of the constant changes.
Constant declaration
Constants are normally declared at the beginning of a piece of pseudo code
(unless it is
desirable to restrict the scope of the constant).
Constants are declared by stating the identifier and the literal value in the
following
format:
Only literals can be used as the value of a constant. A variable, another constant
or an
expression must never be used.
ARRAYS
Declaring arrays
Arrays are considered to be fixed-length structures of elements of identical data
type,
accessible by consecutive index (subscript) numbers. It is good practice to
explicitly state
what the lower bound of the array (i.e. the index of the first element) is because
this
defaults to either 0 or 1 in different systems. Generally, a lower bound of 1 will be
used.
Square brackets are used to indicate the array indices.
One- and two-dimensional arrays are declared as follows (where l, l1, l2 are
lower
bounds and u, u1, u2 are upper bounds):
Locating errors in questions