Il 0% ha trovato utile questo documento (0 voti)
17 visualizzazioni61 pagine

Coding 2

iAS Alogorithms

Caricato da

randomsites7
Copyright
© © All Rights Reserved
Per noi i diritti sui contenuti sono una cosa seria. Se sospetti che questo contenuto sia tuo, rivendicalo qui.
Formati disponibili
Scarica in formato PDF, TXT o leggi online su Scribd
Il 0% ha trovato utile questo documento (0 voti)
17 visualizzazioni61 pagine

Coding 2

iAS Alogorithms

Caricato da

randomsites7
Copyright
© © All Rights Reserved
Per noi i diritti sui contenuti sono una cosa seria. Se sospetti che questo contenuto sia tuo, rivendicalo qui.
Formati disponibili
Scarica in formato PDF, TXT o leggi online su Scribd
Sei sulla pagina 1/ 61

Cambridge iAS / iAL

Computer Science
Coding
1 – Alogorithms
Colour Coding

Main Topic Headings


Sub-headings
Key Words
Practice

Tips!
Key Terms

Structured English – a method of showing the logical steps in an algorithm, using an


agreed subset of straightforward English words for commands and mathematical
operations.
Flowchart – a diagrammatic representation of an algorithm.
Algorithm – an ordered set of steps to be followed in the completion of a task.
Pseudocode – a method of showing the detailed logical steps in an algorithm, using
keywords, identifiers with meaningful names, and mathematical operators.
Stepwise refinement – the practice of subdividing each part of a larger problem into a
series of smaller parts, and so on, as required.
1 - Alogorithms
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 flowchart shows diagrammatically, using a set of symbols linked together with
flow 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, identifiers with meaningful names and mathematical operators
to represent a solution.
• Pseudocode does not need to follow the syntax of a specific programming
language, but it should provide sufficient detail to allow a program to be
written in a high-level language.
Writing simple algorithms using pseudocode

Each line of pseudocode is usually a single step in an algorithm.


The pseudocode is set out using a fixed width font and indentation, where required, of
four spaces, except for THEN, ELSE and CASE clauses that are only indented by two
spaces.
All identifier names used in pseudocode should be meaningful.
Eg:
• The name of a person could be stored in the variable identified 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 identifiers are usually considered to be case insensitive, unlike identifiers
used in a programming language.
It is good practice to keep track of any identifiers used in an identifier table:
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):
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:
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 beneficial if you are studying the full Cambridge
International A Level course.
Python
Visual Basic
Java
To perform iteration using FOR, REPEAT–UNTIL and WHILE loops:
A FOR loop has a fixed number of repeats, the STEP increment is an optional
expression that must be a whole number.
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.
Python
Visual Basic
Java
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 find 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.
This can be written in
pseudocode by making use
of the function INT(x) that
returns the integer part of x:
The identifier table for this algorithm is:
Here are three programs to find 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.
Python
Visual Basic
Java
Practice

In pseudocode, write statements to check that a number input is between 10 and 20 or


over 100. Make use of brackets to ensure that the order of the comparisons is clear,
Practice

In pseudocode, write an algorithm to set a password for a user when they have to
input the same word twice. Then allow the user three attempts to enter the correct
password. Complete an identifier table for your algorithm.
Finally, check your pseudocode algorithm works by writing a short program from your
pseudocode statements using the same names for your identifiers.
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 identified and put in an identifier table
– these can be items input or output as the results of calculations.
• Input and output can be identified from the wording used, for example, Enter,
Read, Print, Write.
• Selection can be identified from the wording used, for example, If, Then, Choose.
• Any iteration required can be identified from the wording used, for example,
Loop, Repeat.
• Any processes needed can be identified from the wording used, for example, Set,
Calculate.
When the identifier 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 identifier table.
Using these identifiers, each step of the structured English algorithm can be converted
to pseudocode:

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:
Flowcharts can be used to identify any variables required and you can then complete
an identifier table.
Each flowchart symbol can be used to identify and write one or more pseudocode
statements.
Here is an example of a flowchart of an
algorithm that can be used to check an exam
grade:
The same algorithm is presented in pseudocode
on the left.
Below is the identifier 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 flowchart shows this clearly and the
pseudocode uses indentation to show the
nesting.
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
refinement.
Many problems are more complex than they seem if a robust solution is to be
developed.
Look at the first step of the structured English to calculate a time in seconds.

The first 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:
Practice

Look at the algorithm to calculate the area of a chosen shape written in structured
English below.
Use stepwise refinement to break each step into more manageable parts then rewrite
the algorithm using pseudocode.
Then check your pseudocode algorithm works by writing a short program from your
pseudocode statements using the same names for your identifiers.

Potrebbero piacerti anche