0% found this document useful (0 votes)
4 views

9.2 Algorithms

The document discusses writing algorithms using pseudocode. It covers writing pseudocode from structured English descriptions, using different pseudocode statements and operators, and providing examples of algorithms written in pseudocode and different programming languages.

Uploaded by

Ahmed Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

9.2 Algorithms

The document discusses writing algorithms using pseudocode. It covers writing pseudocode from structured English descriptions, using different pseudocode statements and operators, and providing examples of algorithms written in pseudocode and different programming languages.

Uploaded by

Ahmed Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

9.

2
Algorithms

Zain Merchant
Zain Merchant
In order to design a computer system that performs a speci c task, or solves a given
problem, the task or problem has to be rigorously de ned and set out, showing
what is going to be computed and how it is going to be computed.
Practice is essential to develop skills in computational thinking. Designs shown with
pseudocode or owcharts can be traced to check if the proposed solution works,
but the best way to actually test that a computer system works is to code it and use
it or, even better, get somebody else to use it. Therefore, practical programming
activities, alongside other activities, will be suggested at every stage to help
reinforce the skills being learnt and develop the skill of programming.

Zain Merchant
fl
fi
fi
Writing algorithms that provide
solutions to problems
There are several methods of writing algorithms before attempting to program a
solution. Here are three frequently used methods.
• Structured English is a method of showing the logical steps in an algorithm,
using an agreed subset of straightforward English words for commands and
mathematical operations to represent the solution. These steps can be
numbered.
• A owchart shows diagrammatically, using a set of symbols linked together with
ow lines, the steps required for a task and the order in which they are to be
performed. These steps, together with the order, are called an algorithm.
Flowcharts are an effective way to show the structure of an algorithm.
• Pseudocode is a method of showing the detailed logical steps in an algorithm,
using keywords, identi ers with meaningful names and mathematical operators
to represent a solution. Pseudocode does not need to follow the syntax of a
speci c programming language, but it should provide suf cient detail to allow a
program to be written in a high-level language.

Zain Merchant
fl
fl
fi
fi
fi
Zain Merchant
Writing simple algorithms using
pseudocode
Each line of pseudocode is usually a single step in an algorithm. The pseudocode
used my notes follows the rules in the Cambridge International AS & A Level
Computer Science Pseudocode Guide for Teachers and is set out using a xed
width font and indentation, where required, of four spaces, except for THEN, ELSE
and CASE clauses that are only indented by two spaces.
All identi er names used in pseudocode should be meaningful; for example, the
name of a person could be stored in the variable identi ed by Name. They should
also follow some basic rules: they should only contain the characters A–Z, a–z and
0–9, and should start with a letter. Pseudocode identi ers are usually considered to
be case insensitive, unlike identi ers used in a programming language.
It is good practice to keep track of any identi ers used in an identi er table, such
as

Pseudocode statements to use for writing algorithms. To input a value:

To output a message or a value or a combination:

To assign a value to a variable (the value can be the result of a process or a


calculation):

Zain Merchant
fi
fi
fi
fi
fi
fi
fi
Operators used in pseudocode assignment statements:

To perform a selection using IF statements for a single choice or a choice and an


alternative, and CASE statements when there are multiple choices or multiple
choices and an alternative:

Zain Merchant
Relational operators used in pseudocode selection statements:

Programming languages may not always have the same selection constructs as
pseudocode, so it is important to be able to write a program that performs the
same task as a solution given in pseudocode.
Here are three programs, one in each of the three prescribed programming
languages, to demonstrate the single choice IF statement. Note the construction of
the IF statement, as it is different from the pseudocode.
While the Cambridge International AS Level syllabus does not require you
to be able to write program code, the ability to do so will increase your
understanding, and will be particularly bene cial if you are studying the full
Cambridge International A Level course.

To perform iteration using FOR, REPEAT–UNTIL and WHILE loops:

A FOR loop has a xed number of repeats, the STEP increment is an optional
expression that must be a whole number.

Zain Merchant
fi
fi
Statements in a REPEAT loop are always executed at least once.

Statements in a WHILE loop may sometimes not be executed.


Programming languages may not always use the same iteration constructs as
pseudocode, so it is important to be able to write a program that performs the
same task as a solution given in pseudocode.
Here are three programs to demonstrate a simple FOR loop, one in each of the
three prescribed programming languages. Note the construction of the FOR
statement, as it is different from the pseudocode.

WHILE and REPEAT loops and IF statements make use of comparisons to decide
whether statements within a loop are repeated or a statement or group of
statements are executed. The comparisons make use of relational operators and
the logic operators AND, OR and NOT. The outcome of these comparisons is
always either true or false.

A simple algorithm can be clearly documented using these statements. A more


realistic algorithm to nd the average of a number of integers input would include
checks that all the values input are whole numbers and that the number input to
determine how many integers are input is also positive.

Zain Merchant
fi
This can be written in pseudocode by making use of the function INT(x) that
returns the integer part of x:

The identi er table for this algorithm is presented in table

Identifier name Description


Total Running total of integer values entered

Number Number of integer values to enter

Value Integer value input

Average Average of all the integer values entered

Here are three programs to nd the average of a number of integers input, one in
each of the three prescribed programming languages. Note the construction of
the loops, as they are different from the pseudocode. All the programming
languages check for an integer value.

Zain Merchant
fi
fi
Writing pseudocode from a
structured English description
There are no set rules for writing structured English – the wording just needs to be
unambiguous and easily understandable. Pseudocode is more precise and usually
follows an agreed set of rules.
From a structured English description, the following things need to be possible:
• Any variables that need to be used can be identi ed and put in an identi er table
– these can be items input or output as the results of calculations.
• Input and output can be identi ed from the wording used, for example, Enter,
Read, Print, Write.
• Selection can be identi ed from the wording used, for example, If, Then, Choose.
• Any iteration required can be identi ed from the wording used, for example,
Loop, Repeat.
• Any processes needed can be identi ed from the wording used, for example,
Set, Calculate.
When the identi er table is complete, each structured English statement can be
used to write one or more pseudocode statements, keeping the same order as the
structured English.
Here is an example of an algorithm to calculate a runner’s marathon time in
seconds, using structured English.

This can be used to identify the variables required and complete the identi er
table

Using these identi ers, each step of the structured English algorithm can be
converted to pseudocode, as demonstrated below.

Zain Merchant
fi
fi
fi
fi
fi
fi
fi
fi
fi
There are three variables used: MarathonHours, MarathonMinutes and
MarathonSeconds. This is explicitly input and implicitly output as the user needs to
understand what input is required. The pseudocode required is as follows.

This is a process using the variables MarathonHours, MarathonMinutes and


MarathonSeconds and using an assignment statement to store the result in
TotalMarathonTimeSeconds. The pseudocode required is as follows.

This is output using the variable TotalMarathonTimeSeconds. The pseudocode


required is as follows.

Writing pseudocode from a


flowchart
Flowcharts are diagrams showing the structure of an algorithm using an agreed set
of symbols, as shown in table

Zain Merchant
Flowcharts can be used to identify any variables required and you can then
complete an identi er table. Each owchart symbol can be used to identify and
write one or more pseudocode statements.
Here is an example of a owchart of an algorithm that can be used to check an
exam grade:

Zain Merchant
fi
fl
fl
The same algorithm is presented in pseudocode on the left. Below is the identi er
table:
3 4 5 and 6 form a nested selection (IF) structure, as each following statement is
part of the ELSE clause. It is only at 7 that the selection is complete. The owchart
shows this clearly and the pseudocode uses indentation to show the nesting.

Zain Merchant
fl
fi
Zain Merchant
Stepwise refinement
The algorithms looked at so far have been short and simple. When an algorithm is
written to solve a more complex problem, decomposition is used to break the
problem down into smaller and more manageable parts. These parts then need to
be written as a series of steps where each step can be written as a statement in a
high-level programming language, this process is called stepwise re nement.
Many problems are more complex than they seem if a robust solution is to be
developed. Look at the rst step of the structured English to calculate a time in
seconds.

The rst step can be further broken down, as follows:

Each of these steps can be broken down further:

These steps can now be written in pseudocode. For example, the input routine for
the seconds:

Zain Merchant
fi
fi
fi
Zain Merchant

You might also like