Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
36 views324 pages

Edexcel Computer Science For GCSE

Dynamic Learning is an online subscription resource designed to support teachers and students in computer science education, specifically for the Edexcel GCSE curriculum. It offers a range of tools including lesson planning resources, interactive eTextbooks, and assessment tracking features. The document outlines the structure of the curriculum, key topics covered, and the importance of algorithms and programming constructs in computer science.
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)
36 views324 pages

Edexcel Computer Science For GCSE

Dynamic Learning is an online subscription resource designed to support teachers and students in computer science education, specifically for the Edexcel GCSE curriculum. It offers a range of tools including lesson planning resources, interactive eTextbooks, and assessment tracking features. The document outlines the structure of the curriculum, key topics covered, and the importance of algorithms and programming constructs in computer science.
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/ 324

COMPUTER SCIENCE FOR

Steve Cushing

| endorsed for d H O D DE R

on patecieninians comin LEARN MORE


DYNAMIC
LEARNING

Dynamic Learning is an online subscription solution that supports teachers and students with high
quality content and unique tools. Dynamic Learning incorporates elements that all work together to give
you the ultimate classroom and homework resource.

Edexcel only endorses the Student book and Student eTextbook. The other resources referenced here
have not been submitted for endorsement.

Teaching and Learning titles include interactive resources, lesson-planning tools, self-marking tests
and assessment. Teachers can:
e Use the Lesson Builder to plan and deliver outstanding lessons
e Share lessons and resources with students and colleagues
e Track student progress with Tests and Assessments

Teachers can also combine their own trusted resources alongside those from Edexcel Computer
Science for GCSE which has a whole host of informative and interactive resources including:
e A ready-made toolkit to deliver the GCSE specification, including a Scheme of Work, lesson plans for
each chapter, outline presentations for all topics, and animated explanations for difficult key concepts
e Practical tasks and examples to develop problem-solving, computational thinking and Edexcel
language-specified programming skills in chosen languages
e Opportunities to develop independent learning skills with activity worksheets and answers, and
additional programming exercises to enhance understanding of certain topics
e Sample Tasks using Linux style commands (to provide practical experience with the command line
interface)

Edexcel Computer Science for GCSE is available as a Whiteboard eTextbook which are online
interactive versions of the printed textbook that enable teachers to:
e Display interactive pages to their class
e Add notes and highlight areas
e Add double-page spreads into lesson plans

Additionally the Student eTextbook of Edexcel Computer Science for GCSE is a downloadable
version of the printed textbook that teachers can assign to students so they can:
e Download and view on any device or browser
e Add, edit and synchronise notes across two devices
e Access their personal copy on the move
RS SS

To find out more and sign up for free trials visit: www.hoddereducation.co.uk/dynamiclearning
G CSE ee ap
date shown. O R
EXC E L Please return / renew by
ED
You can renew at: norlink.norfolk.gov.uk
or by telephone: 0344 800 8006
Please have your library card & PIN ready.

31 eT 2046
1 ANOV 2016
25 NOV 2016

NORFOLK LIBRARY
AND INFORMATION SERVICE

NN30129 078 323

Steve Cushing

pov ic
wamNING
Bes LEAR TUNN
G— EDUCATIO
AN HACHETTE UK COMPANY
E In order to ensure that this resource offers high-quality support for the associated Pearson
qualification, it has been through a review process by the awarding body. This process confirms
that this resource fully covers the teaching and learning content ofthe specification or part of
a specification at which it is aimed. It also confirms that it demonstrates an appropriate balance
between the development ofsubject skills, knowledge and understanding, in addition to
preparation for assessment.
Endorsement does not cover any guidance on assessment activities or processes (e.g. practice
questions or advice on how to answer assessment questions), included in the resource nor does it
prescribe any particular approach to the teaching or delivery of arelated course.
While the publishers have made every attempt to ensure that advice on the qualification and
its assessment is accurate, the official specification and associated assessment guidance materials
are the only authoritative source of information and should always be referred to for definitive
guidance.
Pearson examiners have not contributed to any sections in this resource relevant to examination
papers for which they have responsibility.
Examiners will not use endorsed resources as a source of material for any assessment set by
Pearson.
Endorsement of aresource does not mean that the resource is required to achieve this Pearson
qualification, nor does it mean that it is the only suitable material available to support the
qualification, and any resource lists produced by the awarding body shall include this and other
appropriate resources.

The Publishers would like to thank the following for permission to reproduce copyright material.

Photo credits
p-4 both © TfL from the London Transport Museum collection; p.71 © Photodisc/Getty Images/
Business Today 35; p.133 © Robert Gray; p.150 © istockphoto.com/Mark Evans; p.218 © Robert
Gray; p.219 © Godfried Edelman/iStockphoto.com; p.293 ©Jwrodgers/ Wikimedia (http://
creativecommons.org/licenses/by-sa/3.0).
Hachette UR’s policy is to use papers that are natural, renewable and recyclable products and made
from wood grown in sustainable forests. The logging and manufacturing processes are expected to
conform to the environmental regulations of the country of origin.
Orders: please contact Bookpoint Ltd, 130 Milton Park, Abingdon, Oxon OX14 4SB. Telephone:
(44) 01235 827720. Fax: (44) 01235 400454. Email [email protected] Lines are open from
9 a.m. to 5 p.m., Monday to Saturday, with a 24-hour message answering service. You can also order
through our website: www.hoddereducation.com
© Steve Cushing 2016
First published in 2016 by
Hodder Education,
An Hachette UK Company
Carmelite House
50 Victoria Embankment
London EC4Y 0DZ

www.hoddereducation.com
Impression number 10 9 8 765 43 2 1
Year 2020 2019 2018 2017 2016

All rights reserved. Apart from any use permitted under UK copyright law, no part ofthis publication
may be reproduced or transmitted in any form or by any means, electronic or mechanical, including
photocopying and recording, or held within any information storage and retrieval system, without
permission in writing from the publisher or under licence from the Copyright Licensing Agency
Limited. Further details of such licences (for reprographic reproduction) may be obtained from the
Copyright Licensing Agency Limited, Saffron House, 6-10 Kirby Street, London EC1N 8TS.

Cover photo © Antonis Papantoniou/Hemera/Thinkstock/Getty images


Typeset in Palatino LT Std Light, 11.5/14 pts by Aptara, Inc.

Printed in Italy
A catalogue record for this title is available from the British Library.
ISBN: 9781471866227
Contents

Section one: Problem solving


1 Computational thinking 2
2 Using flowcharts 14
3 Pseudo-code 22

Section two: Programming


SOSH OHEHSSHSHESTHHSHHHSHEHHEHHESHHSHHSSHEHSEHSHHESHSHEHHSHOHSEHOHHTEHHHOEHHEHSEHHHETEHTOSEOOE

4 Mathematical skills in computer science 29


5 Loops and mathematical operations 42
6 Variables and constants 48
7 Programming in code 56
8 Iteration and selection 64
9 Booleans and logic gates (85)
10 Structuring programs in a modular way 82
11 Subroutines 88
12 Testing your code 95
13. Algorithm efficiency 108
14 Comparing pseudo-code, flowcharts and code aba)

Section three: Data


SPOSHHSHSHSHSHHHSHHHHSHHHSHHHHEHHHSHHHTHSOHESOHHTHHOHSHHHEHHHHHOHHHHHHHHOHSEHOOHOOSHOOOOEEEDE

15 The language computers actually use 125


16 Binary and hexadecimal numbers 133,
17 Computing and data representation 145
18 Data types and structures 158
19 Data structures 164
20 Data and program validation and verification 174
21 Data size, storage and compression 180
22 Databases 188
23 Reading and writing to a text file 194
24 Encryption 198
25 Search and sort algorithms 202
Section four: Computers
COSHSOHHOSSOHSHHHOSHOSOHHOESHSHSHOSHHSHSHHHSHHHOHHHSSHSHSHHHHHHHSHHHTSHESESHSHOTEHEEEHEEEEHEEEOE

26 The computer systems architecture 213


27 Memory PAK:
28 Secondary storage Za
29 Cloud computing 232
30 Fetch—decode-—execute cycle 236
31 Software PLeSS)

Section five: Communication and the internet


32 Networks 249
33 Network data transfer 259

Section six: Emerging trends, issues and impact


34 Personal vulnerabilities 269
35 Social engineering and cyber-security 274
36 Ethics and the law 284
37 Embedded systems 292

Section seven: Set practical project


SHSHOSCHOHHHHSHHHHHHHSHHSHSHHHSHHHHSOHHHOHSSHHOHHHOHOHSHSHOHHHHOHHLOHEHEHHHSHOSHHOHHSSEHHESEHHEHOHECOE

38 Project 298

Key points index 305


Summary of features

4 Mathematical skills in computer


SCl Ee al Ce Each chapter clearly references
the understanding and skills
students will need to practise
and exhibit in their

Specification references .2.1_ understand the structural components of


© You should: a program (variable and type declarations,
command sequences, selection, iteration, data
1.1.2 understand how to create an algorithm to
structures, subprograms)
solve a particular problem, making use of
programming constructs (Sequence, selection,
2.2.2 be able to use sequencing, selection and
iteration constructs in their programs
iteration) and using appropriate conventions
(flowchart, pseudo-code, written description, 2.5.1 understand the purpose of, and how to use,
draft program code) arithmetic operators (add, subtract, divide,
multiply, modulus, integer division)
1.1.3 understand the purpose of a given algorithm
and how an algorithm works 2.5.2 understand the purpose of, and how to use,
relational operators (equal to, less than,
: 1.1.4 understand how to determine the correct
greater than, not equal to, less than or equal
: output of an algorithm for a given set of data
to, greater than or equal to)

The study of algorithms did not start with the invention of


computers, indeed the example we have been using so far in the
book of adding two numbers together is a mathematical problem.
also understand how they work in mathematics. You
Practice Tasks will help build
> to apply mathematical skills relevant to computer
students’ coding, programming
en the term algorithm is used in mathematics, it
m-sol
t of steps used to solve a mathematical problem. The
algorithm for performing long division or multiplication is a good
example.
1 Write the pseudo-code where
If you were carrying out a long division for 52 divided by 3, you
the user inputs the dimensions
would have the following specific sequence of steps and their
of a rectangle and the
outcomes:
perimeter of the rectangle is
@ How many times does 3 go into 52?
shown on the computer screen.
® The answer is 17 (3 x 17 = 51)
@ How many are left over? The answer is 1
! Key point @ How many times does 3 go into 10?
For the examination you will ® The answer is 3 with 1 left over.
need to understand the purpose @ And of course the answer becomes 17.3333333
of, and how to use, arithmetic The step-by-step process used to do the long division is called a
operators (add, subtract, divide, long division algorithm.
multiply, modulus, integer
division).
Algorithms are used a lot in mathematics, especially in algebra.

Key Points clarify significant information fot


students to be able to process and recall
easily and include Key Terms to help students |
develop seni Unaine language skills to facilitate
ding
Summary of features
nnn nnn esemmmmmnmmmezemamemensmmmemmensnneeacaseneetn eet eee

Constants
ee
ES ES eee

If a variable contains non-numeric information (e.g. names)


then it clearly should be saved as a character string. But if the
variable contains real numeric data which will be used in numeric
calculations, such as weight or height, then it should be stored as
a numeric variable.
NSCS MCASTS complex when you want to store something like a
eI example male or female. It is actually better to store
Bee Coles numeric variable with an appropriate format, rather
‘than a character variable with, for example, values ‘M’ and ‘FP’.
Numeric data is more efficient and gives you more processing and
| 2 What is a constant? data analysis possibilities, therefore it is better to store M and F
3 Describe the main reasons
numerically as 0 and 1. As a general rule it is always preferable to
why it is important to choose
save it as a variable of type numeric rather than a variable of type
character, even if you have no intention of performing algebraic
descriptive names for variables.
calculations using the variable.

Chapter review
® Understand and be able to determine the correct
In this chapter we have explored variables and output of an algorithm for a given set of data (More
assignment. We also explored the importance of on this topic in Chapters 2, 3, 4,)
correctly naming your variables. ® Understand and be able to code an algorithm
Remember, before tackling any computer science task in a high-level language (More on this topic in
or examination question on this topic you must: Chapters 7, 8, 14, 5, 11, 10)
® Understand and be able to explain what an ® Understand and be able to explain how the choice
algorithm is, what algorithms are used for and be of algorithm is influenced by the data structures
able to interpret algorithms (flowcharts, pseudo- and data values that need to be manipulated
code, written descriptions, program code) (More on (More on this topic in Chapters 7, 8, 14, 5, 11,
this topic in Chapters 1, 2, 3, 7, 8, 14, 5, 11, 10) 10, 18)
® Understand and be able to create an algorithm ® Understand and be able to explain the benefit of
to solve a particular problem, making use of producing programs that are easy to read and be
programming constructs (sequence, selection, able to use techniques (comments, descriptive
iteration) and using appropriate conventions names (variables, constants, subprograms),
(flowchart, pseudo-code, written description, draft indentation) to improve readability and to explain
program code) (More on this topic in Chapters 2, 3, how the code works (More on this topic in
AN Tf ts, WAL, Sy, ln IC) Chapter 3)
® Understand and be able to explain the purpose of a ™Be able to use sequencing, selection and iteration
given algorithm and how an algorithm works (More constructs in your programs (More on this topic in
on this topic in Chapters 2, 3, 4, 7, 8, 14, 5, 11, 10) Chapters 3,4, 7, 8714) 5) 1a10):

Each chapter concludes with a


Review of key information to
ind consolidate topic
knowledge and understanding
1 Computational thinking

: i Specification references
You should:
1.2.2 be able to decompose a problem into smaller
| 1.1.1 understand what an algorithm is, what
sub-problems
algorithms are used for and be able to interpret
algorithms (flowcharts, pseudo-code, written 1.2.3 understand how abstraction can be used
descriptions, program code) effectively to model aspects of the real world

» 1.2.1 be able to analyse a problem, investigate 1.2.4 be able to program abstractions of real-world
requirements (inputs, outputs, processing, examples
initialisation) and design solutions 4.1.1 understand the input-process-output model

Before you can succeed in computer science you must learn about
! Key points
what is called computational thinking. Computational thinking
m 4 Computational thinking involves learning how to use a set of problem-solving skills
| involves learning how to use and techniques that are used by computer programmers to
a set of problem-solving skills write programs. Computational thinking is not thinking about
and techniques that are used computers or even thinking like a computer. Computers don’t
by computer programmers to think for themselves. If you give ten computers the same
write programs. instructions and the same input, they will give exactly the same
A For the examination you will output. Computers are predictable.
need to understand how to
Computer scientists use logical reasoning to work out exactly what
create an algorithm using a
a program or computer will do. Computational thinking is about
written description.
considering a problem in a logical way so that a computer could
help us to solve it. This logical reasoning is the essential building
block of computer science. So first we need to fully understand
the techniques involved and how we start with a problem and end
up with the programming code.
Two important techniques used in computational thinking are:
@ Decomposition: This is breaking any given task or problem into
simple logical steps or parts.
® Abstraction: This is the process of taking away or removing
irrelevant characteristics in terms of the problem being solved
in order to reduce it to something simpler to understand. In
computer science, abstraction is often used for managing the
complexity of computer systems.
Decomposition

] Key points
__[ The Probleaem_| ee
A Logic programming is a Decompostition
method of writing computer
programs based on the
mathematical study of logical 7Reareech to
0 | |Approach to |
reasoning. part of the | | part of the | | part of the |
4 Decomposition means ——— | Le Lil e Problem |
breaking a problem into a
number of sub-problems,
so that each sub-problem SLCOS aaa
accomplishes an identifiable
‘ Particular
task, which might itself be The algorithm
further subdivided. Figure 1.1 Decomposition and abstraction
4 Abstraction is the process of
We will explore each of these in detail later in the book but let’s
removing unnecessary detail
start with a simple example of decomposition and abstraction as
from a probiem.
it relates to problem solving.

§ Decomposition
Decomposition is a term used
When a chef writes a recipe for a meal, he is creating a set of
for the separation of a task into
instructions that others can follow to replicate the meal. Each part
discernible parts, each of which
of the recipe is listed separately, the overall meal is decomposed
is Simpler than the whole.
into separate dishes, and these are often decomposed further, e.g.
making the pastry and the filling.

a Abstraction
1 Use this book and other
sources such as the internet You may have come across the term ‘Abstract Art’ where a painting
to research how to decompose is a set of shapes representing the picture. A good example of
a problem into smaller sub- the use of abstraction for technical purposes is the London tube
problems. map. It is the brainchild of an electrical draughtsman named
2 Use this book and other Harry Beck. Rather than emphasising the real distances and
sources such as the internet to geographical location of all the tube lines, Beck stripped away
research how abstraction can the sprawling tube network by abstracting just the information
be used effectively to model needed by travellers. He then used this to create an easy-to-read
aspects of the real world. diagram of coloured, criss-crossing lines common in electrical
diagrams.

! Key points
A |n abstraction we remove unnecessary details from a problem until we
can represent the problem in a way that is possible to solve.
A For the examination you will need to be able to interpret written
descriptions.
1 Computational thinking

Figure 1.2 A geographical map of the London underground Figure 1.3 An original abstracted map of the London
stations underground

Solving a problem using abstraction


Let’s look at a simple example of abstraction.

Figure 1.4 Fred wants to cross the river

A man called Fred wishes to cross a 10 metre wide river with a


wolf, a white goat and a bale of newly cut hay. He has a small
blue boat and oars, but unfortunately he can only take one thing
across at a time. The problem is, if he leaves the wolf and the goat
alone together, the wolf will eat the goat, and if he leaves the
goat with the hay, the goat will eat the hay. They are currently all
together on the same bank.
How does he do it?
There is a simple computational approach for solving this problem.
You could of course simply try all possible combinations of items
that may be rowed back and forth across the river. Trying all
Abstraction

possible solutions to a given problem is referred to in computer


science as a brute force approach. But logical thinking will bring
about a better solution.
Only the relevant aspects of the problem need to be represented;
all the irrelevant details can be ignored. A representation that
A representation that leaves out leaves out irrelevant details of what is being represented is a form
unnecessary details of what is of abstraction. So what can we leave out?
being represented is a form of
abstraction.
Is the man’s name relevant?
Is the width of the river relevant?
Is the colour of the boat relevant?
3 Use this book and other We'll start:
sources such as the internet
to research how to analyse
Banks are A and B
a problem, investigate Goat =G
requirements (inputs, outputs,
processing, initialisation) and
IEW = heal
design solutions. Wolf = W
Man=M

So to start with we have:

A B
G H W M

We need to end up with:


A B
G H W M

Each step we show needs to correspond to the man rowing a


particular object across the river (or the man rowing alone).
Let’s look at the first step:
B
G M H Ww

The man (M) has taken the goat (G) to the other side of the river.

! Key points
4 Solve the rest of the man ina A Abstraction draws out the essence of a problem - by solving it we
boat problem. can also see what other problems can be solved using the same
techniques.
A For the examination you will need to be able to decompose a problem
into smaller sub-problems.
1 Computational thinking
pel
h

§ More than one solution to any problem


Pa,
3)
ie

There will always be a number of methods to solve the same


problem, but you always need to create ordered steps to solve any
of these solutions.
Let’s look at a simple problem. Imagine a map; you are given
a starting point and the point you wish to arrive at. The map
contains a grid to help navigation. The map grid has numbers in
the vertical axis, and letters in the horizontal axis. Let’s say we
start at 10C and want to arrive at 15L.
This diagram shows four possible pathways. There are of course
many more. We could take a very complicated route, but we want
to be efficient and take as few moves as possible.

B CH |e) E
Figure 1.5 Four possible pathways

We could describe each of these pathways using words; pathway


‘A’ for example could say move north until you reach map reference
15, then turn right 90°, now move forward to map reference ‘L’.
We can also describe the path using distances rather than the grid
For the examination you will need
positions, for example move forward five, turn right 90°, move
forward nine.
to
A understand how abstraction Of course both of these directions will only work if people
can be used effectively to follow them exactly. We have abstracted the problem as an
model aspects of the real example and we do not refer to M9 for example. We could make
world the directions better by refining the instructions and adding
A be able to program more detail, for example telling the user what to do if they go
abstractions of real-world wrong. We could add a position check. If you can understand
examples. these concepts, you are well on the way to being able to write
computer programs.
bes
Choosing the best solution
by

§ Choosing the best solution


! Key points We know there are many answers to the same problem, so what
makes the best solution and would lead to the best algorithm?
4 An algorithm is simply a set of
steps that defines how a task The first criteria we need to consider are does the solution:
is performed. ® work?
© complete its task in a finite (reasonable) amount of time?
4 A program is a sequence of
instructions to perform a task. We have lots of solutions to our problem and each, whilst very
different, satisfies these two criteria. Therefore, the next step is to
determine which of our solutions is ‘best’.

There are generally two criteria used to determine whether one


For the examination you will need computer algorithm is ‘better’ than another and these are:
to understand what an algorithm is. @ the space requirements (i.e. how much memory is needed to
complete the task)
@ the time requirements (i.e. how much time it will take to
complete the task).
A third criterion that we can consider is the cost of human
5 Use this book and other sources
coding time — the time it will take us to develop and maintain the
such as the internet to research
program. A clever coding system may improve the space and/or
what an algorithm is and what
time requirements but result in a loss of program readability and
algorithms are used for.
increased human cost to maintain the program.

§ What is an algorithm?
The word ‘algorithm’ comes from the ninth-century Arab
I Key points mathematician, Al-Khwarizmi, who worked on ‘written processes
A An algorithm is not a computer to achieve some goal’. The term ‘algebra’ also comes from the
program; a computer program term ‘al-jabr’, which he introduced. You have already started to
is the implementation of an write algorithms by developing pseudo-code.
algorithm. Algorithms are at the very heart of computer science. An
A Programs must be carefully algorithm is simply a set of steps that defines how a task is
designed before they are performed. For example, there are algorithms for cooking (called
written. During the design recipes), algorithms for finding your way through a strange city
stage, programmers use tools (directions), algorithms for operating washing machines, and
such as pseudo-code and even algorithms for playing music (sheet music).
flowcharts to create models of
A scientific description of an algorithm would be:
programs.
A A process is a series of ‘a series of unambiguous steps to complete a given task in a finite
actions or steps taken in order amount of time’.
to achieve a particular end. An algorithm has input data, and is expected to produce output
data after carrying out a process, which is the actions taken to
e Question achieve the required outcome. You will need to understand this
input—process—output model as much of what you will learn in
1 What is an algorithm?
computer science is founded upon this model.
1 Computational thinking

§ The input-process—output model


] Key point
Processing
A You must be able to identify
where inputs, processing and
outputs are taking place within
an algorithm. Storage

Feedback

Figure 1.6 A computer considered as an input-process-output model

A computer can be described using a simple model as shown.


The INPUT stage represents the flow of data into the process from
outside the system.
The PROCESSING stage includes all tasks required to effect a
For the examination you will transformation of the inputs.
need to:
The OUTPUT stage is where the data and information flow out of
A understand what algorithms the transformation process.
are used for
A understand the purpose of a
You will notice that we have added two new parts to the model:
given algorithm and how an
storage and feedback.
algorithm works The STORAGE stage keeps the data until it is needed.
A understand the input-process-
In solving any problem, you must follow this model. You define
output model
the problem, defining what the solution must be, and then work
A be able to analyse a problem, on the transformation (process) to achieve the desired solution.
investigate requirements Feedback occurs when outputs of a system are routed back as
(inputs, outputs, processing, inputs that form a circuit or loop.
initialisation) and design
solutions. Sometimes programmers even plan out their code using these
headings.

§§Sequences
To solve a problem there must be a sequence of steps. In computer
science, a sequential algorithm is an algorithm that is executed
sequentially, one step at a time from start to finish. It does this
without any other processing executing. Most standard computer
algorithms are sequential.

In computer science, to execute is the process of carrying out an


instruction by a computer.

Let’s look at a simple example, say we want to input two numbers,


add them together and show the answer on the screen.
———
ee
Sequences

Input Process - Output


Two numbers add the first number to the new number
the second

We could write this as the following sequence:


INPUT first number #input stage
INPUT second number #input stage
ADD first and second number
together and STORE as total #process stage
OURPUT total #output stage

Saving an INPUT with a name is called assignment, for example


in this instance we input a number and called it ‘first number’,
then we input another number and called it ‘second number’. It is
called this because we ‘assign’ a name to the value. In computer
programming, an assignment statement sets and/or re-sets the
value stored in a storage location. We will explore this in more
detail later in the book.

Why are sequences so important?


In computer programming you have to first work out the correct
sequence of commands. This may sound simple but let’s look at
an example to show how careful you need to be. If you write down
your friend’s address it may look like this:
John Smith
22 Holly Road
Hempton
London

You know that this is the order that you should write an address,
but this is the exact opposite of the way that the postal system
! Key point works. There are millions of John Smiths. There may be hundreds
Many computer programmers
of Holly Roads, many of them with a number 22. The postal
system needs to know the address in the logical task order,
label their files using the date
format year, month, day as this is
meaning the order that you go about finding the address; that is
London first, next the area, then the road and then the number.
the logical way to automatically
list them, the year being the In some countries the conventional order follows the logical task
first piece of data required, the order. In Russia letters are addressed in exactly the opposite order
month next and the day last. This to the UK with the city first.
is because there can be 12 files
For your programming to work correctly, all the commands
with the same day number in a
have to be there AND they need to be in the correct sequence.
single year.
Sequencing is extremely important in programming.
* 1 Computational thinking

Decomposition and sequences


We’ll now look at a simple problem in terms of input, process and
output.

Sample System Diagram

2G), ; ——— (e)


f ~_ ee, =
6 Create a sequence to show
a (aa
how to make cheese on toast.
ses
e Water e An electric kettle is filled @ Tea
e Tea bags with water

e Boiling water ® Tea bags are placed in


the tea pot
e Milk
e The boiling water is poured into
e Sugar the tea pot and the tea brews
e Milk is added to the cup
e Sugar is added to the cup
e The ‘brewed’ tea is poured
into a cup
Figure 1.7 A sample system diagram

Let’s say we want to make a cup of tea using tea bags and a kettle.
The sequence could look like this:

Input SS ~ Process
Water An electric kettle is filled with water Tea
Tea bags Tea bags are placed in the tea pot
Boiling water The boiling water is poured into the tea pot and the
tea brews
Milk Milk is added to the cup
Sugar Sugar is added to the cup
The ‘brewed’ tea is poured into a cup

We would do the same to make more than one cup of tea but we
would only fill the kettle and boil it once. We could divide the
7 Use this book and other sequence into two parts.
sources such as the internet
to research how to evaluate Sequence 1
the fitness for purpose
Count number of people wanting tea #input
of algorithms in meeting
Fill the kettle with enough water #input and process
specified requirements.
Boil the water #process
Put tea bag in pot #input and process
Pour on water #input and process
Allow tea to brew #process

10
sss
Sequences

Sequence 2

For each person wanting tea #requires input from


#first sequence
Pour brewed tea from pot to cup #process
Gidents lave been deed to chow Add sugar to cup #input and process
the parts of the sequence we Add milk to cup #input and process
would repeat. Serve tea #output and process

We would need to carry the number of people wanting tea from


the first sequence to the next sequence so we can repeat the
second sequence of the task making a cup of tea for each person
but only boiling the water once. We could do this by creating what
is called a variable.
Count the number of people wanting tea
Store answer in a variable called teaCount
Fill the kettle with enough water
Boil the water
Put tea bags in cups
Pour on water
Allow tea to brew
Repeat the following steps for the number stored in the
variable called teaCount
Add sugar
Add milk
End Repeat
Serve

We could of course extend this by asking who wants sugar or


milk; this is called an IF statement construct.

Do you require sugar?


If answer is yes
Add sugar
End If construct
Do you require milk?
If answer is yes
Add milk
End If construct
We could also run parts of the task in parallel, for example whilst
the kettle is boiling we could add the tea bags to the cups and
we would almost certainly add all the tea bags to the cups before
adding the water. We would not add one tea bag, then add the
water to that cup before adding the next tea bag.
‘ 1 Computational
a thinking

Explore all these possibilities and represent them as simple


English sentences. If you do this you have just decomposed a
problem and have started to create a program. We will explore
all of the concepts you need such as ‘loops’ and ‘if’ statements in
more detail later in the book. For now you just need to understand
how to break a problem down into simple steps and how to group
these steps into separate parts of the task.

§ Modularity
There are several advantages to designing solutions in a structured
! Key points manner. One is that it reduces the complexity as each set of steps
A A subroutine is a sequence can act as a separate module. Modularity allows the programmer
of instructions that is set to tackle problems in a logical fashion. Modules can also be reused.
up to perform a frequently
To develop modules the programmer needs to carry out what is
performed task.
called decomposition. This is to break down a problem into easy-
A A procedure is a subroutine to-understand steps.
that does not return values.
Because modules can be reused many times, this saves time and
reduces complexity, as well as increasing reliability as the modules
will have already been tested in another program. It also offers an
easier method to update or fix the program by replacing individual
modules rather than having to fix larger amounts of code.
One reason for modular (also called structured) programming is
In pSeudo-code a computer can that it avoids the increased possibility of data corruption often
repeat a group of actions using caused by using a large number of lines of code. It also ensures
REPEAT-UNTIL. that there is a logical structure in the program being written to,
which makes the code more efficient and easier to understand
and modify.
? Question Structured programming makes extensive use of subroutines,
2 What is an algorithm? block structures and ‘for’ and ‘while’ loops. We will explore all of
these later in the book.

§ Algorithms and code


Each instruction should be carried out in a finite amount of time.
An algorithm, given the same input, will always produce the
same output. During the processing the underlying algorithm
will always pass through the same sequence of states.
Since we can only input, store, process and output data on a
computer, the instructions in our algorithms will always be limited
to these functions.
First we must not only fully understand the problem but give each
item a name before solving it:
The term ‘Call’ to Subroutine
® Identify and name each Input/Givens.
means the code inside the
® Identify and name each Output/Resullts.
subroutine should be executed.
@ Assign a name to our algorithm (Name).
Recording your ideas Mi

Combine the previous three pieces of information into a formal %


statement (Definition).

i Recording your ideas


Once we have abstracted the necessary data and understand the
sequences involved, rather than writing a long text explaining the
problem and its solution, we need to find a way to record our
thinking and the solutions to the problem. The most effective way
is to use either a flow diagram or pseudo-code.

Chapter review

In this chapter we have explored computational thinking including


decomposition and abstraction. We also looked at the input, process
and output model and explored the importance of sequences.
Remember, before tackling any computer science task or examination
question on decomposition and abstraction you must:
® Be able to analyse a problem, investigate requirements (inputs,
outputs, processing, initialisation) and design solutions
® Be able to decompose a problem into smaller sub-problems
® Understand and be able to explain and apply abstraction effectively
to model aspects of the real world
® Be able to program abstractions of real-world examples
® Understand and be able to explain and apply the input-
process-output model.
2 Using flowcharts
e

‘ F Specification references iteration) and using appropriate conventions


You should: (flowchart, pseudo-code, written description,
draft program code)
1.1.1 understand what an algorithm is, what

algorithms (flowcharts, pseudo-code, written and how an algorithm works


descriptions, program code) 1.1.4 understand how to determine the correct
1.1.2 understand how to create an algorithm to output of an algorithm for a given set of data
solve a particular problem, making use of 2.4.1 understand how to write code that accepts and
programming constructs (Sequence, selection, responds appropriately to user input

. ! Key points There are a lot of different design procedures and techniques for
building large software projects. The technique discussed in this
A A flowchart is a diagram chapter, however, is for smaller coding projects and is referred to
representation of an algorithm. by the term ‘top down, structured flowchart methodology”. We will
‘ A For the examination, you will explore how to take a task and represent it using a flowchart. A
need to be able to interpret flowchart puts the sentences from a sequence into shaped boxes.
flowcharts. The shapes indicate the action.
. | A Flowcharts are a graphical
You will know from the last chapter that a sequence is where a set
method of designing
of instructions or actions are ordered, meaning that each action
programs.
follows the previous action.

;
A A well-drawn flowchart is easy
to read.

! Key point
In a flowchart the lines express
the order of execution. Figure 2.1 A sequence

14.
SSS
: Flowchart advantages ewe
te
he

§ Flowchart advantages |
@ Flowcharts are a graphical way of writing pseudo-code.
@ They are all standardised: we all pretty much agree on the
symbols and their meaning.
@ They are very visual.

§ Flowchart disadvantages
@ Flowcharts can be time consuming and difficult to modify.
@ They need special software for symbols although some software
has these built in.
® The structured design elements are not all implemented.

§ General rules for flowcharts


@ All symbols of the flowchart are connected by flow lines
! Key points (these must be arrows not lines to show direction).
A Flowcharts must have flow @ Flow lines enter the top of the symbol and exit out the bottom,
lines with arrows to show the except for the Decision symbol, which can have flow lines
order of execution. exiting from the bottom or the sides.
A An algorithm is a sequence of @ Flowcharts are drawn so flow generally goes from the top to
steps that can be followed to the bottom of the page.
complete a task. e The beginning and the end of the flowchart is indicated using
A A sequence is where a set the Terminal symbol.
of instructions or actions are Let’s look at a simple sequence. Say we want to calculate A plus B,
ordered, meaning that each where A = 200 andi — 400;
action follows the previous
action. Start
A = 200 B = 400
Add = 200 + 400
Output = 600
End
We could create the simple flow-
chart shown in Figure 2.2.
Let's look at a another sequence,
for example the sequence you carry
1 Produce a sequence to show
out each morning in the bathroom.
how to brush your teeth. Figure 2.2 A simple flowchart
This sequence could be:
@ Brush teeth
2 Questions @ Wash face
@ Comb hair.
1 What is a terminator?
2 What is a sequence? As you can see, sequences are a useful tool for showing what
happens and in what logical order, but each step, for example
3 What are inputs and outputs?
‘brush teeth’, needs to be defined in more detail to be carried out.
2 Using flowcharts
ka
a pr

Once we have picked up our toothbrush, turned on the tap and


added the toothpaste we can put the toothbrush in our mouth and
brush. The act of actually brushing your teeth could be recorded
in a linear way (press, brush up, brush down, brush up, brush
down, etc.), but it would be much simpler to explain the brushing
once and then tell the user to repeat the same action x times. We
will explore this later when we consider looping (iteration), but for
now let us explore how we can use a flowchart to represent simple
sequences. First we need a few more elements.

Cleaning your teeth is called a procedure in coding. You perform the


same action every day, for example: pick up brush, put toothpaste on
brush, brush teeth for two minutes, spit out, clean brush. These actions
could be given a procedure name: ‘Brushing Teeth’.

Basic elements of flowcharts


A You use a PROCESS symbol
for an OPERATION or ACTION The flowchart symbols denoting the basic building blocks of
STEP. programming are shown in Figure 2.3 below. Text inside a symbol
is called a label.
A You use a TERMINATOR
symbol for a START or END in
a PROCESS.
A You use a DECISION symbol
for a QUESTION or BRANCH of
a process. START | qi DECISION
4 Flowchart symbols contain text : PROCESS —
called labels. END =
A For the examination, you will
need to understand how to
create an algorithm using a
flowchart. START END PROGESS DECISION
Symbol Symbol Symbol Symbol
Figure 2.3 Basic elements of a flowchart

The START symbol represents the start of a process.


The process symbol is labelled with a brief description of the
process carried out by the flowchart. The END symbol represents
the end of a process. [t contains either ‘End’ or ‘Return’ depending
on its function in the overall process of the flowchart.

Representing a process
A PROCESS symbol is representative of some operation that is
carried out on an element of data. It usually contains a brief
description of the process being carried out on the data. It is
possible that the process could be even further broken down into

ae,
a
Basic elements of flowcharts |
SS =

Key points simpler steps by another complete flowchart representing that


process. If this is the case, the flowchart that represents the
A All flowcharts must have a process will have the same label in the ‘Start’ symbol as the
START and an END symbol. description in the ‘Process’ symbol at the higher level. A process
A The DECISION symbol will always has exactly one input arrow and one output arrow.
have exactly one input and two
outputs In practice, sequences are not a simple line. Often the next action
depends on the last decision. This is called selection. In selection,
one statement within a set of program statements is executed
depending on the state of the program at that instance. We ask a
question and choose one of two possible actions based upon that
decision.

Representing a decision
A DECISION/SELECTION symbol always makes a Boolean choice. We
e Key points
will explore Booleans in more detail later in the book. The label in
A One of the most confusing a decision symbol should be a question that clearly has only two
things in a flowchart is telling possible answers to select from.
the loops apart from the
selections. This is because
both use the diamond shape
as their control symbol. Mark
them clearly.
true?
A A ‘Decision’ symbol always
makes a Boolean choice.
[eyes

Figure 2.4 How to represent a decision

The DECISION symbol will have exactly one input arrow and two
output arrows. The two outputs will be labelled with the two
answers to the question, in order to show the direction of the
logic flow depending upon the selection made.
Selections are usually expressed as decision key words, such as ‘if
.. then ... else ... endif, switch or case’. They are at the heart of all
programming.

Condition
Then | true? | Else

Action 2

? Questions
4 What is a flowchart?
5 What is a selection? Figure 2.5 A flowchart representing a selection
2 Using flowcharts

Flowcharts can use the following symbols:


| __ Purpose =
Flow line The lines show the
sequence of operations.

Terminal Denotes the start and end


Start (Start/Stop) of an algorithm.

Processing Denotes a process to be


carried out.
. Delay
Alarm
Set for
onde 5 mins Decision Used to represent the
operation in which there
are two alternatives, true
and false.

Hit Snooze We can use a decision to create a flowchart of what happens in


button the morning on school days, as shown in Figure 2.6.
We also explored selections when we looked at the sequence
of making tea. We explored using IF someone wants sugar and
IF someone wants milk. The process of making the tea differed
Climb out according to their answer to these questions.
of bed
The flowchart below shows a different process for making tea and
adds two decision boxes.

End
Start 7) Boil kettle
Figure 2.6 A flowchart showing what - —
happens on a school morning

Pour water
in mug

Drink tea

Figure 2.7 A flowchart showing a different process for making tea


Basic elements of flowcharts a

If we wanted to show how to play the game of snakes and ladders,


we could explain how to play the game in English as follows:
Start game
Throw the dice: the number indicated by dice is x.
Move your counter x squares on the board and check:
Have you landed on snake’s head?: no/yes
If yes slide down snake to its tail.
If no check next statement
Have you landed on the bottom of the ladder?: no/yes
If yes move up the ladder.
! Key point If no check next statement
Have you reached the last block of the game?: no/yes
Repetition is used when the
If yes output 'you are the winner’
same bit of code is needed
several times. Instead of writing it If no give the dice to the next player
over and over again, you can use Repeat until someone reaches the last block of the game.
the REPEAT command. Repetition End
can also be called iteration
(looping). We have more decisions in this example and could show the game
with the following flowchart.

Give dice to
next player

Slide down |
tail of snake |

_ Move counter Move up


‘number of places | ladder
| shown on dice

Output ‘You are


the winner’

End
Yes

Figure 2.8 A flowchart showing the game of snakes and ladders


> 2 Using flowcharts
We , ;

io Other structures we will use in this book include:

SEQUENCE SELECTION INTERATION


Structure (ES THENSS ELSE) (WHILE)
Structure Structure

Figure 2.9 Other structures we will use in this book

On-page and off-page connectors may also appear in some


flowcharts. This occurs when a flowchart goes over more than
one page. For the purposes of this chapter we will only explore
flowcharts that can be represented on a single page. If a flowchart
is so big it needs to go onto another page, you should split it into
sub processes

Subprocesses
We can also use subprocesses in flowcharts using the symbol
below.

Figure 2.10 The subprocess symbol

Subprocesses are useful because:


@ they help with the modularisation of complex programs
@ they provide a way of simplifying programs by making common
processes available to a wide number of programs
® they lead to more reliable programs since once a process is
tested and works it can be made a subprocess and need not be
tested again.
In flowcharts subprocesses are also useful in sticking to the rule
that a flowchart should fit on a single page.

a
Basic elements of flowcharts

Figure 2.11 shows an example of the main page of a flowchart.


It contains two subprocess symbols. Each subprocess symbol
contains text which describes briefly what the subprocess does.
Each subprocess symbol also contains a page reference where the
| Initialise flowchart for the subprocess will exist.
variables

Chapter review

In this chapter we built upon the last chapter to explore sequences in


calculation more detail and established how we can show these using flowcharts.
a aeSeript
Kaas See2, 2
We looked at the basic elements of flowcharts and introduced the
concept of decisions and how these can be represented.
Remember, before tackling any computer science task or examination
question on this topic you must:
~ Print sum
® understand what an algorithm is and what algorithms are used for,
and be able to interpret algorithms in the form of flowcharts
® use a systematic approach to problem solving and algorithm creation
representing those algorithms using flowcharts
iEnd
® understand and be able to use appropriate conventions in flowcharts
Figure 2.11 The subprocess ® understand and be able to explain the purpose of a given algorithm
and explain how it works
® understand and be able to explain and determine the correct output
of an algorithm for a given set of data.
3 Pseudo-code

bs Specification references

understand what an algorithm is, what understand the benefit of producing


algorithms are used for and be able to interpret programs that are easy to read and be able
algorithms (flowcharts, pseudo-code, written to use techniques (comments, descriptive
descriptions, program code) names (variables, constants, subprograms),
understand how to create an algorithm to indentation) to improve readability and to
solve a particular problem, making use of explain how the code works
programming constructs (sequence, selection, understand the structural components of
iteration) and using appropriate conventions a program (variable and type declarations,
(flowchart, pseudo-code, written description, command sequences, selection, iteration, data
draft program code) structures, subprograms)
understand the purpose of a given algorithm be able to use sequencing, selection and
and how an algorithm works iteration constructs in their programs
understand how to determine the correct understand how to write code that accepts and
output of an algorithm for a given set of data responds appropriately to user input

§ Basic elements of pseudo-code


Pseudo-code is another way to develop an algorithm. It consists
of natural language-like statements that precisely describe the
steps required.
Pseudo-code must:
® contain statements which describe actions
® focus on the logic of the algorithm or program
! Key points ® avoid language-specific elements
A Pseudo-code is a language @ be written at a level so that the desired programming code can
designed to express algorithms be generated with little effort from each statement
in an easy-to-follow form. ® contain steps, subordinate numbers and/or indentation used
A Pseudo-code is an easy-to-
to show dependent statements in selection and repetition
structures.
read language to help with
the development of coded
Pseudo-code advantages
solutions.
@ Pseudo-code is similar to everyday English.
A When writing in pseudo-code
@ It helps programmers to plan an algorithm.
resist the urge to write in
@ It can be done easily on a word processor.
whatever language you are
@ It is easily modified.
most comfortable with.
® It implements structured concepts well.

22
The importance of syntax

Pseudo-code disadvantages
@ Pseudo-code is not visual like flowcharts.
@ There is no accepted standard, so it varies widely.
@ It is not an actual programming language.
2? Question @ It is an artificial and informal language.
4 What is pseudo-code? ® Some people have a tendency to put actual code in. This makes
it harder to understand.

§ The importance of syntax


Syntax is the set of rules, principles and processes that enable us
to understand a language. The syntax rules of a language define
the spelling and grammar and as with natural human languages
each language has its own rules. Computers are very inflexible
and understand what you write only if you state what you want
in the exact syntax that the computer expects and understands.
Each programming language has its own rules and specialist
‘ Key point
syntax including the words that the computer understands, which
For the examination you will need combinations of words are meaningful, and what punctuation is
to understand how to create necessary for the code to be correctly structured. Whilst pseudo-
an algorithm using appropriate code does not have a fixed syntax, you will need to understand
conventions (flowchart, pseudo- the syntax used in the examination papers. Understanding the
code, written description, draft importance of syntax is also vital when you start using a
program code). programming language.

Symbols
When we write code in English we also use symbols in the form of
punctuation. Symbols are used because they are human-readable.
The symbols you use are important as they have an effect in your
code.
these-words-are-separated-by-a-symbol #the - is the symbol used
#here
there is alsoasymbolinthis sentence #here the space is the
#symbol used

Symbols can also be used as what are called identifiers. In some


The symbols <, <<, <, -, = are programming languages, they are also called atoms rather than
often used as what are called symbols.
‘assignment operators’ in
The symbols <, <<, <, - and = are often used as what are called
pseudo-code.
operators.
3 Pseudo-code

v
;\

In pseudo-code, you use the following syntax to receive data from


a device. The red brackets < > are only to show where you add
something; you don’t need to put them in your code.
Syntax
1 Write the pseudo-code where RECEIVE <add identifier here> FROM (type) <add device
the user inputs the dimensions here>
of a rectangle and the area of Examples
the rectangle is shown on the RECEIVE Name FROM (STRING) KEYBOARD
computer screen.
or

RECEIVE LengthOfJourney FROM (INTEGER) CARD_READER


or
RECEIVE YesNo FROM (CHARACTER) CARD_READER

Common action keywords


Several keywords are often used to indicate common input, output
and processing operations.
@ Input: READ, OBTAIN, GET
@ Output: PRINT, DISPLAY, SHOW
@ Process/compute: COMPUTE, CALCULATE, DETERMINE
@ Initialise: SET, INIT
@ Add one: INCREMENT
In pseudo-code, you use the following syntax to send output to
the screen. The red brackets < > are only to show where you add
e Key point something; you don’t need to put them in your code.
For the examination, you will
Syntax
need to be able to interpret
program code. SEND <add expression here> TO DISPLAY
Example
SEND ‘Have a good day.’ TO DISPLAY

Whilst there is no common way of writing pseudo-code, in


this book we have written the commands in capital letters to
differentiate them from the examples in Python and to help you
understand what the command words are.
Questions in the Edexcel written examination that involve code
will use the following pseudo-code command words alongside
other words:

Bess INPUT
END FOR OUTPUT
END IF REPEAT
END WHILE RETURN
FOR THEN
IF WHILE

a
Commenting on your code

§ Commenting on your code


Good code is not only well written, but should also be well
annotated. There are programmers who argue that comments are
A comment is explanatory text for not necessary if the code is written well, but you are undertaking
the human reader. an examination and it will be useful to explain what your code
does and why.
You will find many examples of commented code in this book.
Comments are shown using either // or #. Different programming
languages have different ways to tell the computer that this is a
comment NOT the code. You can make all the code you write in
pseudo-code a comment when you write the actual code using
your chosen language. This is considered good practice when
A Good code is well written and learning to code. You can also comment out bits of code to find
well annotated. errors, but we will explore this later.
A For the examination, you will
Questions in the Edexcel written examination that involve code
need to understand the benefit
will use the following pseudo-code syntax for comments:
of producing programs that
are easy to read, and be able #some text
to use techniques (Such as
comments, descriptive names Multiline comments will show the hash # for each separate
and indentation) to improve
comment line.
readability and to explain how #some text
the code works.
#some more text on a new line
For the examination, you will
need to understand how to Comments remind you and the examiner why you included
Create an algorithm making certain functions. They also make maintenance easier for you
use of programming constructs later.
such as sequence, selection
and iteration. Have you ever tried to work with someone else’s complex
spreadsheet or database? It’s not easy. Now imagine how difficult
it is if you’re looking at someone else’s programming code.
2 Question
When you fully document your code with comment tags, you're
2 What is a comment? answering (at least) three questions:
@ Where is it?
@ Why did I do that?
@ What does this code do?
2 Describe the main reasons No matter how simple, concise and clear your code may end up
why a prograrnmer would wish being, it’s impossible for code to be completely self-documenting.
to annotate or add comments Even with very good code it can only tell the viewer how the
to their code. program works; comments can also say why it works.
=) ee
SPSCUCOrCOCE
3 Pseudo-code

Adding selection
As we discovered in the last chapter on flowcharts, another
important aspect of programming is selection. If we want to write
pseudo-code that tells a user to enter a number to a variable, and
then we want the code to see if the number they entered is a 3 or
a 4 we could write a selection algorithm in pseudo-code that could
look like this:

RECEIVE inoutNumber FROM (int) KEYBOARD #input


IF inoutNumber = 3 #selection (Process)
SEND ‘your number is 3' TO DISPLAY #output
ELSE IF inoutNumber = 4
SEND ‘your number is 4’ TO DISPLAY
eye
SEND 'your number is not 3 or 41 TO DISPLAY __ #the colours are used
#to show the level of
#indentation

END IF

We could also write the code a different way:


RECEIVE inoutNumber FROM (int) KEYBOARD
IF inoutNumber = 3
& The IF statement is used to
THEN SEND ‘your number is 3' TO DISPLAY
create a decision structure,
which allows a program to
ELSE IF inoutNumber = 4
have more than one path of THEN SEND ‘your number is 4' TO DISPLAY
execution. ELSE SEND ‘your number is not 3 or a 4’ TO DISPLAY
A The IF statement causes END IF
One or more statements to
execute only when a Boolean
expression is true.
A An IF-ELSE statement 3 Use this book and other sources such as the internet to research how
will execute one block of to identify and correct errors in algorithms.
statements if its condition is 4 Use this book and other sources such as the internet to research how
true, or another block if its standard algorithms (bubble sort, merge sort, linear search, binary
condition is false. search) work.

ea
Commenting on your code

Chapter review

In this chapter we have explored pseudo-code and ® understand how to determine the correct output of
how to use it to show program flow and decision an algorithm for a given set of data
making.
® understand and be able to use appropriate
We also explored the importance of syntax and how to conventions in pseudo-code
comment on your code.
® understand and be able to explain the benefit of
Remember, before tackling any computer science task producing programs that are easy to read and be
or examination question on this topic you must: able to use techniques such as comments and
® understand and be able to explain what algorithms descriptive names for variables, constants and
are used for subprograms alongside indentation to improve
readability and to explain how the code works
® be able to interpret and write algorithms in pseudo-
code ® be able to use sequencing, selection and iteration
constructs in your programs
® understand and be able to explain and create
algorithms to solve a particular problem, making ® understand and be able to apply structural
use of programming constructs (Such as sequence, components of a program including variable and
selection, iteration) type declarations and sequences, selection,
iteration, data structures and subprograms.
® understand and be able to explain and apply the
purpose of a given algorithm and how an algorithm
works
4 Mathematical skills in computer
science

understand the structural components of


You should: a program (variable and type declarations,
command sequences, selection, iteration, data
1.1.2 understand how to create an algorithm to
structures, subprograms)
solve a particular problem, making use of
2.2.2 be able to use sequencing, selection and
programming constructs (Sequence, selection,
iteration constructs in their programs
iteration) and using appropriate conventions
(flowchart, pseudo-code, written description, 2.5.1 understand the purpose of, and how to use,
draft program code) arithmetic operators (add, subtract, divide,
multiply, modulus, integer division)
1.1.3. understand the purpose of a given algorithm
and how an algorithm works 2.5.2 understand the purpose of, and how to use,
relational operators (equal to, less than,
— 1.1.4 understand how to determine the correct
greater than, not equal to, less than or equal
output of an algorithm for a given set of data
to, greater than or equal to)

The study of algorithms did not start with the invention of


computers, indeed the example we have been using so far in the
book of adding two numbers together is a mathematical problem.
We need to also understand how they work in mathematics. You
must be able to apply mathematical skills relevant to computer
i science. When the term algorithm is used in mathematics, it
4 refers to a set of steps used to solve a mathematical problem. The
algorithm for performing long division or multiplication is a good
example.
Write the pseudo-code where
If you were carrying out a long division for 52 divided by 3, you
the user inputs the dimensions
would have the following specific sequence of steps and their
of a rectangle and the OntCOTEr
posite Lobineriecianee)ie @ How many times does 3 go into 52?
shown on the computer screen. @ The answer is 17 (3 x 17 = 51)
@ How many are left over? The answer is 1
@ How many times does 3 go into 10?
For the examination you will @ The answer is 3 with 1 left over.
need to understand the purpose e And of course the answer becomes 17.3333333

of, and how to use, arithmetic The step-by-step process used to do the long division is called a
operators (add, subtract, divide, long division algorithm.
multiply, modulus, integer
Algorithms are used a lot in mathematics, especially in algebra.
division).
A
4 Mathematical skills in computer science

B Modules
! Key point
A module is asoftware component or part of aprogram that contains
Programmers commonly use one or more routines. Grouping related code into modules makes
a technique known as the code easier to understand and use. We will look at modules in
top-down design to break down more detail later in this book but one of the advantages of modules
an algorithm into functions and is that some of them are already written for you and many relate
modules. to mathematical operations so you will need to understand what
they can do.
A number of very useful modules exist in Python, normally in
a file named <modulename>.py. You can use any Python file as a
module by using an import statement.
The import statement for a module has the following syntax:
import <module name>
Here’s how these statements and functions work.

import <module name> imports the module named, and creates a


reference to that module. After you have done this, say you have
imported module A, you can use A.name to refer to things defined
in the imported module A.
This textbook is divided into chapters and paragraphs. We now
know that in a computer program specific functionality can be
divided up into modules. Programs often use blocks of code and
modules that have already been created in other projects. In a
computer program these modules are parts of a program that we
want to re-use or repeat. Modules often contain functions which
take input values and produce some output. We will explore these
in more detail later.
In pseudo-code you use the following syntax to define a function.
The red brackets < > are only to show where you add something —
you don’t need to put them in your code.
Syntax:
FUNCTION <id> (<add parameter here>, ...)
BEGIN FUNCTION
<add command here>
RETURN <add expression here>
END FUNCTION

Example:
FUNCTION myMarks (Mark1, Mark2, Mark3)
BEGIN FUNCTION
SET Total to (Mark1 + Mark2 + Mark3)/3
RETURN Total
END FUNCTION

Ca
Mathematical algorithms

You can also use <module name> import * to import the module and
then you can simply use a plain name to refer to things defined
In computer science, an object in the module. You can even call the module by coding from
could be a variable, a data <module name> import a, b, c. This imports the module and creates
structure or a function. Each references to the given objects in the module. In other words, you
object has a location in memory can then use a and b and ¢c in your program.
and a value. Objects are often
referenced by an identifier. In Python to import modules we use the following syntax:
import moduleName #imports a module
from moduleName import name #imports part of a module
from moduleName import *

§ Mathematical algorithms
Your knowledge of mathematical algorithms is vital as many
2 Write the pseudo-code to ask computing algorithms follow the same processes but not all are
the user to input a number the same. Let’s look at some examples and how these differ.
and output to the screen
whether it is an odd or even
number. The following instructions are the same But in computer science the meaning
in mathematics. of A = B is different.
A=B Be A In mathematics A = B is comparative,
In mathematics if both A and B = 1 you but in computer science it’s an
could write it as either A= Bor B=A. assignation, meaning you assign B as A.
This is different from B = A which
means you assign A as B.
In mathematics we work with relations. But in computer science, we work with
assignments. We can have:
In computer science signs such The relation B= A+ 1 means that it is Met S)
as = mean different things and true all the time. B=Ac &
A=3
different syntactical rules apply. The relation B = A + 1 is true only after
the second instruction and before the
third one. After the third one B = 6 and
[Ne Gh
The instruction A =A + 3 is false in But in computer science A=A+ 3
mathematics. It cannot exist. means: the new value of A is equal to
3 Use this book and other
the old value plus three.
sources such as the internet The instruction A + 6 = 3 is allowed in But A + 6 = 3 has no meaning in
to research how to use mathematics (it is an equation). computer science — the left side must
arithmetic operators. be a variable so we would have to say
three =A+ 6.

Understanding what can be effectively programmed and executed


by computers, therefore, relies on the understanding of computer
algorithms and mathematics. Algorithms solve general problems, and
not specific ones. Algorithms, therefore, are general computational
methods used for solving particular problem instances.
Be careful with the brackets.
(+ 5)*3

Here, brackets are used to do the addition first.

Output: 33
4 Mathematical skills in computer science

Gar (Sa2)
Brackets here are used to do the multiplication first.
Output: 21
Computers can execute instructions very quickly and reliably
without error, so algorithms and computers are a perfect match!

Translating a simple mathematical problem


into pSeudo-code
Let’s look at a simple problem. Someone has given us a
J Key point temperature of 82° Fahrenheit. But we need a metric temperature.
The word variable in We will call Centigrade and Fahrenheit variables as they could
programming describes a place be any number. In algebra we use letters to represent a value
to store information that can that we haven’t decided on yet. In computer science we call these
change, such as numbers, variables. We will examine variables in much more detail later in
text, lists of numbers and text. the book. For now we will follow the algebra example and call the
Another way of looking at a Fahrenheit temperature ‘F’ and the centigrade temperature ‘C’.
variable is that it’s like a box with The mathematical expression is: C = 5(F — 32)/9
a label on to store something.
If we wanted to write this in English we could say:
GET inputNumber (INTEGER) FROM KEYBOARD _ #input
subtract 32 from F to get O
multiply Q by five to get X
divide X by nine to get C
SEND value of C TO DISPLAY
stop

Or we could create a different variable for each step in the process.


GET F (INTEGER) FROM KEYBOARD
Q =F - 32
WO)
C=x/9
SEND value of C TO DISPLAY
stop

Any number entered as F will be converted to degrees C.

! Key point § Using mathematical phrases and symbols


An operator is a symbol or
To symbolise the arithmetic operators (sometimes called relation
instruction that performs a
operators in computer science as they make a comparison) in
specific function: for example,
programming we use these symbols:
‘NOT’ or ‘AND’.
Using mathematical phrases and symbols

4 Write the pseudo-code where + Addition

the user inputs three numbers Subtraction 8-5 3


and the following is shown
é Multiplication 4*5 20
on the screen: either an ‘all 3
ji Division 19/3 6
equal’ or a ‘not all equal’.
% Remainder 19% 3 1
Reon’ Exponent Dare 16

() Grouping () (isFour = 4) Oorl

In Python we have the following mathematical operators. We will


explore some of these such as Booleans in more detail later in this
book.
@ Add, subtract: +, -
@ Multiply, divide, mod: *, /, %
Seon aisot: 5. =
@ Boolean operators: NOT, AND, OR
e Exponential: **
Operators also have an important order of execution. If we look at
some of the operators from above, this is the order of execution:
@ () equations in parentheses
@ ** exponentiation
@ / and * (division and multiplication)
@ + and - (addition and subtraction)
In mathematics we use what are called real numbers. This is called
a ‘data type’ and again this is something we will cover in more
detail later in the book. Understanding data types is an essential
part of computer science. For now you should understand that
nearly any number you can think of is a real number. They are not
called ‘real’ because they show the value of something real, but
because in mathematics when we write 0.5 we mean exactly half.
But in the real world half may not be so exact as we must work to
what are called tolerances. When you use a calculator you do real
division. The calculator takes a real number, divides it by another
real number and returns a real number.
Real whole numbers are called integers. Whole numbers greater
than zero are called positive integers. Whole numbers less than
zero are called negative integers. The integer zero is neither
positive nor negative, and has no sign. In number theory we work
with integers, so we have what is called integer division. When
you divide two numbers, you have a quotient and a remainder.
10 div 3 = 3 (quotient)
10 mod 3 = 1 (remainder)
3.93 He] acl0
4 Mathematical skills in computer science

The other term you will come across often in programming is


floating-point number. The term floating point is derived from
the fact that there is no fixed number of digits before and after the
decimal point; that is, the decimal point can ‘float’.
Data type Description

str A string of characters, which can include letters, ‘some


numbers, spaces, and symbols. text’

int An integer whole number, which DOES NOT have a 10


decimal point.

float A floating-point number, which DOES have a decimal 10.6


point.
The truth value of a statement is bool A Boolean logical truth value, which is either True or ak
always either true or false. False

In computing we also have to make choices and these are often


based upon mathematical decisions. We can use mathematical
phrases like ‘is less than’ or ‘is more than’.
There is a universally accepted set of symbols used to represent
5 Write the pseudo-code where these phrases:

ee
the user inputs three numbers
and the largest number entered
is shown on the screen. > Greater than

< Less than

SS Greater than or equal to

<= Less than or equal to

= Equal to

<> Less than or more than

I= Not equal to

We'll now look at how these can be paired up.

— Logical operators: AND, OR, NOT


AND: if any of the conditions are false, the whole expression is
false.
IF day = ‘Saturday’
AND noHomework = True THEN #both must be true
OUTPUT 'Let’s go to the park!’
END IF
OR: if any of the conditions are true, the whole expression is true.
IF month = ‘EndJuly'
OR month = ‘August’ THEN #either is true
OUTPUT ‘Great no school!’
END IF

34
Examples of pseudo-code mathematical algorithms

NOT: if the condition is NOT true, the output condition is true, so


2? Question
NOT Sunday will mean no church.
1 What are relational operators? IF NOT day = ‘Sunday’ THEN
OUTPUT 'No church today hombre'
END IF

Using the IF command in Python


The if command means that if a condition is true, then the program
runs a block of commands. If the condition isn’t true, the block
is skipped. In Python the block after the if command is always
indented.
ans = input(‘Is it November 5th? (y/n)')
if ans == 'y’:
print(‘Bonfire night!’)
We can modify this a little by adding an alternative using else:
ans = input(‘ls it November 5th? (y/n)’)
if ans == 'y':
print(‘Bonfire night!’)
orint(‘Time for fireworks.')
else:
print('No fireworks yet!')

Examples of pSeudo-code mathematical


algorithms
Earlier in this chapter we looked at a simple sequential algorithm
to add two numbers together. So let’s say that we want to write
a sequential pseudo-code algorithm that reads two numbers,
multiplies them together and prints out the result — we could
write:
GET num1, num2 #read two inputs
Sled dan ouute a OMalany
kalae lage #process
OUTPUT multi TO DISPLAY — #output

Notice how we have named the two numbers as num1 and num2 and
created a variable called multi to store the result of the calculation.
This is because these are variable as they can be any number. They
are not fixed. Notice also the comments shown after the #.
If we explore mathematical operations to find the sum of 5 entered
numbers in both simple English and a flowchart it may look like
this.
Algorithm — in simple English
1 Initialise sum = 0 and count = 0
2 Enter n (I/O)
> 4 Mathematical skills in computer science

3 Find sum + nand assign it to sum and then increment count by


1 (Process)
4 Is count < 5 (Decision)
if YES go to step 2
else Print sum (I/O)

6 Write the pseudo-code to ask


the user to input ten numbers,
and send the total to the screen.
sum = 0 |
count = 0

sum = sum + 1
count = count + 1

Figure 4.1

Mathematics and Python


Let’s explore other mathematical tasks in Python:
a= 12 #set variable ‘a’
b=/7 #set variable ‘b’
print( Addition: \t', a, /+,,05 = , aso) #output addition
#to screen
print( ‘Subtraction: \t!, a, -,b,'=',a-—b) #output subtraction
#to screen
orint( Multiolication: Wt -a, x,0, = .a ba #output multiplication
#to screen
orint( "Divisions \t®) al, “= b =. anaion #output division
#to screen
print?’ Floor DMSIOn= itary =) = aal// bo) #output floor
#division to screen

=
Probability and randomisation

print( Remainder: \t',a,'%,b,'s',a%b) #output remainder


#to screen
print( ‘Exponent: \t',a,'2?=',a**b,seo='') #output exponent
#to screen

Output:
Addition: 12+7= 19
Subtraction: 12-7 =5
Multiplication: 12x 7 = 84
Division: 1 fea. 142857142857
142
Floor Division: I2=7 = 1
Remainder: 12%7=5
Exponent: 122 = 35831808
Notice how we have used a string to show the conventional
mathematics symbols such as ‘x’.

§ Probability and randomisation


An important mathematical concept that is used extensively in
computer programming is that of randomisation. Probability theory
is an important aspect of randomisation. Imagine tossing a coin —
Randomisation is a function in
there are two possible outcomes, heads (H) or tails (T). If we were
a computer program that allows
however to toss three coins, the possible outcomes are: HHH,
random outcomes from a defined
Piddvnlen ba ait, Ti and TTT so the probabilityos
list or number sequence. It is
any of these is one in eight. The chance of heads or tails is random
useful when creating games.
between two options. If we wanted to code this we would need to
randomise the output to be either 0 or 1.
Random numbers are important in programming. In Python there
is arandom module built specifically for this purpose. Using it is
quite simple.
To generate a random integer you would use:
import random #import the module
? Question #(this does not work with all languages)
1 What will the following num = random.randint[a, b)
pseudo-code do? #this chooses a random integer in the
RANDOWM_INT1(3, 5) #range [a, b) and assigns it to a variable.
#note the use here of two types of
#bracket [ and ).
#the notation [a, b) is used to indicate
#an interval from a to b that is inclusive
#of a but exclusive of b.
#that is, [1, 10) would be the set of
#integer numbers between 1 and 10,
#including 1 but not including 10.
#the range will be inclusive of the
#integer used for a and b.
4 Mathematical skills in computer science

You can use this module to generate an integer and assign it to a


variable called num:
import random
randrangefa, b) #this chooses a random integer in
#the range [a, b).

You can also use this module to generate a floating-point number


! Key point and assign it to a variable called num:
You will be expected to use import random #import the module
random number generation within #select a random number and
num=random.random()
your computer programs. #assign to the variable
print(num) #print the variable random
#number

This code will select a random floating-point number in the range


(0, 1). This means the random number will be between 0 and 1,
including 0.0 but will always be smaller than 1.0.
If we want a larger random number we can multiply it.
For example, for a random number between 0 and 100:
import random
num=random.random() * 100 #we could use randit or we could
#use multiplication
print(num)

In Python you can also generate a random choice. To generate a


random value from a sequence we would use:
random.choice( ['red', ‘black’, 'green'] )

The choice function can also be used for choosing a random


An element in computer science element from a list:
is an essential or characteristic import random
part of something; other terms
myList = [2, 109, False, 10, 'yesterday', 82, 'today']
often used outside computer
science are part, section,
random.choice(myList)
portion, piece and segment. chosen = random.choice(myList)
print(chosen)
We will explore here a few other ways to generate different
random numbers in Python using the random function and
another function in that module called sample. Remember that
the first part of each algorithm will need to import the functions
from the module.

38
Probability and randomisation

To generate a floating-point number:


from random import random, sample #import two functions
#from the ‘random’ module
num = random() #assign a random
#floating-point number to
#a variable called num
print(‘Random Float 0.0-1.0:', num) #output the value ofthe
#random floating-
#point number

Possible output (remember that each time only one random


number will be shown, not a set of them):
Random Float 0.0-1.0 : 0.8444218515250481
The example code below generates a random list of integers in
different ways, first using a list and then using a loop.
from random import random #import two functions
#from the ‘random’ module
num = random() #assign a random
#floating-point number
#to a variable called num
num = int(num * 10) #multiply the floating-
#point number and cast it
#to become an integer
orint(‘Random Integer O-9:', num) — #output the value of the integer

Possible output (remember each time a different set of random


numbers will be shown):
Random Integer 0-9 : 8
To generate a random number list:
from random import random, sample #import two functions from the
#'random’ module
numsList =[];a=0 #now create a list of random
#numbers
while a <6: #add a loop to assign multiple
#random integers to our list
numsList.append(int(random()* 10)+ 1) — #assign multiple
#unique random
#integers to the list
at=1
print('Random Multiple Integers 1-10: ', numsList) #output the list
#of random numbers
numsList = sample(range(1, 99) , 6)
print('Random Integer Sample 1-99 : ' , numsList)
4 Mathematical skills in computer science

Possible output (remember each time a different set of random


numbers will be shown):
Random Multiple Integers 1-10: [8, 5, 3, 6, 9, 8]
Random Integer Sample 1-99 : [39, 62, 46, 75, 28, 65]

Now we’ll explore a simple game using these concepts.


import random #import the random module
num = random.randint(1, 10) #set the random range and
#define as integer
guess = 0 #set guess variable to 0
guesses = 0 #set guesses variable to 0
while guesses < int(5): #while loop to allow 5
#guesses then end loop
print(‘'Guess my number 1-10 :') #output string to screen
guess = input() #set variable guess to user
#input
guesses = guesses + 1 #add 1 to guesses variable
if int(guess) < num: #see if guess is higher than
#number
print(‘Too low’) #to output message to
#screen
elif int(guess) > num : #see if guess is lower than
#the number
print('Too high’) #to send message to
#screen
else: #number must be the same
#as it is not higher or lower
print('Correct... My number is ' + guess) #to output
#message to
#screen
break #stop the while loop

What if someone types a string, e.g. ‘Two’ rather than 2? We


could add a line of code to solve this issue. We will also add a new
variable to stop the loop if a correct answer is given.
import random
num = random.randint(1, 10)
flag = True #we are adding a new
#variable
guess = 0
guesses = 0 #set guesses variable to 0
print((Guess My Number 1-10: ', end ='')
while flag == True: #if True run loop if not stop

40
Probability and randomisation

guess = input()
guesses = guesses + 1 #add 1 to guesses variable
if not guess.isdigit() : #checks it is not a string
print(‘Invalid! Enter only digits 1-10') #output string
#to screen
break #stop the while loop
elif int(guess) < num:
print('Too low, try again: ', end= '')
elif int(guess) > num:
print('Too high,try again: ', end ='')
else ¢
print(‘Correct... My number is ' + guess)
flag = False #makes flag False to stop
#the while loop

Chapter review hand


Building upon the last two chapters, in this chapter we ® Understand and be able to explain the purpose of a
have explored computational mathematics. given algorithm and how an algorithm works (More
We looked at the differences between mathematics on this topic in Chapters 2, 3, 6, 7, 8, 14, 5, 11, 10)
and computational mathematics. We also added to
your knowledge of both flowcharts and pseudo-code. ® Understand how to determine the correct output of
an algorithm for a given set of data (More on this
Building upon the use of selection in decision making,
topic in Chapters 2, 3, 6)
we explored logical operators.
® Understand the structural components of a
Remember, before tackling any computer science task
program (variable and type declarations, command
or examination question on operators you must:
sequences, selection, iteration, data structures,
® Understand and be able to explain the purpose subprograms) (More on this topic in Chapter 3)
of, and how to use, arithmetic operators such
® Be able to use sequencing, selection and iteration
as addition, subtraction, division, multiplication,
constructs in your programs (More on this topic in
modulus, and integer division
Chapters 3, 6, 7, 8, 14, 5, 11, 10)
® Understand and be able to create an algorithm
® Understand and be able to explain and apply
to solve a particular problem, making use of
structural components of a program (variable and
programming constructs (Sequence, selection,
type declarations, command sequences, selection,
iteration) and using appropriate conventions
iteration, data structures, subprograms) (More on
(flowchart, pseudo-code, written description, draft
this topic in Chapter 3).
program code) (More on this topic in Chapters 2, 3,
Opio, 145-.11,10)
5 Loops and mathematical operations

+ You should:
a | 1.1.1 understand what an algorithm is, what 1.1.6 understand how to code an algorithm in a ©
algorithms are used for and be able to interpret high-level language
algorithms (flowcharts, pseudo-code, written 1.1.7 understand how the choice of algorithm is
descriptions, program code) influenced by the data structures and data
/ 1.1.2 understand how to create an algorithm to values that need to be manipulated
solve a particular problem, making use of 2.2.2 be able to use sequencing, selection and
programming constructs (sequence, selection, iteration constructs in their programs
iteration) and using appropriate conventions 9.3.3 understand the need for, and how to
(flowchart, pseudo-code, written description,
manipulate, strings
draft program code)
2.4.1 understand how to write code that accepts and
-) 1.1.3 understand the purpose of a given algorithm
responds appropriately to user input
: and how an algorithm works
1.1.4 understand how to determine the correct
: output of an algorithm for a given set of data

JKey polnt We often use loops with procedures for mathematical operations.

A loop is part of a program that If we now compare a WHILE loop with a procedure in flowchart,
repeats itself (to prevent the pseudo-code and Python form they would look like this:
need for the same piece of code
to be typed out multiple times).
Teele cede
WHILE x <5 while x < 5:
DO PROCEDURE() do_procedure()
ENDWHILE :

1 Write the pseudo-code to


output the numbers 1 to 50 do_procedure
inclusive using a while loop.

42
: Minimum and maximum
SS
SSS SSS sls sss SSeS SS

A REPEAT loop with a procedure in flowchart, pseudo-code and


Python form would look like this:

REPEAT while True:


DO PROCEDURE() do_procedure()
UNTILx <5 ifxe<r:
break

Python has no REPEAT LOOP so a


similar effect can be achieved by using
do_procedure a WHILE loop. We could also recode
| this code to avoid the use of a break
by adding a condition that is met to
exit the loop. Using ‘break’ in code
frequently makes the code hard to
follow, so often structured programmers
[re]write programs without ‘break’
statements. Care needs to be taken
however as sometimes replacing them
with the introduction of additional
variables and/or code duplication
can also make the program harder to
understand.
2 Write the pseudo-code to
output the numbers 1 to 60
inclusive using a for loop. And a FOR loop condition in flowchart, pseudo-code and Python
form looks like this:

Flowchart

FOR count=1T05 for count in range(1,6):


DO PROCEDURE() print (‘x’)
END FOR

do_procedure| |

- Count =
Count + 1

§ Minimum and maximum


Sometimes a user wants to find the minimum and maximum.

We will now explore a small program that will allow a user to


type ina list of numbers and from this list calculate the minimum
and maximum. To make the algorithm a little simpler we will ask
the user to enter a count of the numbers in the list before entering
the actual numbers.
5 Loops and mathematical operations

The algorithm for this might be:


! Key point
get count of numbers
Sequencing is placing
enter numbers and find maximum and minimum
information, concepts and
actions in order. Steps are
output results
followed to lead to the same Of course the user may enter any number of numbers, even no
result, like following a recipe. numbers. This may seem a bit stupid, but it is not uncommon when
writing a general-purpose function to allow for the possibility of
no inputs (referred to as ‘null input’). We will explore functions
in more detail later in the book but basically a function is a
subprogram not a program and all functions return a value. For
3 Write the pseudo-code to ask now let’s modify our algorithm to accept this.
the user to input two values
GET count of numbers
into variables for start and
IF count of numbers is zero
finish, then send to the screen
the integers from start to finish then exit
inclusive using a for loop. ESE:
ENTER numbers and find maximum and minimum
OUTPUT results

In our new algorithm, once the count of the numbers is known


a loop is executed that number of times, each time reading in a
number and somehow using that number to find the maximum
and minimum.
In this loop the number of times the loop is executed is known,
i.e. equal to the count of numbers.
loop n times
<body of loop>
Algorithms often repeat a set of instructions, a set number of
times. Note that the body of the loop is a compound statement.
So we have the following new version of the algorithm:
INITIALISE
GET count of numbers.
IF count of numbers is zero
then EXIT
EESE
Iteration (looping) is repeating LOOP count times
parts of a sequence over and ENTER a number
over again, like eating breakfast
PROCESS the number
every day at 7.30 a.m.
OUTPUT results
We now need to compute the maximum and minimum values.
If you wanted to do this manually, one way of doing it would be to start

on,
at the beginning of the list and work through the list systematically,
4
Eee...Mathematical arrays a
<a je

always remembering the smallest number seen so far — whenever a


number is found smaller than the memorised number the memorised
4 Write the pseudo-code to number would be replaced by the new smaller number. At the start
ask the user to input two of the list the smallest number yet seen is of course the first number,
values into variables for start and when the end of the list is reached the memorised number is the
and finish, then send to the smallest. We could do a similar thing for the largest number.
screen all the integers from
Start to finish inclusive using The algorithm would not work if the user is asked for the min/max
a for loop, but if the start is of just one number. Hence the following:
bigger than finish, swap over GET count of numbers
their values so they are now IF count of numbers is zero
in the proper order, then send then EXIT
to screen the integers as
EESE
specified.
GET first number
SET small to number
SET large to number
LOOP count-1 times
ENTER a number
IF number is less than small
5 use this book and other
then SET small to number
sources such as the internet
to research the key functions IF number is greater than large
of your chosen high-level then SET large to number
programming language. PRINT small and large

§ Mathematical arrays
When we want to use loops to work out mathematics we often
store the numbers in an array. We will explore these again in more
An array is a collection of data detail later in the book, but for now we will explore the concept in
items that are given a single mathematics.
name and saved on a computer
system in a sequential form. The You may have used arrays asachild when you learnt your multiplication
data items are distinguished by tables. By progressively adding another column of three objects, you
numbers. can visualise basic number facts in a graphical way.

6 Use this book and other


lexxs 3:%3 TANS oes
sources such as the internet to
or or or or
research the pre-existing array Spe || 3x3 Sed Brees
functions (built-in, library) for
Figure 5.1 Arrays of dots
your chosen language.
We have an array of dots. Each column has three dots. So two
columns would be six dots, five columns fifteen dots.

This is quite a difficult array as it actually has two dimensions


with rows and columns. But we also have what are called
5 Loops and mathematical operations

one-dimensional arrays. You can think of arrays as boxes that


hold a number — you are allowed to look at what’s in the box or
replace its contents with something else (see Figure 5.2)
-myanray=
Figure 5.2 If myArray is an array of length n, you can imagine it has n boxes
lined up in a row.
It should be noted that array indexing can start at 0 or 1 depending
upon the programming language. Python, by default, uses lists
instead of arrays with indexing starting at zero.
You can use arrays in the same way you use variables:
x = myArray[2] #we are using an index of 1
#therefore this
#sets x to the second value in the array (here, 6)
#(third value in Python)
myArray[3] = 2 #sets the third value in the array A to the
#value 2 (replacing 9)
# (fourth value in Python)

Sometimes you will use a variable to specify which element of the


array you mean:
y — myArray[i] #sets y to the i’th array value

Suppose we have the following array:


myArray = (2,4,6,8)

The array shows the 2 times table. If we go to the third indexed


number we will have 2 x 3; the fourth indexed number will be 2 x 4.

myArray = (3,6,9,12,15)

would represent the three times table.


In pseudo-code you use the following syntax for a count-
controlled loop using an array. The loop executes for each element
of the array. The red brackets < > are only to show where you add
something — you don’t need to put them in your code.
Syntax:
FOR EACH <id> FROM <expression> DO
<command>
END FOR EACH
Example:
SET WordsArray TO ['The’, ‘Sky’, 'is', ‘grey'] SET Sentence to ''
FOR EACH Word FROM WordsUArray DO
SET Sentence TO Sentence & Word &''

ear
END FOR EACH
Pe
Finding the mathematical mean Se
eee
ee

§ Finding the mathematical mean ¥


If we have N numbers and want to find their arithmetic mean, the
simplest method is to put the numbers into an array then sum all
values and divide by the number of values:
def simple_mean(array[N)): #pseudo-code
stim
= 0.
1OfF in=eletosN
sum += array|[i]
return sum /N

Another method but in Python would be:


SU ine".
resi — 0
for num in numbers:
sum += num/N
rest += num %N
sum += rest/N
rest = rest % N
return sum, rest

Chapter review ® Understand and be able to explain the purpose of a


given algorithm and how an algorithm works (More
In this chapter we have built upon your extended on this topic in Chapters 2, 3, 4, 6, 7, 8, 14, 11, 10)
knowledge of loops from the last few chapters
® Understand and be able to explain how to code an
and also integrated knowledge of computational
algorithm in a high-level language (More on this
mathematics.
topic in Chapters 7, 6, 8, 14, 11, 10)
Remember, before tackling any computer science task
or examination question on this topic you must: ® Understand and be able to explain how the choice
of algorithm is influenced by the data structures
® Understand and be able to explain what an and data values that need to be manipulated (More
algorithm is, what algorithms are used for and be on this topic in Chapters 18, 6, 7, 8, 14, 11, 10)
able to interpret algorithms (flowcharts, pseudo-
® Be able to use sequencing, selection and iteration
code, written descriptions, program code) (More on
constructs in your programs (More on this topic in
this topic in Chapters 1, 2, 3, 6, 7, 8, 14, 11, 10)
Chapters 3, 4, 6, 7, 8, 14, 11, 10)
® Understand and be able to create an algorithm
® Understand and be able to explain the need for,
to solve a particular problem, making use of
and how to manipulate, strings (More on this topic
programming constructs (Sequence, selection,
in Chapters 6, 7, 8, 14, 5, 11, 10
iteration) and using appropriate conventions
(flowchart, pseudo-code, written description, draft ® Understand and be able to write code that accepts
program code) (More on this topic in Chapters 2, 3, and responds appropriately to user input (More on
ANG, f, 8, 14,11, 10) this topic in Chapters 2, 3, 6, 7, 8, 14, 11, 20).
©) You should:
» 1.1.1 understand what an algorithm is, what understand how the choice of algorithm is
algorithms are used for and be able to interpret influenced by the data structures and data
algorithms (flowcharts, pseudo-code, written values that need to be manipulated
descriptions, program code) understand the benefit of producing
understand how to create an algorithm to programs that are easy to read and
solve a particular problem, making use of be able to use techniques (comments,
programming constructs (sequence, selection, descriptive names (variables, constants,
iteration) and using appropriate conventions subprograms), indentation) to improve
(flowchart, pSeudo-code, written description, readability and to explain how the
draft program code) code works
understand the purpose of a given algorithm be able to use sequencing, selection and
and how an algorithm works iteration constructs in their programs
understand how to determine the correct understand the need for, and how to
output of an algorithm for a given set of data manipulate, strings
understand how to code an algorithm in a understand how to write code that accepts
high-level language and responds appropriately to user input

§j Variables
At the core of any program are variables. Variables are where all
the dynamic information is stored.
When you use a keyboard to add your name to a running program,
your name would almost certainly be stored as the value of a
variable. Not all variables are the same though. In fact, there are
many different types. We have used the term variables before
for letters (as used in algebra) and for the temperatures in the
first section of the book. We used F (for Fahrenheit) and C (for
Centigrade).
In this section we explore variables in more detail. The ability to
Aelseaniepookarakerne: operate on different values each time a program is executed is
sources such as the internet to | iValuable in coded solutions. This is provided by variables
. If you
research how to use variables
want a user to input somethi ng to the system like a number that
and constants.
will change due to a calculati on, this will need to be stored as a
variable. Imagine variables as boxes. On the outside of the box is
What are variables?

the variable name. Inside the box is the variable. You can change
and add to what is in the box but you can’t change the box name.

§ What are variables?


Our variables are like labelled boxes that store different types of
] Key points.
data. We will be exploring variables throughout the book, but the
A In pseudo-code a computer types we will look at here are numbers, strings and Booleans.
can assign a value to a
Strings: A string variable is a sequence of alphanumeric characters and
variable using = to assign
allowed symbols that are contained within quotation marks. ‘Hello
VALUE to VARIABLE (memory
world’ is an example of a string. Strings can also be contained within
location).
single quotes. Strings are basically boxes used for storing text.
4 For the examination you will
need to understand the need Numbers: A number variable only stores numbers. You don’t store
for, and how to use, data types them within quotes like strings.
(integer, real, Boolean, char). Booleans: We explored Booleans a little in the last two chapters. A
A A variable name is used to Boolean variable is always one of two things: True or False. It is a bit
identify a value in memory like an on and off switch where you can ask questions. For example,
that can changed during the you might ask ‘is the TV on?’ If you did the response you'd get would
execution of the program. be a Boolean variable. True would mean the TV is on and False
A Alphanumeric refers to would mean it is not on. We will explore Booleans more in later
alphabetic and numeric chapters, but you need to understand the concept in order to
characters, and is used understand selection. We may want to say IF the TV is off, switch it
to describe the collection on. IF statements are probably the easiest type of logic statement to
of Latin letters and Arabic understand. An IF statement is basically a statement that says IF the
numeric digits. specified condition is true, then run this block of code. It can also be
used to say IF the first condition isn’t met, do this instead.

A variable can be assigned different values during a program’s


? Question execution — hence, the name ‘variable’. Wherever a variable
1 What will the following appears in a program it is the value associated with the variable
that is used, and not the variable’s name.
pseudo-code evaluate to?
‘computer’ + ‘science’ Be careful as you cannot mix the data types in your box. It is not
possible to add a number to a string. We will explore this later in
the book, but as this is a very common error in coding it is worth
mentioning here as well.
As a human, you know the difference between a number and a
There are different types of errors,
string, so if asked to add:
or bugs, that prevent computer
programs from working correctly. ‘One ils 2
Three of the key error types are
you will mentally convert the string ‘one’ into the number 1. A
runtime, syntax and semantic.
computer cannot do this so you will get an error if you code it this way.

§ Variable values
Variables are assigned values by use of whatis called an assignment
operator which is usually = or <— depending on the language used.
In pseudo-code SET may be used to assign a variable.
6 Variables and constants

You use the following syntax to assign a value to a variable. The


red brackets < > are only to show where you add something — you
don’t need to put them in your code.
Syntax:
SET Variable TO <add value here>

Examples:
SET Counter TO 0

! Key point and

A variable can be assigned SET myString TO ‘Hello world’


different values during a program’s
execution - hence, the name You are ‘setting’ a variable value. In pseudo-code you use the SET
‘variable’.
X TO syntax, but many languages use the = sign.

§ Assignment and equality


So far we have looked at assignment. There is a big difference
] Key point between assigning and testing for equality. Consider the following:
The assignment operator (=) and myVariable = 6;
the equality operator (==) appear
myVariable == 6;
to be similar, but are used for
completely different reasons. The Can you tell the difference?
assignment operator (=) is used
to assign a value to a variable, The first is known as assignment. It means assign the value of
element or array; the equality myVariable to 6.
operator (==) is used to compare The second statement is one of equality. It’s a test that means ‘is
two values or expressions. The myVariable equal to 6?’ The answer you would get would be true or
result is TRUE if the expressions false. Notice the difference: == is used as the syntax.
are equal or FALSE if they are not.
@ Declaring variables and constants
All variables and constants have to be declared. Declaring a
variable or constant simply means defining its type and initialising
it by setting an initial value (called initialising the variable). The
type allows the compiler to interpret statements correctly.
Variables do not have to be initialised (assigned a value) when
they are declared, but it is often useful to do this.
In Python you do not need to declare variables before using them,
or declare their type. Every variable in Python is an object. But it
is always good coding practice to check that a variable has valid
data in it before it is accessed for some other purpose.
A constant declaration specifies the name, data type and value of
the constant. A constant holds a value that does not change. You
cannot declare a constant in Python. Some Python programmers
use names for variables that are all-uppercase to show that the

EN
variable is intended to be a constant.
Inputs to variables

Inputs to variables :
The variable may get its value within the program, for example
Shen Meaeiaay ROA

The variable num now equals 11. But what if the program states:
SET num TO num + 1

Mathematically, num = num + 1 does not make any sense. In


computing it is used to increment the value of a given variable by
one. Each time the code runs num will be the old num plus 1.
In pseudo-code you use the following syntax to compute the value
of an expression and assign to a variable. The red brackets < > are
only to show where you add something — you don’t need to put
them in your code.
Syntax:
SET Variable TO <add expression here>
Examples:
SEIsSum [© Score + 10

and
SET Size to LENGTH(Word)

As we have seen this can change as the program runs:


SE nuny (©) numa #assign adds 1 to variable num

but there are other ways a variable can be assigned and changed,
not least through user input. When a user inputs data into the
system it is usually stored in a variable.

§ Creating a string variable in Python


As you now know a string might include letters, numbers, symbols
or spaces. Strings can be placed in variables.
a = ‘Smile! ' #define variable ‘a’ with a string
b = 'The sun is out. ' #define variable ‘b’ with a string
c=a+b #define variable ‘c’
print(c) #output to screen
Output: Smile! The sun is out.
c=b+'Itis Saturday.’ +a #adding an additional string to ‘c’
orint(c)
Output: The sun is out. It is Saturday. Smile!
What if we wanted to show the string ‘It is Saturday’ as ‘It’s
Saturday.’ The apostrophe in ‘It’s’ would cause us a problem as the
2 Use this book and other string will have an extra apostrophe. We overcome this problem
sources such as the internet by using a ‘\’ before it. This is called ‘escaping’.
to research how to manipulate c=b+ ‘It\'s Saturday. ' +a
strings.
print(c)
Output: Smile! It’s Saturday. The sun is out.
The input() function is used to accept input from the keyboard into
a program. It waits until the user finishes typing and presses the
‘return’ or ‘Enter’ key.
name = input('Enter your name: ')
print(‘Hello', name)

You will know from earlier sections in the book that two variables
! Key point can be compared using the == operator or the != operator. Strings
To compare is to examine (two have to match exactly to get a ‘True’ output. Let’s look at an
Or more strings, etc.) in orderto example in Python:
note similarities and differences. subject = ‘Computer Science is Wonderful’ — #define the
#variable subject
with a string
if subject == ‘Computer Science is Wonderful’: #compare our
#variable with a
#string
a = 'TRUE' #if they match set ‘a’ to TRUE
print (a) #now output ‘a’ to screen

Output will be:


True #as they match

String manipulation
As you now know a string is simply a list of characters in order,
where a character can be anything a user can type on the keyboard
in a single keystroke.
You will also know that an empty string is a string that has 0 characters.
Python recognises as strings everything that is delimited by
quotation marks (either ’ ’ or ‘ ’).
We can also manipulate strings using some of Python’s built-in
methods. For example we can changing upper and lower case strings:
string = 'Hello World'
print(string.upper())
Output: HELLO WORLD
string = ‘Hello World'

cap)
print(string.lower())
Output: hello world
Ce
TCT Getting inputs

string = ‘Hello World’


print(string.capitalize())
Output: Hello world
string = 'Hello World’
print(string.swapcase())
Output: hELLO wORLD
We can even reverse a string:
string = 'Hello World'
print(’ "join(reversed(string)))
Output: dlroWolleH
We will explore strings further in later sections of the book.

§ Getting inputs
Inputs can come from a number of sources including the keyboard,
touch screens, games controllers, barcode readers etc.

Often a program will have data for the variables from both within
the code and user input.
In the example below, the user input is for the route and the
computer setting of a variable for the count.
Think back to our map in the first chapter but now imagine it as a
computer game screen with the user having to input the movement
from C10 to the finish at L15. User input will determine the route.
But at H12 there is a banana that is worth points.

Bre Curt) gee came teem tte Slt re), Ke. LM


Figure 6.1 Computer game screen

The user navigates the man from C10 to L15 using pathway C. On
the way a variable called banana will increase by 1.
SET banana TO banana + 1
es

$ * 6 Variables and constants


a

iy
Fn
te

[Const es
jsits
ote an :*
than a variable.
Sometimes you use a constant in your code rather
You would use a constant instead of a variable if you know that a
value should not be changed when the program is running.
Using a constant eliminates the risk that the value will be changed
somewhere in the program code.
You should always use constants instead of typing values into
other places in your program code as this makes the code easier
to understand and easier to update in the future.
Note: You cannot declare a constant in Python. Some programmers
use names for the variables that are all-uppercase to show that
the variable is intended to be a constant.

Choosing names for variables


A Often programmers get errors and constants
because they use slang
The name should be as descriptive as possible.
variable names, e.g. ‘paws’
instead of ‘pause’, and then Dont use generic names for variables and constants. Adding
use the wrong name later in descriptive names improves the overall quality of the code. It
the code. makes it much easier to modify and read the code. It is good
A Golden rule: Spend a few practice to give names that describe their purpose.
minutes thinking about your The computer stores variables and constants in an assigned
variable names before you use memory location but the programmer does not need to know which
them in your code. memory location will be used to store it. The programming
A The computer memory is made language software and operating system take care of this.
up of partitions (much like a
If you find yourself writing names as soon as you need them in
set of drawers). The memory
your code without even thinking about the name for a second,
location is the address which
you are probably picking bad names. Similarly if you revisit your
uniquely identifies every
code and can’t understand what the purpose of a variable is
location in the memory.
from its name, it’s a bad name. Normally variables have names
A You must know why named written like this: myVariable. Notice the use of lower and upper
constants and variables are
case letters. This naming convention is often called camel case
used.
naming because of the hump in the middle. Whilst camel case
4 For the examination you will is often used, many coders prefer capitalised camel case, others
need to understand the need prefer underscores. Consistency is the important factor. Different
for, and how to use, variables languages also have different rules and conventions.
and constants.
Should data be stored in a variable as
character or numeric?
Sometimes an inexperienced programmer will write code with
all of the variables defined as strings. The variables may contain
numeric values, but if they are defined as a type string, there will

a
be very few things you can do later to analyse the data.
Should data be stored in a variable as character or numeric?

If a variable contains non-numeric information (e.g. names)


then it clearly should be saved as a character string. But if the
variable contains real numeric data which will be used in numeric
calculations, such as weight or height, then it should be stored as
a numeric variable.

It gets more complex when you want to store something like a


gender, for example male or female. It is actually better to store
this as a numeric variable with an appropriate format, rather
than a character variable with, for example, values ‘M’ and ‘FP’.
e Questions
Numeric data is more efficient and gives you more processing and
2 What is a constant? data analysis possibilities, therefore it is better to store M and F
3 Describe the main reasons numerically as 0 and 1. As a general rule it is always preferable to
why it is important to choose save it as a variable of type numeric rather than a variable of type
descriptive names for variables. character, even if you have no intention of performing algebraic
calculations using the variable.

Chapter review
® Understand and be able to determine the correct
In this chapter we have explored variables and output of an algorithm for a given set of data (More
assignment. We also explored the importance of on this topic in Chapters 2, 3, 4,)
correctly naming your variables. ® Understand and be able to code an algorithm
Remember, before tackling any computer science task in a high-level language (More on this topic in
or examination question on this topic you must: Chapters 7, 8, 14, 5, 11, 10)
® Understand and be able to explain what an ® Understand and be able to explain how the choice
algorithm is, what algorithms are used for and be of algorithm is influenced by the data structures
able to interpret algorithms (flowcharts, pseudo- and data values that need to be manipulated
code, written descriptions, program code) (More on (More on this topic in Chapters 7, 8, 14, 5, 11,
this topic in Chapters 1, 2, 3, 7, 8, 14, 5, 11, 10) 10, 18)
® Understand and be able to create an algorithm ® Understand and be able to explain the benefit of
to solve a particular problem, making use of producing programs that are easy to read and be
programming constructs (Sequence, selection, able to use techniques (comments, descriptive
iteration) and using appropriate conventions names (variables, constants, subprograms),
(flowchart, pseudo-code, written description, draft indentation) to improve readability and to explain
program code) (More on this topic in Chapters 2, 3, how the code works (More on this topic in
Oe fe 6214 851 1°40) Chapter 3)
® Understand and be able to explain the purpose of a ® Be able to use sequencing, selection and iteration
given algorithm and how an algorithm works (More constructs in your programs (More on this topic in
on this topic in Chapters 2, 3, 4, 7, 8, 14, 5, 11, 10) Chapters 3, 4, 7, 8, 14, 5, 11, 10).
7 Programming in code

B Specification references
o You should: .1.7 understand how the choice of algorithm is
c | 1.1.1 understand what an algorithm is, what influenced by the data structures and data
algorithms are used for and be able to interpret values that need to be manipulated
algorithms (flowcharts, pseudo-code, written 2.1.1 be able to write programs in a high-level
descriptions, program code) programming language
understand how to create an algorithm to .1.2 understand the benefit of producing
solve a particular problem, making use of programs that are easy to read and be able
programming constructs (Sequence, selection, to use techniques (comments, descriptive
iteration) and using appropriate conventions names (variables, constants, subprograms),
(flowchart, pseudo-code, written description, indentation) to improve readability and to
draft program code) explain how the code works
understand the purpose of a given algorithm .2.2 be able to use Sequencing, selection and
and how an algorithm works iteration constructs in their programs
understand how to determine the correct .3.3 understand the need for, and how to
output of an algorithm for a given set of data manipulate, strings
understand how to code an algorithm in a -4.1 understand how to write code that accepts and
high-level language responds appropriately to user input

In this chapter we will look at programming in code, focusing


primarily upon Python. Everything in Python is implemented as
an object. A very simple view of an object would be a box that
] contains a piece of data. Students who go on to cover Object
e Key point Orientated Programming will see that in OOP, objects can contain
For the examination you will need other things as well as data. We used the idea of boxes in the last
to underetane hew to code an chapter when we looked at variables. The box also has a type,
algorithm in a high-level language. such as Boolean or integer. This determines what can be done
with the data in the box.
The type of box also determines if the data value contained by the
box can be changed, which is called mutable in programming,
! Key point or if it is the opposite and can’t be changed, which is called
immutable.
An immutable object is an object
whose state cannot be modified In an immutable object box you can see the value but you can’t
after it is created. change it. In a mutable object box, whilst you can’t change its
type you can see the value inside and can also change it.

<7
Strings in Python

Start up your Python editor and save the following into a file and
save your file as HelloWorld.py
print(‘Hello, World!')

You have just produced your first piece of working code.

] Key points
4 All the letters, numbers, and symbols in this book could be a string.
Think of a string as a collection of letters and/or numbers which are
stored as text not numbers.
4 We create a string by putting quotes around text so that the
programming language can tell it is a string not a number or
something else.
A No matter which programming language you choose, coding takes
time and it’s really a lot of trial and error.

§ Strings in Python
Now we understand how to develop code and use variables we
need to explore aspects of code that can change as a program runs.

! Key points Let’s say we want a user to input a string. You will know from
the last chapter that a string is simply a sequence of letters and
A A string is a data type used numbers. This sentence is a string.
to store a sequence of
characters. In Python, the input function is used for this purpose:
A Often programmers get errors name = input('What is your name?’) #‘What is your name’
because they write functions #is a string

that are too big. Big functions Strings are always within quotes. This particular use of a string,
_ are hard to debug because it for requesting input from the user, is called a prompt. Not only is
is hard to understand exactly the bit between quotes a string, so is the variable it is stored into,
what they’re trying to do. name, which has no quotes.
To create string (s) in Python we can use:
s = "Hello World!" or
s = 'Hello World!’
Write a short piece of code in
The input function displays the string on the screen to prompt the
your chosen language that will
user to put in an input of their name.
store a user input as a string.
In most cases strings must be contained all on one line.
A string may contain zero or more characters, including letters,
digits, special characters and blanks. A string consisting of only a
For the examination you will pair of matching quotes (with nothing in between) is called an
need to understand how to write empty string.
code that accepts and responds
Numbers can also be stored as strings, but be careful as when
appropriately to user input.
they are strings they are NOT NUMBERS.
7 Programming
eee
es eee in code
: Ls
q

Can you spot the difference between the two rows of values
i Key points shown below?
A A string may contain zero or
BE 45 98 3
more characters, including
letters, digits, special 22 45 98 S
characters, and blanks.
This is actually a trick question because (visually) there is no
A Numbers can be stored as
difference. The difference is how the values in the bottom row
strings but then they are no
are stored. They are stored as a text string, not a number. This is
longer numbers.
a very common mistake in programming and leads to problems
if you want to carry out mathematical operations later in the
code.

String manipulation in Python


We now know that a string is a list of characters in order. We also
know that a character is anything you can type on the keyboard
User input comes into Python in one keystroke, like a letter, a number, a space or a backslash.
as a string, which means that An empty string is a string that has 0 characters.
when you type the number 10 on
Python recognises as strings everything that is delimited by
your keyboard, Python saves the
quotation marks (“” or ‘’).
number 10 into a variable as a
string, not a number. You can manipulate strings in Python, with Python’s built-in
methods.
If you examine the first bit of Python code in more detail:
myString = 'Hello World’ #set the string
print(myString) #show string on screen
You would see on the screen: Hello World
If you want to access just part of the string you can use [ ] to
access characters in a string:
myString = 'Hello World’
letter = myString[0]
print(letter)

You will print on the screen:


H #Python has an index starting at 0 unlike pseudo-code index
#1, we will explore this further in later chapters

In Python, to access the fifth character ina string we would use:


myString = 'Hello World'
letter= myString[4]
print(letter)
Which would display the letter: 0
In Python, to concatenate (which is to join) two strings (string s4
and string $2), we write:

ane
s1 +52
Strings in Python

In Python, length (len) always returns the number of characters in


string (s):
n = len(s)

Example:
myString = ‘Hello World’
print(len(myString))
will print on the screen: 11
You can also find letters in a string:
myString = 'Hello World'
print(myString.count('!’)) #count how many times | is in the
#string myString

will print on the screen: 3


In Python, to split string (s) you would use:
s = ‘Hello World’
Ssolit(: |)
s.split('r')

Strings often store many pieces of data. These are often stored in
a comma-separated format, which simply means that each part is
divided with commas. A space is another common delimiter, so in
‘Hello World’ the two words in our string are delimitated by a space.
#input string
myString = ‘London ,Bristol ,Birmingham ,Liverpool'
#delimitated using commas
cityNames = myString.split(’,’)
#loop and print each city name
for city in cityNames:
print(city)
Output:
London
Bristol
Birmingham
Liverpool
In programming the best way to
learn is by doing, programming So with split we extract the parts separated by the delimiter. Often
lines must be split. This is done with readlines() and with split. The
by ‘trial and error’. So, expect to
split() method with a string argument separates strings based on
make mistakes, they will help you
the specified delimiter, but with no arguments split() separates
to become a better programmer.
strings using one or more spaces as the delimiter.
7 Programming in code
eee

! Key point
§ Variables |
Variables are a storage As you will know from the last chapter, a variable is a memory
placeholder for texts and location that is used to store a value that can be changed. Variables
numbers. A placeholder is a are identified by names.
term referring to something not
In Python, variables are storage placeholders for texts and numbers.
yet finalised. It is a section of
Variables in Python are assigned with the equals sign, followed by
computer storage reserved for
the value of the variable. But be careful as there are some reserved
information that will be provided
words for Python that cannot be used as variable names.
later.
Variable names are case sensitive but you can use any letter, the
special character _ and numbers provided you do not start names
with them.
The value of the variable can be changed later on.
To store the value 10 in a variable named myVariable we would use:
myVariable = 10
To store the value of myVariable + 10 in a variable named myExtended:
myExtended = myVariable + 10
There are many different variable types in Python including ones
Naming variables with a capital
you should now understand:
letter used for the first letter of
the second word is called camel myVariable = ‘hello’ #string
case because the uppercase myVariable = 123
characters that appear in the
and new ones we will explore later in the book:
name suggest a camel’s hump.
myVariable = 3.14 #double float
myVariable = [0,1,2] #list
myVariable = (0,1,2) #tuple
myVariable = open(‘hello.py’, 'r') #file
You can even assign a single value to several variables at the same
time using multiple assignments.
a= b= Can #variables a, b and c are assigned to the
#same memory location, with the value of 1

Example code using variables:


It is possible to use a word myLength = 2
processor to create a Python myWidth = 4
program, but you must be sure myArea = myLength * myWidth
to save the program as a plain print('The area is: ', myArea)
text file.
will print out: The area is: 8

an
Indentation

Size of variables
We have looked at variables as being boxes that can hold
something. The variable name is the label on the box. Not all
variables take up the same amount of memory. These can vary
according to the programming language used.

a Indentation
Indenting is adding spaces/tabs in front of ‘blocks’ of code (such
as IF), so that it is easier for you and other people to see how
the code flows. It shows which parts of the code will run under
certain situations.

Why should you indent your code?


There are several benefits to indenting your code, including:
Python expects the number @ A user can see at a glance where the end of a code block is
of spaces in a block to be rather than having to read each line until they find it.
consistent. It doesn’t matter how @ A user can immediately ignore chunks of the code that aren't
many spaces you insert, as long relevant to what they are currently doing.
as you use the same number for @ It is much easier to notice if a line of code is in the wrong place.
every new line.
In Python you are forced to indent parts of the code, but it is good
practice to do this in any coded solution even when writing in
2 Question pseudo-code.

1 What is a byte? Is whitespace more significant in Python


source code?
] Key points Only the indentation level of your statements is significant (i.e.
the whitespace at the very left of your statements). Everywhere
A Whitespace is any section that else, whitespace is not significant and can be used as you like, just
is unused, for example the like in any other language.
space around an item in the
You can also insert empty lines that contain nothing (or only
code.
arbitrary whitespace) anywhere.
A Whitespace is a useful tool
in any coded solution as it The exact amount of indentation doesn’t matter either, only the
helps to separate the blocks of relative indentation of nested blocks (relative to each other).
code.
§ Adding comments to Python code
There are a number of ways to add comments to Python code.
As with the pseudo-code used throughout this book, single-line
comments begin with the hash character (#) and are terminated
by the end of line. Python just ignores all text that comes after the
# until it gets to the end of the line.
Let’s show this by using an example:
#this is a comment in Python
print('Hello World')#This is also a comment in Python
text = '# This is not a comment because it’s inside quotes.’
7 Programming in code

§ Decisions in programming |
The other thing you looked at in the last chapter was decisions.
One of the most important aspects in programming is setting up
conditions: asking the question if...? Before moving on to the next
chapter we will explore this in a little more detail.

IF statement
Python provides the IF statement as the sole selection statement.
Consider the following example of a simple IF statement in Python:
if value <Q:
orint('The value is negative.’

Python does not use parentheses to enclose the condition but as the IF
statement is a compound statement, it requires a colon (?) following the
condition, even though there is only a single statement to be executed.
The format of the simple IF statement is shown below.
if <condition>:
<statement-block>

ELSE structure
The IF statement can also contain an ELSE part to be executed when
the logical condition is false. It too is acompound statement which
is indicated by the required colon following the ELSE statement.
if value < 0:
print('The value is negative.')
value = abs( value )
else:
print(‘The value is positive.’)

The format of the IF-ELSE structure is shown below.


if <condition>:
<statement-block-1>
else:
<statement-block-2>

A For the examination you will ffString evaluations


need to understand the need
for, and how to manipulate, Comparison of two strings
strings.
Often a programmer needs to compare two strings. In Python,
A Equivalency is to look for this can be achieved using ordinary operators.
equality (are they the same?)
in value, etc. The normal equivalency operator is also used in Python to evaluate
strings.

gan
el...String..
evaluations

An example could be:


e Question | mystr1 = ‘A String’
2 What will the following | mystr2 = 'A String’
pseudo-code evaluate to? ionysth =i
SUBSTRING(2, 9, 'computer | print(‘They Are The Same.’)
science’)
else :
print(‘They Are Not The Same.’)
will output They Are The Same.

Chapter review

In this chapter we have built upon the last few chapters of the book and
explored the use of programming languages focusing upon Python.
We looked at indentation, strings and decisions in programming.
Remember, before tackling any computer science task or examination
question on this topic you must:
® Be able to write programs in a high-level programming language
® Understand and be able to explain what an algorithm is, what
algorithms are used for and be able to interpret algorithms
(flowcharts, pseudo-code, written descriptions, program code)
(More on this topic in Chapters 1, 2, 3, 6, 8, 14, 5, 11, 10)
® Understand and be able to create an algorithm to solve a particular
problem, making use of programming constructs (Sequence, selection,
iteration) and using appropriate conventions (flowchart, pSeudo-
code, written description, draft program code) (More on this topic in
Chapters 2, 3, 4, 6, 8, 14, 5, 11, 10)
® Understand and be able to explain the purpose of a given algorithm
and how an algorithm works (More on this topic in Chapters 2, 3, 4, 6,
Sela Dalian tO)
® Understand and be able to explain how to code an algorithm in a high-
level language (More on this topic in Chapters 6, 8, 14, 5, 11, 10)
® Understand and be able to explain how the choice of algorithm is
influenced by the data structures and data values that need to be
manipulated (More on this topic in Chapters 6, 8, 14, 5, 11, 10, 18)
® Be able to use sequencing, selection and iteration constructs in your
programs (More on this topic in Chapters 3, 4, 6, 8, 14, 5, 11, 10)
® Understand and be able to explain how to manipulate, strings (More
on this topic in Chapters 6, 8, 14, 5, 11, 10)
® Understand and be able to write code that accepts and responds
appropriately to user input (More on this topic in Chapters 2, 3, 6, 8,
125 Set 20).
8 Iteration and selection

understand how to determine the correct


output of an algorithm for a given set of data
/ *) 1.1.1 understand what an algorithm is, what
1.1.6 understand how to code an algorithm in a
algorithms are used for and be able to interpret
high-level language
algorithms (flowcharts, pseudo-code, written
descriptions, program code) 1.1.7. understand how the choice of algorithm is
influenced by the data structures and data
1.1.2 understand how to create an algorithm to
values that need to be manipulated
solve a particular problem, making use of

iteration) and using appropriate conventions iteration constructs in their programs


(flowchart, pseudo-code, written description, 2.3.3 understand the need for, and how to
draft program code) manipulate, strings
} 1.1.3 understand the purpose of a given algorithm 2.4.1 understand how to write code that accepts and
and how an algorithm works responds appropriately to user input

In the first section of this book we learnt that computer


programming is all about creating a set of instructions to complete
a specific task. You do this in your everyday life, you just don’t
think of what you are doing in a programming way. In the first
section of the book we also explored selection and the use of
IF statements and how these can be represented in flowcharts
and code.
Let’s look at an example of a simple sequence. You get home from
school and want to make yourself a jam sandwich — you know
that you will first need to get two slices of bread, butter each
piece, spread the jam, and finally put the two together. You can’t
put the jam on the bread first or put the slices of bread together
before you spread the butter and jam, everything has to be done
in the correct sequence. To create any dish you have to follow a
recipe. A recipe, like a correctly sequenced program, is useful for
replicating a set of actions.
In the context of computing, programming means creating a set
of instructions not for a human to perform but for a computer to
perform. These actions are performed in order to accomplish a
specific task.
a
Understanding loops

! Key points In practice even a simple task like this contains a number of
actions, some of which are repeated. We explored this when we
A Functions and procedures are looked at cleaning your teeth. You had a procedure to do, cleaning
quite similar as they are bits your teeth each day, and a function to do, brushing x number of
of code that can be recalled times. But remember that a computer can only do exactly what we
at any point in your program tell it to do. So we need to explore each action in much more detail.
and used again and again but
functions can return a value. You have already learnt in the last chapter about sequencing and
selection.
A There are three fundamental
control structures: Remember:
e sequence @ A sequence control structure is simply a series of procedures
e selection that follow one another.
e iteration (looping). @ A selection (if-then-else) control structure involves a choice.
A For the examination you will
need to understand how to §§Understanding loops
create an algorithm to solve a
The last of the three basic steps in coding is the use of loops. You
particular problem, making use
may think that writing a set of instructions with a loop for a
of programming constructs
computer to repeat will be simple, but computer programming
such as sequence, selection
must be exact. The simplest loop construct is called indefinite
and iteration.
iteration. This is where the loop controls no variables, and simply
executes its body repeatedly until a condition is true. Loops can
also control variables and this type of loop is referred to as definite
iteration. So the terms definite and indefinite come from the
1 Write the pseudo-code number of iterations which is, for example, definite in a FOR loop
to create a repeat-until as it is controlled by the loop counter, but indefinite in a WHILE
iteration for 1 - 6. loop because this is a controlled condition and it is not known
how many times the loop will iterate until the condition is true.

! Key points
A A Joop is a way of repeating a statement a number of times until the
condition is met. The condition allows the user to escape from the loop.
A The three combining principles (Sequence, iteration/repetition and
selection/choice) are basic to all imperative programming languages.
You must be able to write and understand programs using these
statement types.
A In indefinite loops, the number of iterations is not known before we
start to execute the body of the loop, it all depends on when a certain
condition becomes true. For example WHILE.
A In definite loops, the number of iterations is known before we start
the execution of the body of the loop.
A A counter is a timer or variable that can be used to count.
8 Iteration and selection
h ed

The following instructions are taken from a shampoo bottle.


@ Wet hair @ Rinse
@ Apply shampoo @ Repeat application
@ Work to a rich lather
For us to use the shampoo, these instructions are fairly simple
and easy to follow. But for a computer they are useless. You know
2 Write the pseudo-code enough about the process not to put the whole bottle of shampoo
to create a for iteration to onto your hair at one time, you know that you only need to
output 1 - 3. repeat the application one time. If this was a computer program it
would repeat forever. Using these instructions without additional
information would not always achieve the same result.

— Forever loops in Python


As we have seen some indefinite iteration loops run forever. If
you set the condition in a ‘while’ loop to be ‘True’, it can never be
False and the loop will never end. This can be useful. Let’s look
at an example of this:
while True:
answer = input('Type a word and press return:’)
print(‘Please do not type \"' + answer + '\' again’)
Output:
Type a word and press return: Saturday
Please do not type ‘Saturday’ again
Type a word and press return:

This loop will repeat forever.

§ Adding conditions
Remember that whenever you write any algorithm, it must:
@ be detailed enough to describe what needs to be done to solve the
problem
® be unambiguous enough so that anyone could perform the actions
® always give the same results with the same input
® give the correct results.
Let’s now consider the same problem using a flowchart, but add
some extra conditions to meet these requirements (see Figure 8.1).
We can also use structured English:
Wet hair
Loop While only when hair is not clean
Apply shampoo
Lather
Rinse
Figure 8.1 A flowchart End Loop

ae
Exploring different types of loops

With these refinements, we are starting to get an algorithm that


could work. Notice how we have added a loop while or while loop
A condition-controlled loop statement with a testable condition to see whether hair is clean.
Causes a Statement or set of
statements to repeat as long as The test is at the start/top of the loop. The body of the loop is
a condition is false. In Python indented to show clearly what is happening within the loop. We
you use the WHILE statement to have also added an end statement to denote the end of the block
write a condition-controlled loop. of code that is within the loop.
You need to understand that iteration (looping) must always
specify a number of steps or until a condition is true.
So we could write in English:
SET number to 1
INPUT a number
WHILE number is not >9 repeat the following two
] Key points instructions

A A repetition structure causes a ADD number to total


statement or set of statements INPUT next number
to execute repeatedly. PRINT total
A In pseudo-code the command
We now have created a repeating loop which is also called an
WHILE is a loop (repetition)
iteration. A WHILE loop does not always execute — if the loop
with a simple conditional test
condition is false (number is greater than 9) then the loop won't
at its beginning. do anything.

§ Exploring different types of loops


A WHILE is a loop (repetition) with a simple conditional test at its
beginning. This is what we used in our shampooing hair example.
A In pseudo-code REPEAT-
A REPEAT-UNTIL is a loop with a simple conditional test at the
UNTIL is a loop with a simple
bottom.
conditional test at the bottom.
4 |In pseudo-code FOR is a A FOR is a ‘counting’ loop.
‘counting’ loop. Sometimes we know how many times a loop will need to run.
For example, Print the odd numbers from 1 to 101. The opposite
of this is when we do not know how many times the loop will
need to run, for example if we want to ask the user to guess a pre-
determined number between 1 and 101. You will not know how
many guesses it will take.
In our example of brushing our teeth we do know the number of
times we need the loop to run, so we could have what is called a
FOR counting loop to brush x times. If we did not want to use a FOR
loop but wanted a condition that continued until the teeth were
clean, we could use a REPEAT-UNTIL teeth are clean loop. Unlike a
WHILE loop a REPEAT-UNTIL loop will always execute. It should be
noted that a REPEAT-UNTIL loop doesn’t exist in some languages
including Python.
8 Iteration and selection

Pre-condition loop Post-condition loop

Body of —
the loop —

Figure 8.2 Pre- and post-condition flowchart examples

WHILE statements will continue to loop until the condition is false.


] Key point
WHILE condition
While statements are efficient
sequence
loops that will continue to loop
until the condition is false. ENDWHILE

REPEAT-UNTIL statements will continue to loop until the condition


Seuitdes

The difference between a WHILE loop and a REPEAT-UNTIL loop is


therefore that in a WHILE loop the condition is checked before the
first iteration of the loop.
Here is a quick example; obviously 5 does not equal 8 so the
condition is false:
3 Write the pseudo-code to Viet 5 88 #this code will never run
create a while iteration to
OutpUL 73: whereas:
REPEAT-UNTIL 5 = 8 #this code will run forever as 5 will never be 8

Let’s take a look at an iteration (looping) structure with a


REPEAT-UNTIL loop in a flowchart, see Figure 8.3.
Selection

START

Count =—FA6)

oun

Figure 8.3 A WHILE loop

This will have the following pseudo-code:


Se lacount/ ©.
WHILE count < 10
a iecounialO.cOUnt ca
Repeat-until statements are also GENO countulG@lDISPLAY
efficient loops that will continue ENDWHILE
until the condition is true.
SEND 'The end’ TO DISPLAY

4 Selection
We know now that in practice sequences are not a simple line,
sometimes the next action depends upon the last decision. We also
know that this is called selection. In selection one statement or a block
| Sequences are often not a of statements within a set of statements is executed depending on the
simple line; often the next action | state of the processing of the program at that instant.
depends upon the last decision.
oe We started to explore selection when we looked at IF statements
This is called selection.
in the last two chapters.
8 Iteration and selection

In pseudo-code you use the following syntax for a selection where


if <expression> is true then < first command> is executed, otherwise
<second command> is executed. The red brackets < > are only to show
where you add something — you don’t need to put them in your code.
Syntax:
IF <add expression here> THEN
<add command here>
ELSE
<add command here>
END IF
Example:
IF Answer = ‘correct’ THEN
SEND ‘Well done’ TO DISPLAY
Epo
SEND 'Have another go' TO DISPLAY
END IF
An example of selection in both pseudo-code and a flowchart
would be:
IF condition is true THEN
PROCESS step(s) 1
BLS
PROCESS step(s) 2
END IF

Process / Process
~ step(s) 2
-step65)1|
Selection is when a path through
the program is selected based on
a condition.
Figure 8.4 Selection flowchart

In pseudo-code you use the following syntax for a selection where


if <expression> is true then you want the command to be executed.
The red brackets < > are only to show where you add something —
you don’t need to put them in your code.

70
Conditional sequences

Syntax:
IF <add expression here> THEN
<add command here>
END IF
Example:
IF Answer = 20 THEN
SET Score TO Score + 1
END IF

§ Conditional sequences
To explore programming in terms of what is called a conditional
sequence and loops in more detail, we will look at the rock, paper,
scissors game.

4 Produce an iteration (looping)


sequence to show the scoring
system for the game rock,
paper, scissors where the
winner of each game scores Figure 8.5 Rock, paper, scissors game. A looping structure
1 point and the overall winner
If the first player is rock and the second is scissors the first player
is the best of 21.
wins, but if the second player is paper then they win.
Design a simple rock, paper,
scissors game in Python or This game leads to one outcome or sequence of events to be
your chosen language. executed if a statement is true, and another outcome or sequence
of events to be triggered if the statement is false.
In most programming languages these structures take the form
if ... then ... else. The game also contains repetitions — when the
game is over it starts again. Another name for this is an iteration
(looping) structure (a list of instructions to do more than once).
You write this type of program to make the computer repeat a
certain command or sequence of commands. The loop may run
for a predetermined number of times, until a certain condition
becomes true, or as long as a certain condition remains true.
A flowchart is a diagram of the
You could tell the computer to:
sequence of movements or
® Do the following 20 times
actions of the program involved
® Do the following once for each word in the list
in a complex system or activity.
® Repeat the following until the user carries out a particular action.
5 8 Iteration and selection

ea In pseudo-code you use the following syntax where you want a


~ pre-conditioned loop. Execute <add command here> whilst <add
condition here> is true.
Syntax:
WHILE <add condition here> DO
<add command here>
ENDWHILE
Example:
WHILE Flag = 0 DO
SEND ‘All fine’ TO DISPLAY
ENDWHILE
Selections are usually expressed as ‘decision’ keywords such
as if...then...else...endif, switch or case. They are at the heart of all
programming.
Let’s explore what this might look like using pseudo-code:
IF condition is true THEN
perform instructions in Action’
EBSE
perform instructions in Action2
END IF
or
IF condition THEN
sequence 1
ES
sequence 2
END IF

or we could have an example which says:


IF student's mark in this exam is greater than or equal to 60
SEND 'passed' TO DISPLAY
ELSE
SEND 'failed' TO DISPLAY
END IF
In Python we have the following if selection syntax:
if test: #do this if test is true
elif test 2: #do this if test2 is true and keep doing
#this until a condition is met
else: #do this if both tests are false
In pseudo-code you use the following syntax where you want
a post-conditioned loop. Execute <command> until <condition> is
true. The loop must execute at least once.

em
Syntax:
REPEAT
<command>
UNTIL <condition>

Example:
REPEAT
SEN GoTG"Ge + |
END UNTIL Go = 10

B Elif
Sometimes you will have a function with multiple conditional
statements. In these instances you need to be careful how you use
IF statements.

For example look at this IF statement in Python:


a=2
ifa>ilia=a+
i, deaiznel=ide a!
a #this is the output of the above
4 #this is the output of the above

This is not the same as using an ELIF statement:


a=2
(Lieu. an—tarers
elifa>2:a=a+1
a #this is the output of the above
#this is the output of the above

An else statement can be combined with an if statement but there


can only be one else statement following if. The elif statement
allows you to check multiple expressions for TRUE and execute a
block of code as soon as one of the conditions evaluates to TRUE.
Unlike else, for which there can be at most one statement, there
can be a number of elif statements following an if.
if <expression1>:
<statement-block-1>
elif <expression2>:
<statement-block-2>
elif <expression3>:
<statement-block-3>
else:
<statement-block-4>
8 Iteration and selection

In Python we have the following for iteration syntax:


! Key points
for #keep doing this until test is false
A The statements in the FOR #do this for each member of a Sequence
for x in a Sequence:
loop repeat continuously for
#e.g. each character in a string, each
a specific number of times
#item in a list, etc.
that is previously set by the
programmer or the user. for x in range(10): #do this 10 times (0 through 9)

4 A WHILE loop is a control flow for x in range(5,10): #do this 5 times (5 through 9)
statement that allows code
to be executed repeatedly
based on a given Boolean Chapter review (es
condition. The WHILE loop can
be thought of as a repeating In this chapter we have explored loops in more detail.
IF statement. It will be Remember, before tackling any computer science task or examination
executed continuously until the question on this topic you must:
condition is met. ® Understand and be able to explain what an algorithm is, what
algorithms are used for and be able to interpret algorithms
(flowcharts, pseudo-code, written descriptions, program code) (More
on this topic in Chapters 1, 2, 3, 6, 7, 14, 5, 11, 10)
® Understand and be able to create an algorithm to solve a particular
problem, making use of programming constructs (Sequence, selection,
iteration) and using appropriate conventions (flowchart, pseudo-
code, written description, draft program code) (More on this topic in
Chapters 2, 3, 4, 6, 7, 14, 5, 11, 10)
® Understand and be able to explain the purpose of a given algorithm
and how an algorithm works (More on this topic in Chapters 2, 3, 4, 6,
7, 14551 810)
® Understand and be able to explain and apply how to code an
algorithm in a high-level language (More on this topic in Chapters 7, 6,
145511810)
™® Understand and be able to explain how the choice of algorithm is
influenced by the data structures and data values that need to be
manipulated (More on this topic in Chapters 18, 6, 7, 14, 5, 11, 10)
® Be able to use sequencing, selection and iteration constructs in your
programs (More on this topic in Chapters 3, 4, 6, 7, 14, 5, 11, 10)
™® Understand and be able to explain the need for, and how to
manipulate, strings (More on this topic in Chapters 6, 7, 14, 5, 11, 10)
® Understand and be able to write code that accepts and responds
appropriately to user input (More on this topic in Chapters 2, 3, 6, ve
14) 5) 11)

aie)
Ea Specification references

You should:
2.5.3 understand the purpose of, and how to use, logic operators
(AND, OR, NOT)
4.3.1 be able to construct truth tables for a given logic statement
(AND, OR, NOT)
4.3.2 be able to produce logic statements for a given problem

one
| In computer science, the Boolean data type is a logical data type
that has one of two values (usually called true or false, 0 or 1).
_ A Boolean is a variable that can It represents the truth values of logic and Boolean algebra.
| hold a single true or false value. Booleans are very important in computer science.
_& A Boolean expression is an
expression that when evaluated : Boolean: AND
is either true or false. The AND operator requires that both of its inputs are true for it
to output true. In searches, you use AND to retrieve records or
pages that contain both terms.

For the examination you will The OR operator requires at least one of the specified conditions
need to: to be true. In searches you use OR to retrieve records or pages that
A understand the purpose of, contain either of two or more terms. The OR operator is generally
and how to use, logic operators used to assess similar, equivalent or synonymous conditions.
_ (AND, OR, NOT).
| & be able to construct truth
§ Boolean: NOT
tables for a given logic The NOT operator reverses the value of a Boolean expression. Thus
statement (AND, OR, NOT). if B is true X is false. If B is false X is true.

2 Questions
1 What is an AND function? 1 Use this book and other
2 What is a Boolean variable? sources such as the internet to
research data types (integer,
3 What is a NOT function?
real, Boolean, char).
9 Booleans and logic gates
s
is I key point § Understanding the concept of logic gates
In computers, transistors act as Logic gates can be understood better if you think of simple switches.
switches. The current flowing Logic functions depend on binary bits of information. You should
through can be turned on or know from Key Stage 3 that a bit can take on only two values as
off. A switch is a also the name it can be in one of two states. We will explore this in more detail
given to a device for connecting later in the book. A simple switch is either open or closed.
LTO NAS TOUT es The bits could be represented by a yes or a no or can be ON
(conducting) or OFF (not conducting).
Whatever the two states might be, we will call one of the states a
2 Use this book and other ‘1’ and the other a ‘0’. It doesn’t matter which we call ‘1’ and which
sources such as the internet we call ‘0’ if we are consistent but we will call the closed or
to research the use of logic conducting state of a switch a ‘1’ and the open or non-conducting
operators (AND, OR, NOT). state a ‘0’.

BOR circuit
A parallel system of switches would make an ‘OR’ circuit (see
Figure 9.1). That is, if any one or more of the switches A or B is in
state ‘1’, the system is in state ‘1’ (conducting).
For a 2-input OR gate, the output Q is true if EITHER input A
‘OR’ input B is true, giving the Boolean expression: (Q = A or B).
We show this in the form of what is called a truth table. The truth
table specifies the state of the system for each state of the switches:

OR GATE A 2
0 O O
wir 1 4
[7 1 0 z
if 1 a
Ov
Oo—

We show this in electrical diagrams using the symbol below. To


Figure 9.1 A parallel circuit representing help you understand the concept, the diagram also shows the
an OR gate
possible states.

Two One
inputs —— aes output

0 ee OS ae! |
0 0 1 1 We
Figure 9.2 Electrical diagrams showing input and states of
an OR gate

ms
AND circuit

BAND circuit
Let’s also look at how a series of switches makes an ‘AND’ circuit.
That is, all switches must be in the ‘1’ state for the system to be in
the ‘1’ state (conducting).

AND GATE

Figure 9.3 A series circuit representing


an AND gate

For a 2-input AND gate, the output Q is true if BOTH input A


‘AND’ input B are both true, giving the Boolean expression: ( Q
= Aand B ). The truth table for the system would look like this:

INPUTS | OUTPUT

O O

ali O

6) O

ee
ee
re
—© a ak

Again the circuit symbol is shown below with possible states


indicated.

Two ae One
inputs output

1 1 @) 0

Figure 9.4 Electrical diagrams showing input and states


of an AND gate

§ Turning problems into binary logic


Suppose we have a set of race starting lights with two coloured
lights, amber and green. If both the amber and green lights are on,
the race starts. First the amber light is on to warn the drivers that
the race is about to start, next the green light comes on and the
race starts. We could have a bit which is TRUE if the light is on
and FALSE if the light is not on. We could now give the green bit a
name, such as G and the amber bit aname A. Finally, we could have
a bit named S which is true if the race can start. We know that the
9 Booleans and logic gates

value for S is determined by the values of G and A as the race can


only start when both are on. We can therefore define S as follows:
S is TRUE if Ais TRUE andif,G is TRUE

In Boolean notation, we would write


Spe AV NNIBIG

You know from earlier chapters that the letters A, G and S are
called variables because we don’t know their values when we look
at the expression. We do know that they can take on the values of
FALSE and TRUE. We also know that S’s value will be determined
once we know the values for A and G.

y NOT circuit
Unlike AND and OR gates, NOT gates have only one input and
one output. The output is exactly the opposite of the input, so if
the input is a 0, the output is a 1 and vice versa. Here’s how we
represent a NOT in electronics.
One ' One
input output

1 P 0 0) P 1

Figure 9.5 Electrical diagrams


showing input and states of a
NOT gate
The truth table would look like this:

| OUTPUT

§ How to read a truth table


You will now know that a truth table is simply a chart of the
different possible truth values of a statement given the truth value
of the simpler statements.
For example, the truth value C shown below is true when A is true
or B is true:

;
1 aL
aL O al
O t al
O fe) 0)

Gat.)
Truth tables with more inputs

This makes sense, since it’s an ‘or’ statement, like ‘It is raining or
it is sunny’ and thus is true when either part of the statement A:
‘It is raining’ or B: ‘It is sunny’ is true.

§ Truth tables with more inputs


A truth table is always a chart of 1s and 0s arranged to indicate the
results (or outputs) of all possible inputs. All possible inputs are
A truth table is a diagram used arranged in columns on the left and the resulting outputs are listed
to show the value of a Boolean in columns on the right. The chart below tabulates or evaluates the
expression for all possible output of the circuit for all possible combinations of inputs. We
variable combinations. can then write the result at each gate’s output. There are 2 to the
power 1 possible states (or combination of inputs). For example
with three inputs there are 23 = 8 possible combination of inputs.

INPUTS OUTPUT

a)
Ae
f-16-
iey
ea
ie)
ey
PES
[ay
TS eS
OQ)
oot
mo
fi)
iP i
Or
On
TO
Ey
Eon
SOn
JS
HE (o)
oO
(a)
KR
(e)
=
i

§ Constructing a circuit from logic


We can easily create a circuit diagram from the description of the
logic using these symbols. Let’s say we want a system where we
have four inputs A, B, Cand D. We want a circuit if either AAND
Done AND Dare true.
Part of the truth table would look like this:

Cag
als PF
DO
FB
OF
F
Bo sSdzorees OC
BP
FP
PF
@O
FR
O OC
PB
BP
OO
RP
F RP
PrP
PR
OO

mJ ice)
9 Booleans and logic gates

We could create a circuit using an AND gate connected to an OR


gate as shown below.
A __.AANDB
B A AND B
OR
G C AND D
D — CANDD
? Question Figure 9.6 Electrical diagram showing
4 What is a truth table? connected AND and OR gates

Controlling a calculator display with


:logic gates
Calculator displays work using combinations of logic gates,
indeed the whole calculator works using them. If we look at a
small section of the display for a single number we can see how
this would work.

A 0 il
Bl 1 ——

(Gr 1 im

7 a
Figure 9.7 Combination of logic gates to
control section of calculator display

[ Key point
Each segment of a calculator’s display is switched on and off by a
series of logic gates that are connected together. We will explore
For the examination you will the bottom segment coloured in the image.
need to be able to produce logic
statements for a given problem. We need to turn this small segment on if we want to show the
number as a decimal using the computer’s binary system. You
will learn about binary later in the book, but you may remember
it from Key Stage 3. These are the possible numbers with their
binary representation: 0 (binary 0000), 1 (0001), 3 (0011), 4 (0100),
5 (0101), 6 (0110), 7 (0111), 8 (1000) and 9 (1001) — but not if we’re
showing the number 2 (0010).
We can make the segment switch on and off correctly for the
numbers 1-9 by using a circuit of three OR gates and one NOT gate.
If we feed the patterns of binary numbers for these numbers into the
four inputs, the segment will turn on and off correctly for each one.
For example if we feed in the number 7 as the four inputs (0111)
the gates will correctly switch on the segment:
The other six segments of each digit are controlled by other
combinations of logic gates.

oe
Booleans in Python

The truth table for our calculator display for the numbers would be:
Displayed Binary |
A B Cc D

O 0000 O O O O A

1 0004 O 0 O alt iL

D 0010 O 6) 4 O O

3 OOdE 0) O 1 alt at

4 0100 O Al O O ab

5 0101 10) “ik 0) a il

6 OMLGO) O ell al O al

i Odtat O al als il 4

8 1000 Ah O O O at

9 1001 all (0) O al at

§ Booleans in Python
In Python, Boolean operators are spelled out as the words and, or,
and not.
! Key points
If we want to see if a number (a) is bigger than number (b) by
4 Forgetting the colon “is a more than two we could write:
very common mistake when
def a_bigger(a, b):
first learning Python.
ce rane
=to). =.
4 NOT: A logic gate that outputs
the opposite value to the input. return True
A AND: A logic gate that outputs else:
1 if both inputs are 1. return False
A OR: A logic gate that outputs #can all be written as just
1 if either, or both, of the two #return a>b+2
inputs are 1.
Notice that the if-test does not need to be in parentheses in Boolean.

Chapter review

In this chapter we have explored Booleans and logic gates. We also


explored truth tables.
Remember, before tackling any computer science task or examination
question on logic you must:
® Understand the purpose of, and how to use, logic operators (AND,
OR, NOT)
® Be able to construct truth tables for a given logic statement (AND,
OR, NOT)
® Be able to produce logic statements for a given problem.
10 Structuring programs ina
a
ae
B+ Specification references understand how the choice of algorithm is
' You should:
influenced by the data structures and data
values that need to be manipulated
We 1.1.1 understand what an algorithm is, what
algorithms are used for and be able to interpret be able to use sequencing, selection and
algorithms (flowcharts, pSeudo-code, written iteration constructs in their programs
descriptions, program code) understand the need for, and how to
understand how to create an algorithm to manipulate, strings
solve a particular problem, making use of understand the need for, and how to use,
programming constructs (Sequence, selection, global and local variables when implementing
iteration) and using appropriate conventions subprograms
(flowchart, pSseudo-code, written description, understand the benefits of using subprograms
draft program code) and be able to write code that uses user-
understand the purpose of a given algorithm written and pre-existing (built-in, library)
and how an algorithm works subprograms
understand how to determine the correct understand the concept of passing data into
output of an algorithm for a given set of data and out of subprograms (procedures, functions)
understand how to code an algorithm in a be able to create subprograms that use
high-level language parameters

You should now understand how programs are made up of


subroutines which consist of sequences, selection and repetitions.
A single program could include all of these, with some elements
nested within others. It is possible to divide any problem into
tasks and each task into subtasks. It is important to do this until
each subtask is simple enough to be easily constructed in code.
Each basic subroutine of a program should perform a simple —
task. So far we have been thinking about these tasks in terms of
individual actions or subtasks. A subtask could require a single
unit, or a combination of units. Generally speaking a function
returns a value and can be included within an expression, a
procedure doesn't.
Structured programming is sometimes also called modular
programming. Structured programming is why it is called a ‘top-
down’ design model. Programmers map out an overall program
structure by dividing the code into separate subsections. Defined
functions are coded in separate modules. This means that modules
can be reused in other programs. After a module has been tested

82
Understanding scope

individually, it is then integrated with other modules into the


overall program structure. Structured programs use looping
constructs such as ‘for’, ‘until’, and ‘while’.

In pseudo-code you use the following syntax to define a procedure.


The red brackets < > are only to show where you add something —
you don’t need to put them in your code.
Syntax:
PROCEDURE <id> (<add parameter here>. ...)
BEGIN PROCEDURE
<add command here>
END’ PROCEDURE

Example:
PROCEDURE CalculateAverage (Mark1, Mark2, Mark3)
BEGINSPROCEDURE
SET Avg to (Mark1 + Mark2 + Mark3)/3
END PROCEDURE

A modular approach to programming has many benefits. First it


makes the coding much easier. Once written, modules can be
Once a function is correctly checked individually and then be placed in the correct order
constructed, it can be used as a
within the program. Some tasks are quite generic, for example
building block for larger program you may need to perform a particular mathematical operation
structures.
within your code. It would be pointless to make every programmer
have to write their own code for something so common.
To avoid unnecessary work, all programming languages have
predefined modules that can be called. It is possible to ‘call’ any
module. When a module is called the name for this is a subroutine
1 Use this book and other or procedure. A procedure is a special kind of module that
sources such as the internet performs a task or set of tasks that can be added to another
to research data structures program as a subtask of that particular program.
(records, one-dimensional
In pseudo-code you use the following syntax to call a procedure
arrays, two-dimensional
or a function. The red brackets < > are only to show where you
arrays).
add something — you don’t need to put them in your code.
Syntax:
<id> (<add expression here>, ...)

§ Understanding scope
You should now understand the terms procedure and function
but you will not be able to read or write large computer programs
without understanding the concept of scope.
The scope of something (function, variable, etc.) in a program is
the range of code that it applies to. Let’s look at an analogy in
everyday life. You have people you know on a social networking
10 Structuring programs in a modular way

site. What you write on a public group or wall is global scope, but
if you have a group between just your friends, it’s local scope; only
people in the group can see what you put there — you may even
have special language that you use with your friends that no one
outside this group would understand. This is a bit like defining
a function called friends with things no one outside the group
could access or use.

This would then be called local scope (local to a particular


function; this would mean the variable can only be used inside
the function of friends). On the wall anyone can access what you
post, it is global in real terms and in programming terms (usable
from anywhere) so would be called global scope.
Now imagine your program as a virtual social media site with
each module separate. Parts of it will be restricted to local use and
can only be accessed within the module where they exist.

Project | Project Il

Module A Module B — c

LOCA
LOCAL
5 Variable
Variable

Cannot be accessed
Figure 10.1 Local variables
! Key points
Other parts are global, these are accessible from anywhere.
A A function is a group of
statements that exist within
Project | |
a program for the purpose of
performing a specific task. A
function returns a value.
| ModuleA | ModuleB
A In most programming
languages variables are
treated as global if not GLOBAL GLOBAL

declared as local, but in Variable Variable


Python it is the opposite:
variables are local if not
declared as global.
Figure 10.2 Global variables

In some programming languages, special things happen when


variables go in and out of scope. Memory may be allocated to hold
data, or memory may be freed when variables go out of scope.
Scope is also useful for error-checking.
84
EE
ae eee a
Local scope

The scope of a variable in a program is the lines of code in the


program where the variable can be accessed. So the concept of
scope applies not only to variables and variable names but also to
the names of procedures.

] Key point There is another term that is used to describe scope and that is
visibility. You should be aware that the two terms are the same in
Visibility is another name for the terms of programming.
scope of a variable in a program;
it is the lines of code in the §§Local scope
program where the variable can
be accessed. Unlike global scoped variables, local variables have their scope limited
to the block where they are declared. A block is the body of a control
structure, the body of a subroutine, or a place such as the file or string
] Key points with the code where the variable is declared. So the term local scope
A Different subroutines can have refers to when a variable can be read and modified only from within
local variables with the same the subroutine or place in which it is declared.
names because the functions
Let’s take a look at a ‘Visual Basic’ example:
cannot access each other’s
Sub TestRoutine()
local variables.
Dim X As Long
A A local variable is created
inside a subroutine and cannot Dim Y As Long
be accessed by statements X = 1234
that are outside the function - Y = 4321
it has local scope.
Debug.Print 'X:' & X,'Y: '& Y
A For the examination you will
End Sub
need to understand the need
for, and how to use, global In the above example, the variables (X and Y) can only be accessed
and local variables when within the TestRoutine subroutine. The variables are created when
implementing subprograms. TestRoutine is called and they are destroyed when TestRoutine ends.
It should be understood that these variables cannot be accessed or
modified from any other subroutine.
Local scope has the highest priority with regard to the other scope
levels, so if, in addition to these variables, you have variables with
2 Use this book and other the same names declared at a global scope level (see below), the
sources such as the internet code within the local TestRoutine subroutine utilises the variables
to research how to use global declared within this subroutine, not the variables with the same
and local variables. name declared at the project level.
The term local scope refers to when a variable can be read and
modified only from within the subroutine in which it is declared.

Global scope
Global scope variables are those which have the capability of
Often programmers get errors
being accessed from anywhere within the project that contains
because they declare all
their declaration as well as from other projects that refer to that
variables globally.
initial project.
10 Structuring program
10.Structuring MOC way
in aamodular
programss In Wey

Python and scope


Let’s look at a Python example of scope.
total = O #we set the counter to 0, this is a global
#variable
def sum(arg1, arg2): — #function definition is here
#here total is a local variable
total = arg + arg2 _ #add both the parameters and return
#them
orint(‘Inside the function local total : {O}'format(str(total)))
return total
sum(10, 20 )
orint (‘Outside the function global total : ', str(total ))

This would return:


Inside the function local total : 30
Outside the function global total : 0
When a variable is referenced, Python searches for it in the
following order:
1 in the local scope
2 in any enclosing functions’ local scopes
3 in the global scope
4 in the built-in scope.
The first occurrence wins.

Why is it better to use local scope?


It is good programming style to use local variables whenever
possible. This helps to avoid cluttering the global environment
with unnecessary names.
Other benefits of using local scope include:
e The source code is easier to understand when the scope of
individual elements is limited.
® Subroutines can be more easily reused if their variables are all
local.
@ Global variables can be read or modified by any part of the
program. A global variable can be accessed or set by any part
of the program, and any rules regarding its use can easily be
broken or forgotten.
@ Global names are also available everywhere. You may
unknowingly end up using a global variable when you think
you are using a local variable.
© Testing your code is harder if you use global variables.
Python and scope

Chapter review

In this chapter we have explored modules and how to structure programs


in a modular way.
We also explored local and global scope.
Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain the need for, and how to use,
global and local variables when implementing subprograms
® Understand and be able to explain the benefits of using subprograms
and be able to write code that uses user-written and pre-existing
(built-in, library) subprograms
® Understand and be able to explain the concept of passing data into
and out of subprograms (procedures, functions)
® Be able to create subprograms that use parameters
® Understand and be able to explain what an algorithm is, what
algorithms are used for and be able to interpret algorithms
(flowcharts, pseudo-code, written descriptions, program code) (More
on this topic in Chapters 1, 2, 3, 6, 7, 8, 14, 5, 11)

® Understand and be able to create an algorithm to solve a particular


problem, making use of programming constructs (Sequence, selection,
iteration) and using appropriate conventions (flowchart, pseudo-
code, written description, draft program code) (More on this topic in
Chapters 2-3, 45 6y,1o 145 5411)
® Understand and be able to explain the purpose of a given algorithm
and how an algorithm works (More on this topic in Chapters 2, 3, 4, 6,
di, Spel, 759)14)
® Understand and be able to code an algorithm in a high-level language
(More on this topic in Chapters 7, 6, 8, 14, 5, 11)
® Understand and be able to explain how the choice of algorithm is
influenced by the data structures and data values that need to be
manipulated (More on this topic in Chapters 6, 7, 8, 14, 5, 11, 18)
® Be able to use sequencing, selection and iteration constructs in your
programs (More on this topic in Chapters 3, 4, 6, 7, 8, 14, 5, 11)
® Understand and be able to manipulate strings (More on this topic in
Chapters 6, 7, 8, 14, 5, 11).
11 Subroutines

Bs Specification references
1.1.4 understand how to determine the correct
©) You should: output of an algorithm for a given set of data
fp 1.1.1 understand what an algorithm is, what
1.1.6 understand how to code an algorithm in a
: algorithms are used for and be able to interpret
high-level language
algorithms (flowcharts, pSeudo-code, written
descriptions, program code) 1.1.7 understand how the choice of algorithm is
influenced by the data structures and data
1.1.2 understand how to create an algorithm to
values that need to be manipulated
solve a particular problem, making use of
programming constructs (sequence, selection, 2.2.2 be able to use sequencing, selection and
iteration) and using appropriate conventions iteration constructs in their programs
(flowchart, pSeudo-code, written description, 2.3.3 understand the need for, and how to
x. draft program code) manipulate, strings
1.1.3 understand the purpose of a given algorithm 2.4.1 understand how to write code that accepts and
and how an algorithm works responds appropriately to user input

A subroutine is a computer program contained within another


| program. It operates semi-independently of the main program.
| A subroutine is a computer
| program contained within There are two basic forms of subroutines:
another program. It operates ® named code that does a particular task, often called procedures
semi-independently of the main ® code that does a task but also returns a value, often called
program. functions.

The basic idea of a subroutine is to group a collection of statements


into a named piece of code that can be invoked by simply calling
the subroutine by its name.
] Key point
In effect we can give a section of code a name and use that name
A subroutine is a named ‘out
as a statement in another part of the program. When the name
of line’ block of code that may
is reached in the code, the processing in the other part of the
be executed (called) by simply
program stops while the named code is executed. When the named
writing its name in a program
statement.
code finishes executing, processing resumes with the statement
just below the named code.
The place where the named code appears is called the calling unit.
These two types of subroutines have many names.
© C++ calls the first a void function and the second a value-
returning function.
® Java calls both of them methods.
Using subroutines/subprograms

] Key points Whatever the subroutines are called. in your chosen language,
they are powerful tools for abstraction and decomposition. By
A A library is a collection of
identifying a named subprogram you will be able to identify what
files, computer programs, or is being done.
subroutines.
Many subroutines come as part of the library that comes with the
A To import is to bring in from an
language. For example, mathematical problems often need to
outside source.
calculate trigonometric functions.
The import function in a programming system imports new
predefined functions.

! Key points § Using subroutines/subprograms


4 A parameter is a variable that Subroutines the one of the most important concepts in any
receives an argument that is programming language design.
passed into a function.
Subroutines usually have the following characteristics:
A A parameter is information
® Each subroutine has a single entry point.
explicitly passed to a
@ Each subroutine has a name.
subroutine as part of the
subroutine call. There are two ways that a subroutine can gain access to the data
A For the examination you will
that it is to process: through variables or through parameter
need to understand:
passing. Parameter passing is more flexible than using variables.
A the benefits of using Subroutine call statements must include the name of the
subprograms and be able subprogram and a list of parameters to be bound to the formal
to write code that uses parameters of the subroutine. These parameters are called actual
user-written and pre- parameters.
existing (built-in, library)
subprograms a Procedures
A the concept of passing data
into and out of subprograms Procedures are collections of code that perform a specific task.
(procedures, functions). They often also use parameters.
Let’s imagine that you are washing a cup. Your process of washing
a cup could be:
® Take cup ‘a’ #‘a’ is a variable parameter as it can be assigned to more
#than one cup
@ Dip cup ‘a’ into soapy water
® Cover every part of cup ‘a’ with soap
@ Rinse cup ‘a’ with clean water to remove the soap
® Dry cup ‘a’
So every time you need to wash a cup, you do the same procedure:
dip, soap, rinse, dry. You will use the same sequence, repeated
over and over again, each time you wash a cup. This is a procedure.
When you call a procedure in programming it will do the jobs
that the procedure is programmed to do. By replacing instructions
with one single procedure statement, it makes code easier to read
and debug.
: » 11 Subroutines

‘? How to define a procedure


Pseudo-code example Python example

PROCEDURE <id> (<parameter>, ...) PROCEDURE CalculateAverage (Mark1, Mark2, Mark3) ALL functions in Python return a value of
BEGIN PROCEDURE BEGIN PROCEDURE None even if there is no formal return.
<command> SET Avg to (Mark1 + Mark2 + Mark3)/3 Because of this there are no procedures. A
END PROCEDURE END PROCEDURE function with no return value is a procedure.

How to define a function

| Pseudo-code. Pseudo-code example : | python example


FUNCTION <id> (<parameter>, ...) FUNCTION AddMarks (Mark1, Mark2, Mark3) def fahrenheit(T_in_celsius):
BEGIN FUNCTION BEGIN FUNCTION # returns the temperature in degrees Fahrenheit ' '
<command> SET Total to (Mark1 + Mark2 + Mark3)/3 return (T_in_celsius * 9 / 5) + 32
RETURN <expression> RETURN Total for tin (22.6, 25.8, 27.3, 29.8):
END FUNCTION END FUNCTION print(t, ":", fahrenheit(t))
Output:
22.6 : 72.68
25.8 : 78.44
27.3 : 81.14
29.8 : 85.64

L Functions
Functions are structurally the same as procedures but are based on
mathematical functions. Functions are called by their names in
A function is a group of expressions, along with the required actual parameters. A function
statements that exist within can be included in an expression (e.g. print(max(3,4,6))).
a program for the purpose of
performing a specific task. A
So a function is just like a procedure except that it returns a value.
function returns a value.
In Python a function can even return more than one value. For
example a function could be used if somebody asks you to count
the number of cars in a car park. You would go to the car park,
count, and then report the number of cars to the person who first
asked you. That is a function. A function simply returns another
value back into the program, such as complex calculation results,
the position of the mouse cursor, or the number of cars that entered
the car park.
In Python a function definition MUST come BEFORE it is called.
Functions can return almost anything: numbers, strings,
characters, anything. You can even use functions to replace
procedures completely. This is the case in some languages where
there are no special implementations of procedures.
The concept of a function is vital in mathematics. Functions are
often used in computer languages to implement mathematical
functions. The function computes one or more results, which are
determined by the parameters passed to it. When the function is
called, the calling unit lists the subprogram name followed bya
list of identifiers in parentheses.
Functions

There are two types of function:


® built-in functions |
® programmer defined functions.
We looked at built-in functions earlier in the book. Built-in functions
are predefined functions built into the language to perform a
wide range of operations. Built-in functions are functions that are
always available for your code to call.
Functions serve two primary development roles:
! Key point i Functions provide a tool for splitting systems into pieces that
Functions return a value and have well-defined roles. For instance, to make a pizza at home,
procedures do not. you would start by mixing the dough, then roll it out, then add
cheese and other toppings, then put it in the oven.
2 If you are programming a pizza-making robot, functions and
procedures would help you divide the overall ‘make pizza’ task
into small parts with one function for each subtask in the
process. It’s easier to implement the smaller tasks in isolation
than it is to implement the entire process at once.
Functions are about the procedure of how to do something, rather
than what it is doing. The reference between a function and a
procedure is that a function returns a value and a procedure just
executes commands.
A function in Python is first defined by a def statement. Unlike
functions in compiled languages such as C, def in Python is an
executable statement. The function does not exist until Python
reaches and runs the def.
Functions are subroutines and often they have what is called
a parameter interface. This is where the parameters for the
subroutine are defined. If a subroutine has a parameter interface,
you supply values to all of the parameters in its interface when
you call it.
The syntax for Python looks like this:
def function-name(Parameter):
2? Question statements #i.e. the function body

1 What are the two main roles of Parameter can consist of none or more parameters.
a function?
Parameters in functions are called arguments.

Define a function in Python


The definition of a function will always have the keyword def and
the function name at the beginning of the code.
def greeting(): #a colon marks the end of the
#function name and the start of
#the code it contains
print(‘Hello!') #this is the code within the function
11 Subroutines
en

Call a function in Python |


Typing the function name followed by parentheses into the shell
window calls the function and shows the output.
greeting() #parentheses show that this is a
#function call, not a variable

Output: Hello!

The function body always consists of indented statements. It gets


executed every time the function is called.
Parameters can be mandatory or optional depending upon the
function. The optional parameters (zero or more) must follow any
mandatory parameters.
A return statement ends the execution of the function call and
‘returns’ the result, i.e. the value of the expression following the
return keyword, to the caller. If the return statement is without an
expression, the special value None is returned.
By default, all names assigned in a function are local to that
function and exist only while the function runs. To assign a
name in the enclosing module, functions need to list it in a global
statement.

! Key point You will need to understand scope in order to effectively use
Parameters in functions are often
subroutines as local variables usually:
called arguments.
® only exist while the subroutine is executing
® are only accessible within the subroutine.

§ Optional parameters
Functions can sometimes have optional parameters. In effect these
are default parameters. Not all languages do this but where they
do default parameters are still parameters, but they don’t have to
be given as they are predefined in the programming language
used. In this case, the default values are used.
The following script greets a person. If no name is given, it will
greet everybody:
def Hello(name='everybody’):
'" Greets a person '"'
print(‘Hello '+ name + '!')
Hello('Steve')
Hello()

The output looks like this:


Hello Steve!
Hello everybody!

oe
Return values in subprograms

For this small programming task this function is of no real value,


but functions can be very useful when writing code.

§ Return values in subprograms


We now know that functions return values based upon
mathematical functions. So we know that functions return values.
For the examination you will need The problem is return values have what is called scope which we
to be able to create subprograms looked at in Chapter 10.
that use parameters.
Every time you call a procedure or a function, when the procedure
or function finishes, it is removed and everything declared by it
disappears too.
That means that if you had declared a variable in a procedure, you
can’t use that variable after the procedure finishes as it no longer
exists.
Variables, however, are accessible to those procedures placed above it.

Abstraction and generalisation in


Subprograms
Abstraction and generalisation are often used together. You will
remember the term abstraction from the first chapter.
A Abstraction is the process of
Generalisation widens the applicability of a procedure within a
removing unnecessary detail
given domain. For example, changing a procedure that adds up
from a problem.
two numbers into one that could add up any number of numbers
A Generalisation means
or one that could do more than one operation on a pair of numbers
adapting a solution that solved would be generalisation.
one problem to solve another.
It also means replacing many Abstraction reduces complexity by hiding irrelevant detail;
things with one. First decide generalisation reduces complexity by replacing multiple tasks
what similar problems have which perform similar functions with a_ single construct.
in common, then write code Programming languages provide programmers with the tools for
that by using variables and generalisation through variables and parameters. Generalisation
functions can be called places emphasis on the similarities between objects and actions.
when you need these similar Programming languages provide abstraction through procedures,
features. functions and modules which permit the programmer to distinguish
A A procedure is a subroutine between what a program does and how it is implemented.
which is a smaller program
that is part of a main program.
The terms parameter and argument are often used for the same
thing, indeed in many Python tutorials parameters are called
A In computer programming
arguments. But in many languages they are very different. A
a function is a subprogram;
parameter refers to the variable found in the function when it is
given a particular set of defined. A parameter is always a variable, it can change.
argument values, the function
function(x) = x + x #x is a parameter
returns a unique result.
An argument is the actual value passed.
function(2) #2 is the argument of the function
11 Subroutines

Chapter review ax

In this chapter we have explored subroutines.


Remember, before tackling any computer science task or examination
question on this topic you must:

® Understand and be able to explain what an algorithm is, what


algorithms are used for and be able to interpret algorithms
(flowcharts, pseudo-code, written descriptions, program code) (More
on this topic in Chapters 1, 2, 3, 6, 7, 8, 14, 5, 10)
® Understand and be able to create an algorithm to solve a particular
problem, making use of programming constructs (sequence, selection,
iteration) and using appropriate conventions (flowchart, pseudo-
code, written description, draft program code) (More on this topic in
Chapters 2, 3, 4,6, f, 8.14,20,10)
® Understand and be able to explain the purpose of a given algorithm
and how an algorithm works (More on this topic in Chapters 2, 3, 4, 6,
7,03 145.210)
® Understand and be able to code an algorithm in a high-level language
(More on this topic in Chapters 7, 6, 8, 14, 5, 10)

® Understand and be able to explain how the choice of algorithm is


influenced by the data structures and data values that need to be
manipulated (More on this topic in Chapters 18, 6, 7, 8, 14, 5, 10, 18)
™ Be able to use sequencing, selection and iteration constructs in your
programs (More on this topic in Chapters 3, 4, 6, 7, 8, 14, 5, 10)
® Understand and be able to explain and apply the need for, and how to
manipulate, strings (More on this topic in Chapters 6, 7, 8, 14, 5, 10)
® Understand and be able to write code that accepts and responds
appropriately to user input (More on this topic in Chapters 2, 3, 6, 7,
8,14, 5, 20).

7
12 Testing your code

be able to write programs in a high-level


You should: programming language
1.1.2 understand how to create an algorithm to 2.1.2 understand the benefit of producing
solve a particular problem, making use of programs that are easy to read and be able
programming constructs (Sequence, selection, to use techniques (comments, descriptive
iteration) and using appropriate conventions names (variables, constants, subprograms),
(flowchart, pseudo-code, written description, indentation) to improve readability and to
draft program code) explain how the code works
"| 1.1.3 understand the purpose of a given algorithm 2.1.3 be able to differentiate between types of error
and how an algorithm works in programs (logic, syntax, runtime)
a 1.1.5 understand how to identify and correct errors 2.1.4 be able to design and use test plans and test
in algorithms data (normal, boundary, erroneous)
1.1.6 understand how to code an algorithm in a 2.1.5 be able to interpret error messages and
high-level language identify, locate and fix errors in a program

You now can produce well-documented, efficient code, all laid out
well with good use of whitespace. You may think all is well but
the chances are, however hard you tried to avoid errors, they are
inevitable in any program.
Even if you have coded your program perfectly, the users of your
program will always discover some problem in your application
that you never even dreamed possible.
Although it’s impossible to eliminate every error, this section
1 Use this book and other
describes errors and should help avoid most coding errors.
sources such as the internet
to research types of error
Types of errors in computer programming
in programs (logic, syntax, As much as we may wish to deny it, human beings are not perfect.
runtime). We make mistakes. Programmers are not exempt from this and,
Use this book and other frequently, the mistakes we make are in the programs we create.
sources such as the internet These mistakes lead to issues, defects or, as they are most
to research the use of test commonly known, bugs in the programs.
plans and test data (normal,
The process of fixing these mistakes, of removing the bugs, is
boundary, erroneous).
known as debugging.
5S. eee
12 Testing your code

There are basically three types of error that computer programmers


! Key points
encounter when writing code:
A A syntax error is a mistake ® syntax errors
in the program’s words or @ runtime errors
symbols, such as misspelled @ logic errors.
keywords, missing brackets, or
incorrect indents. These types b Key points
of errors are the easiest to
A For the examination you will need to be able to interpret error
find.
messages and identify, locate and fix errors in a program.
A A syntax error is something
A If you write 12 or more lines of code, expect to find a syntax error, a
that stops the interpreter
bad array reference or a misspelled variable. This is quite normal.
from understanding the
code (it cannot run). A For the examination you will need to be able to differentiate between
types of error in programs (logic, syntax, runtime).
A A logic error is something that
stops the program doing what
it is meant to do, or causes a Syntax errors
runtime error (Such as ‘division Syntax errors or, as they are sometimes known, format errors,
by zero’). are a big problem for those who are new to programming. A
syntax error occurs when the programmer fails to obey one of the
grammar rules of the programming language that they are using.
Whilst syntax errors usually prevent the program from running
in some way, they are not easily found by a human.
e Question
Typically, syntax errors are caused by using the wrong case, placing
1 What is a syntax error?
punctuation in positions where it should not exist or failing to
insert punctuation where it should be placed within the code.

! Key points
A Often programmers get errors because they use bad variable and
function names, for example a spelling mistake in a variable name. It’s
hard enough to remember variable names without having to remember
which spelling mistake you made.
A Programmers also get errors because they inconsistently use
uppercase and lowercase letters in variable and function names.
Beginner programmers often create a variable with one case, e.g.
‘myScore = 7’, then later on try to reference it with a different case,
e.g. ‘if myscore > 5:’ or even ‘if score > 5:’ and wonder why the code
doesn’t run!

The most common syntax errors are as follows.


@ When you forget the quotes around a string.
® When you forget to put a colon at the end of an if, elif, else, for,
while, class or def statement.
This causes ‘SyntaxError: invalid syntax’ in Ey nOnec.
if input = #needs a colon

<>
print(‘Hello!')
@ When you forget to declare a variable.
Types of errors in computer programming

® When you try to use a keyword for a variable name.


This causes ‘SyntaxError: invalid syntax’ in Python, e.g.
class = ‘algebra’ #you can’t use a reserved name
@ When you have a different number of open and close brackets
in a statement
fer i in range(5): #should be 'for' not ‘fer’
print(i)
print('Edexcel Computer Science GCSE) #needs a’ after GCSE

Indentation errors are also common in Python, whilst some


languages, e.g. Visual Basic, will automatically highlight syntax
Often programs have errors errors and indent automatically.
because the programmer has
if userNum == 42:
not indented their code properly
orint('Hello!’) #should be indented
or they have inconsistent use of
new lines and whitespace. In Python you may have:
® a tab error where you have used a mixture of tabs and spaces
® an indented error where the programmer has not indented all
lines in a block equally.
Remember that the indentation only increases after a statement
ending with a colon, and afterwards the code must return to the
previous indentation.
print('Steve’)
print(‘Cushing') #should not be indented
mandi nis:
fit =—e4s
orint(‘Hello!’) #should be indented

If you put the following:


print(Hello World!)

Python will say it has a syntax error and point with * to the
exclamation point. The problem is that ! has no meaning in
Python.
The correct code would be:
print(‘Hello World!’) #correct code as in this
#instance Python would
#understand that the ! is part
#of the text

Another example of a syntax error in Python would be:


class = 'Computer Science’ #this is a syntax error because
#you are using one
#of the special words in
#Python, the word ‘class’
.

12 Testing
AN your a
code
je i nnn SS
4 “%

3
~

If you had written GCSE instead of class, there would not have
ae)
been a problem.
GCSE = 'Computer Science’ #correct code

If you are using quotes around text and you forget the second one,
or you are using parentheses and forget the second one, you will
also get syntax errors.
print(‘today) #syntax error, no ‘
print(‘today' #syntax error, no )
You may also have a name error. This happens when you have:
® misspelt a variable, function or method name
® forgotten to import a module
® forgotten to define a variable.
But if the computer gives an error message that does not contain
the word ‘SyntaxError’ it must be a runtime error.

Some program languages that are specifically designed to


! Key point introduce programming to people utilise a ‘drag and drop’ method
of writing code, where the user clicks and drags small bits of
If the rules of a language are code into the place in the program where they require it. This
broken by the program it is allows them the freedom to concentrate on creating a solution to
a syntax error. If the logical a programming problem with a robust structure without having
structure of the program the added distraction of satisfying syntax requirements.
produces unexpected results it is
a logic error. You will need to be able to find errors in your own code and in the
examination so you will need to understand how to do this.

Runtime errors
Runtime errors occur whenever the program instructs the
computer to carry out an operation that it is either not designed to
do or will not do.
Going past the last index of a list causes an error. (‘IndexError: list
index out of range’)

This error happens with code like this:


myList = ['cat', 'dog', ‘horse’]
print(myList[6]) #the maximum it can be is
#myList[2]

2 Question A typo for a method name causes a runtime error or syntax error:
myData = ‘THIS IS IN LOWERCASE:
2 What is a runtime error?
myData = myData.lowerr() #should be one r not two
Runtime errors commonly occur when programming statements
are written in the wrong order or a programmer constructs
instructions that the computer is unable to carry out.
One of the commonest runtime errors is when a program instructs
a computer to divide any number by the value zero. This operation
Types of errors in computer programming

! Key point produces an infinitely large result, which consequentially is too


large for a computer to accommodate. In this situation the
Runtime errors only appear when computer will return an error message informing the user that it
the program is running. Adding is unable to perform that operation.
numbers to strings or dividing by
a= 0
O can cause them.
orint(10 / a) #this will cause an error as no
#number can be divided by 0
title = 'Edexcel Computer Science GCSE'
2 Question
print( titel ) #should be title so shows as
3 Explain the following #a runtime error as code is
statement: #correct

Runtime Error ‘339’ Many runtime errors occur because the coder is using an undefined
component 'MCI32.0CX' or variable or function. This can also occur if you use capital letters
one of its dependencies is not inconsistently in a variable name:
correctly registered: a file is callMe = ‘Today or Tomorrow'
missing or invalid
print(callme) #runtime error, should be
#callMe

This would also be classified as a syntax error in many languages


as the compiler would stop the program compiling. In Python it is
a runtime error because the language is interpreted.
It makes no sense in mathematics to divide by zero so this also
leads to a runtime error.
print(1/0) #runtime error, cannot divide 1
#by zero
input a,b
e = a/b #obviously shows a runtime
#error when b = 0

Using operators on the wrong type of data also leads to a runtime


error, for example you cannot add text and numbers together.
orint(‘print this’ + 19) #runtime error two different
#data types but could also be
#a syntax error

Logic errors
Out of the three common errors that occur in programming, logic
errors are typically the most difficult kind of errors to detect and
rectify. This is usually because there is no obvious indication of
the error within the software. The code will run successfully;
however, it will not behave in the way it was designed to. In other
words it will simply produce incorrect results.
12 Testing your code
ne

The most common reasons for logic errors are:


! Key point
@ The programmer did not understand the manner in which the
Out of the three common errors program was meant to behave.
that occur in programming, logic @ The programmer did not understand the individual behaviour
errors are typically the most of each operation that was part of the program.
difficult kind of errors to detect ® Careless programming.
and rectify.
An example of a logic error is when a function that is supposed to
return the larger of its two arguments returns the smaller argument:
def larger(x, y):
J Key point ieee:
An argument is any piece of data return y
that is passed into a function return X
when the function is called.
With logic errors your code might run perfectly well without
crashing, but still do the wrong thing or produce incorrect results.
In mathematics, to calculate the average of a set of numbers we
add the numbers then divide by the number of numbers.
So if we have two numbers a and b, we add a + b then divide by
the number of items, in this case by 2.
But if we write:
Logic errors are when a program
works but does not give the
a=3
answer you require. For example b=4
using ‘<’ instead of ‘>’, adding average =at+b/2
when you should be subtracting print(average)
etc.
we will get an answer of 5.0 not 3.5.
To make the code work we would need to do the following:
a=3
baa 4:
average = (a+ b)/2
print(average)

Let’s look at another example of a logic error, say we want to find


the sum of 2 + 8 = 10 multiplied by a variable of 5. We could write
the following Python code:
num, =5
print(‘Result:', num * 2 + 8)

The code will run but the answer will not be what we want, we
have another example of a logic error:
Output: Result: 18

100
Testing for errors before and during the coding stages

To get the output to be what we wanted we need to add brackets:


num = 5
print(’Result:', num * (2 + 8))

Now we have a correct output, the logic error has been fixed:
Output: Result: 50
Sometimes logic errors remain undetected until an angry user
contacts the programmer to say that their program has performed
some calamitous operation such as paying all the employees of a
2 Questions company too much in their monthly salary!
4 What is meant by a logic So how can logic errors be prevented? Well, the following rules
error? Give an example of should prevent most of the errors occurring:
a logic error in your chosen @ Programmers should fully understand how a finished program
programming language. is meant to behave.
5 A programmer is developing @ Programmers should have thorough knowledge of the behaviour
a new program. Describe the of every operation that is written into the program.
types of errors they would @ Avoid careless programming.
check for. ® Programmers should implement a thorough and rigorous
testing strategy.

Testing for errors before and during the


coding stages
It is important to thoroughly test a system in order to ensure that
it is robust and not likely to malfunction. It should be understood,
however, that a program is very unlikely to work perfectly the
first time it is executed. Therefore testing is carried out on the
code to try and make it fail and reveal the presence of errors.
If software is not tested effectively in extreme circumstances the
consequences could be:
@ It could ruin the reputation of the company that has written
the program.
® It could cause an accident if the code is part of a program that
runs a system on an aircraft or within a nuclear power station
for example.
In the next section we will look at debugging, which is the
process of detecting and correcting errors during execution of the
program. However, in this section we will look at how errors can
be detected prior to running the program.

Dry-run testing
Dry-run testing is usually carried out on the algorithm which is
written in pseudo-code or as part of a flowchart. This form of
testing is usually done prior to the program code being written.
. 12 Testing your code

y 2 Question
The process involves stepping through the algorithm one
instruction at a time with purposely chosen example test data. A
6 What is dry-run testing? trace table is used by the programmer to keep track of the test
data, its purpose being to demonstrate what went wrong within
an algorithm and pin-point exactly where the problem is. It should
be understood that this form of testing is usually done on a small
scale as it is quite hard work and very repetitive.

Trace tables
A trace table is a technique used to test algorithms to see if any
logic errors are occurring whilst the algorithm is being processed.
! Key point
Within the table, each column contains a variable and each row
A trace table is a method of
displays each numerical input into the algorithm and the resultant
using data to check that a
values of the variables.
flowchart or code covers all
possibilities correctly. Trace tables are particularly popular with people who are learning
to program.

Let’s say we have a variable x which is coded as follows:


x=y+4
We could create a simple trace table for our variable x as follows:

7 Describe three methods that a


programmer can deploy to test 10 14
for errors when programming. 20 24

30 34

100 104

We'll look at this in more detail using a Python example:


ye #variable y = 2
x=2 #variable x = 2
Apes bears
Trace table:

aL 2

D 2 2

4 2

102
Testing for errors before and during the coding stages 2
“<
Now we will use this in a simple WHILE loop:
number = 1 #line 1
y=2 #line 2
x=2 #line 3
while number < 4: #line 4 start of the loop
number = number + 1 #line 5 the variable called
#number increases by 1
y=y+x #line 6 the variable y increases
#by variable x
print(y) #added so we can see the
#value of y as an output

Output:
4
6
8

Trace table:

iL 1

2 1 2
3 1 2 2
4 ii 2 2
5 g 2 2
6 9) 4 2
The main advantage of dry-run
testing is it enables programmers 4 2 4 2
to spot errors even before they 5 3 4 2
start writing code. 6 3 6 2
4 3 6 2
5 4 6 2
? Question 6 4 8 2
8 What is a trace table? 4 4 8 D

Checking code using unit testing


If a programmer only has to check the code within a small
program, they may just go literally through the program line by
line checking for errors manually. Now, this is tedious enough
with a small program and is also open to human error so you can
imagine that with large programs this process of checking code
is completely unfeasible, so professional program developers use
software tools called unit tests.
12 Testing
Cees
ee ng your
OUT code
COO

Unit testing is a popular practice that consists of writing additional


programs that test individual functions of a main program under
development. Unit tests are small pieces of code whose purpose Is
to prove the software modules are correct.
Unit testing is an important process as it is an automatic tool
that can uncover regressions — unwanted changes in previously
working code, which may have inadvertently been introduced
during development.
Like other software, there are effective and ineffective unit tests.
A poor unit test will focus on a scenario that is not relevant for the
application, whilst a good-quality unit test is one that is written
to catch cases such as a scenario where someone enters a negative
value as a salary figure.
Code coverage is a term that is used to describe the percentage of
lines of code that are covered by unit tests; however, it should be
realised that there is no relationship between the number of unit
tests applied to a program and the potential correctness of the code.

Testing for errors during the execution


of code
The process of testing a program for errors during its execution is
a cyclic activity called debugging. To debug code effectively, two
things are needed:
® The ability to test each of the instructions provided by a
program, and
@ The capability to retrieve information about:
e the results of those instructions
] Key point e any changes in the program when the tests were carried out
e the error conditions
The process of testing a program
¢ and what the program was doing when the error occurred.
for errors during its execution is a
cyclic activity called debugging. Fortunately, there are software tools that can assist in the debugging
process. These tools are called debuggers and the source code of a
program is run through these in order to detect syntax, runtime
2 Question and logic errors. The debugger produces a report on screen which
9 Briefly describe the purpose of highlights and lists the above information to the program tester.
a debugger.
Debuggers
There are a number of specific features within debuggers which
! Key point can assist the program tester in detecting errors, such as:
® breakpoints
Make sure parts of your programs ® watchers
work as you write them. Do not ® stepping.
write massive files of code before
you try to run them. Code a little, Breakpoints are breaks that can be manually inserted into code in
run a little, fix a little. order to halt the execution of the program at specific points to
allow the tester to inspect values of variables at those points.

104
SS
How do you document your tests?
SSS SSS SSS SSS SSS SSS

Usually, code on the line where a breakpoint has been inserted


will be highlighted in red or yellow. Any number of breakpoints
can be inserted into a program, although a point is reached when
there really is no reason to insert any more breakpoints.
A watcher is usually in the form of a table and displays the values
of specified fields and variables relative to the particular line that
the debugger is currently on. To add a watcher, the tester usually
types the name of the variable they are interested in within an
area of the user interface of the debugging program.
Also, expressions that require evaluation, such as x + 1 or array
accesses, can be typed in to a watcher.
Once the program is paused (say by a breakpoint), the debugger
A debugger is a piece of software allows the tester to continue the execution of the program one line
that helps a programmer track at a time — effectively stepping through the program. This gives a
down faults in a program. programmer the capability to see exactly how many variables and
objects are impacted when a particular line is executed.

How do you document your tests?


For most programs, it is practically impossible to prove that the
program is correct on all machines and systems with any input.
What you do need to do is demonstrate that your program works
as stated in the specification. You do this by producing a testing
document saying how you intend to test the program and then
showing the results of these tests.
This documentation is called a test plan, and you should provide
one with each program.
Remember to only list measurable things in your test plan.

| Test no Purpose of | Test data | Expected | Actual Action


| the test oo result result needed/
oo. ee comments

When should testing take place?


If a basic software development life cycle is considered, testing
is usually carried out between the ‘development’ period and the
application launch or handover to the client. For you this would
be before you pass your finished program to the teacher. But this
is not a good way to do it as testing should be an ongoing process
as the software is developed.
12 Testing your code

§ Types of testing
There are three types of test data:
4 Under normal conditions. The application is tested under
normal working conditions and the data that the coded solution
is supplied with is within the anticipated range.
i) Under boundary conditions. The coded solution is provided
! Key point with data that is within the operating range but at the limits of
There are three types of testing: its performance.
under NORMAL conditions, under 3 Under erroneous conditions. An application or program is
BOUNDARY conditions and under provided with data that is outside its limits of performance.
ERRONEOUS conditions. These tests try to break the application and to investigate if
things occur when they shouldn't or vice versa.

§ Choosing the test data


Before performing a test, you need to decide what data you intend
! Key points
to use to carry out the test. It is not normally possible to perform
A Extreme data is data that is tests with every single possible piece of data; developers need to
at the extreme limit of valid choose from a limited range of test data. In making this choice
or invalid to check boundary the developer considers the following:
conditions. ® valid data is the most common data that should work under
A Valid data is data used in normal conditions
testing that represents normal ® extreme data is always valid but is data on the limit of what is
data that could be expected. accepted
A Erroneous data is data ® boundary data is data that is on either side of the boundary — it
that would not normally be might be valid or invalid
expected such as the wrong ® erroneous data, which is invalid data — data that is expected to
data type. fail, for example data that is clearly the wrong data type.
A Boundary data is data that is Tests should find that the program works as expected with the
at the extreme limit of valid correct data and rejects erroneous data without crashing.
data and is often used to
For example, if you are developing a game where the user must
check boundary conditions.
select a whole number between 1 and 20, you could try a range of
Invalid or erroneous data is
inputs to see what happens:
data used io test a program
that should always be rejected 3, 14, (valid data) 1, 20, (valid extreme), the string ‘three’, -12
because it is ‘out of range’. (erroneous), 15.01111 (invalid boundary data).
A Validation is the process of Testing under normal conditions is about testing valid data that
checking data as it is input to
should be accepted.
ensure that it is reasonable.
Testing boundary behaviour involves being subjected to tests
on the borderline of failure but within the acceptable range to
understand how the software behaves. With limits of 0 and 20
being subjected to tests we need to explore values such as 1 and
20. Testing invalid extreme behaviour involves being subjected to
tests on the borderline of failure, but just outside the acceptable

106
ee Choosing the test data
ee ne Re ERNE

range to understand how the software behaves. With limits of 0


and 20 being subjected to tests we need to explore values such as
For the examination you will need 0.5 and 20.5.
to be able to design and use ;
test plans and test data (normal, Lastly, testing erroneous or invalid behaviour involves the
boundary, erroneous). variable with limits of 0 and 20 being subjected to tests where the
returned results should be clearly wrong to understand how the
software behaves.

Chapter review

In this chapter we have explored types of errors and how to test your
code.
We also explored test plans and how to record your tests.
Remember, before tackling any computer science task or examination
question on this topic you must:
® Be able to differentiate between types of error in programs including
logic, syntax and runtime errors
® Be able to design and use test plans and test data (normal, boundary,
erroneous)
® Be able to interpret error messages and identify, locate and fix errors
in a program
® Be able to determine what value a variable will hold at a given point in
a program (trace table)
® Be able to determine the strengths and weaknesses of a program and
suggest improvements
® Understand how to identify and correct errors in algorithms
® Be able to evaluate the fitness for purpose of algorithms in meeting
specified requirements efficiently using logical reasoning and test
data.
13 Algorithm efficiency

cs Specification reference

You should:
1.1.9 be able to evaluate the fitness for purpose of algorithms in
meeting specified requirements efficiently using logical reasoning
and test data

In the first chapter you learnt that there are many solutions to the
same problem and how algorithm efficiency becomes important.
Some algorithms are more efficient than others. It is obviously
better to have an efficient algorithm.
We explored briefly how there are two main measures of the
efficiency of an algorithm: time and space.
Sometimes it’s possible to solve a problem by using what is called a
brute force technique. Yousimplytry out all the possible combinations
of solutions. If you had to sort words with 158 characters when
combined together including spaces, and you could compute
1,000,000,000 possibilities a second, you would still be left with the
need for over 1 x 10149 seconds, which is longer than the expected
life of the universe. So to reduce the time and make the algorithm
more efficient, it is necessary to find a better approach.
Time measure is a function describing the amount of time an
Some algorithms are more algorithm takes in terms of the amount of input to the algorithm.
efficient than others. It is ‘Time’ can mean the number of memory accesses performed, the
obviously better to have an number of comparisons between integers, the number of times
efficient algorithm. some inner loop is executed, or some other natural unit related to
the amount of real time the algorithm will take.
Space measure is a function describing the amount of memory
(space) an algorithm takes in terms of the amount of input to the
algorithm. We often speak of ‘extra’ memory needed, not counting
the memory needed to store the input itself.

108
Comparison

§ Comparison
If we compare three simple algorithms for computing the sum 1
+2+... +m for an integer n > 0 you will see that even a simple
program can be programmed to work faster.

sum = 0 sum = 0 sum =n * (n+1)/2


forx=1ton forx=1ton
sum = sum + x fory = 1 tox
sum
= sum + 1

Too many nested loops in any program can make the program
take a long time to execute. Steps such as storing the length of an
array in a different variable, instead of making it read the length
at every iteration of the loop, will help to optimise the code. It also
ensures that the code runs more efficiently.
For the examination you will
Restructuring your code (called refactoring) without changing its
need to be able to determine
external behaviour can also help.
the strengths and weaknesses
of a program and suggest For example, instead of this:
improvements. TOMX Ima:
for y in b:
x= Vy:
yield (x,y)

you could write:


return set(a) & set(b)

Chapter review

In this chapter we have explored algorithm efficiency.


Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain how different algorithms can lead
to faster execution and thus be more efficient
® Be able to evaluate the fitness for purpose of algorithms in meeting
specified requirements efficiently using logical reasoning and test
data.
14 Comparing pseudo-code,
flowcharts and code

1.1.4 understand how to determine the correct


© You should:
output of an algorithm for a given set of data
1.1.1 understand what an algorithm is, what
1.1.6 understand how to code an algorithm in a
algorithms are used for and be able to interpret
high-level language
algorithms (flowcharts, pseudo-code, written
descriptions, program code) 1.1.7 understand how the choice of algorithm is
influenced by the data structures and data
1.1.2 understand how to create an algorithm to
values that need to be manipulated
solve a particular problem, making use of
programming constructs (sequence, selection, 2.2.2 be able to use Sequencing, selection and

iteration) and using appropriate conventions iteration constructs in their programs


(flowchart, pseudo-code, written description, 2.2.3 understand the need for, and how to
i draft program code) manipulate, strings
| 1.1.3 understand the purpose of a given algorithm 2.4.1 understand how to write code that accepts and
and how an algorithm works responds appropriately to user input

— Recap: pseudo-code, flowcharts and Python


To recap on what you have now learnt in earlier chapters and to
reinforce this learning it will help if we look at some examples
that use pseudo-code, flowcharts and Python for the same
action.
First we need to explore the inputs and outputs from a variable x.

RECEIVE x (STRING)FROM KEYBOARD _ x = input()

RECEIVE Name
FROM (STRING)
KEYBOARD

110
Recap: pseudo-code, flowcharts and Python

Let’s compare how we show output from a variable using


flowcharts, pseudo-code and Python:
Flowchart | Pseudo-code
SEND x TO DISPLAY print (x)

SEND
<expression>
TO DISPLAY

To fully understand loops we now need to compare how we


would show the IF...THEN condition in flowchart, pseudo-code and
Python form:

Flowchart : = Pseudo-code = == = : = Python 3 =


IF x < 5 THEN if x < 5:
SEND 'Well done' TO DISPLAY print('Well done’)
ENDIF

“SEND ‘Well
done’ TO ©
DISPLAY

In most instances we want this decision to lead to


different outcomes and we will use the ELSE statement; for
example, a decision flowchart adding interest at different
rates according to the amount you have in a bank account
could look like Figure 14.1.
The pseudo-code for this same action would be:
IF amount < 1000 THEN
interestRate — .06 #the ‘yes’ or ‘true’ action
BISSE
interestRate — .10 #the ‘no’ or ‘false’ action
ENDIF

Figure 14.1 Flowchart demonstrating ELSE


14 Comparing pseudo-code, flowcharts and code

Now let’s look at a coded example of this same decision concept


in Python:
if name == ‘Steve’: #you will notice the use of == in this
#coded example
#this is the equality operator used in Python
orint('You have such a cool name!’)
else:
1 Produce a simple coded print(‘Not a bad name, but | think the coolest name is
solution in your chosen Steven)
pe URE ne eee (ol So in the code we have two options. If the entered name is Steve
Ae 0 dey Be the program prints one string, if it is anything else it prints a
and for a simple message second string.
to be printed using the data
entered. Now look at a further example to compare the IF...THEN...ELSE
condition in flowchart, pseudo-code and Python form:

= : Pseudo-code_ Python 3 __
IF x < 5 THEN ifx <5:
SEND ‘Yes’ TO DISPLAY print('Yes')
ELSE else:
SEND ‘No’ TO DISPLAY print('No')
“SEND ‘Yes’ / END IF

__TO DISPLAY

Let’s say you wanted something printed 100 times. By using


! Key point
a loop statement, you don’t have to code the print statement a
lteration is the name given to hundred times; you simply tell the computer to display a string
the repetition of a sequence of that number of times.
computer instructions a specified
In Python the loop statement can be written as follows:
number of times or until a
condition is met. count = 0
while count < 100:
print((Computer Science is great fun!')
count = count + 1

112
Recap: pseudo-code, flowcharts and Python

So what is happening here? Let’s look at a simple flowchart for


the same concept:

print ('Computer Science is great fun.')


count '= count. + 1. 2 he cbs

End
Figure 14.2 A counting loop

While the count is less than 100, the program will continue to
print and add 1 to the variable called count. Once it reaches 100
it will stop. It is worth noting here that we can have a pre-
condition loop where the code checks the condition first and
Indefinite iteration is the name
only runs the loop if the condition passes. The loop will not run
given to the repetition of a
if the condition starts false. We can also have a post-condition
sequence where the number of
loop where the condition is checked at the end of the loop each
repetitions is unknown.
time it runs. A post-condition loop will always be executed once,
and if the condition is false at the end of the loop it will be
repeated again.
Comparing the WHILE loop condition in flowchart, pseudo-code
and Python form will look something like this:

a WHILE x < 5 while x < 5:


DO PROCEDURE() do_procedure()
ENDWHILE
5. 14 Comparing pseudo-code, flowcharts and code
, eee

4 Remember: "5
ae @ WHILE is a pre-condition loop
@ FOR is a pre-condition loop
@ DO...WHILE is a post-condition loop
@ REPEAT...UNTIL is a post-condition loop.
In pseudo-code you use the following syntax where you want a post-
condition loop. Execute <command> until <condition> is true. The loop
must execute at least once. The red brackets < > are only to show
where you add something — you don’t need to put them in your code.
Syntax:
REPEAL
<add command here>
UNTIL <add expression here>
Example:
REPEAT
Sel correo |
UNTIL Go = 10
SEND 'X' TO DISPLAY
END REPEAT

A common programming error involves infinite loops (i.e. the loop runs
forever). If your program takes an unusually long time to run and does
not stop, it may have an infinite loop. If you run the program from the
command window, press CTRL+C to stop it.

We can also control a loop with user input.


In pseudo-code:
REPEAT
SEND ‘Enter a number from 1 to 4'
TO DISR LAY #asks user to enter a number
GET num FROM KEYBOARD #the number entered is stored
#in the variable num
ry SEND adi TOLD ISPEAY #prints the number entered on
e Question #screen
4 What is an infinite loop? UNTIL num = 4 #this will keep repeating the
#question until the number 4
#is entered

The following Python code creates a simple guess the number game.
import random #import module
guesseslaken = 0 #setting our counter to 0
print(‘Hello! What is your name?’)

114
Recap: pseudo-code, flowcharts and Python

myName = input() #user to input their name


number = random.randint(1, 20) #selecting the number from 1-20
#we Can increase or decrease
print(‘Well, ' + myName + ', | am thinking of a number
between 1 and 20.')
while guessesTaken < 6: #note how we have a similar
#count this time called
#guesseslaken
print('Take a guess.’ #there are four spaces
in front
#of print
guess = input()
guess = int(guess)
guesseslaken = guessesTaken + 1
if guess < number:
print(‘Your guess is too low.') #there are eight spaces in
#front of print
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken) #the variable is being
#converted to a string here
#for use later in the program
print(‘'Good job!')
if guess != number:
number = str(number)
print('Nope. The number | was thinking of was:')
print (number)

In pseudo-code you use the following syntax for a count-controlled


loop. The number of times <command> is executed is determined
by the expression. The red brackets < > are only to show where
you add something — you don’t need to put them in your code.
Syntax:
REPEAT <add expression here> TIMES
<add command here>
END REPEAT
Example:
REPEAT 50-Number TIMES
SEND 'X' TO DISPLAY
END REPEAT
14 Comparing pseudo-code, flowcharts and code

As you can see, while loops and if statements are an essential part
of programming.
In pseudo-code:
WHILE myVar <= 60 #if value of myVar is less than
#or equal to 60
SEND myVar TO DISPLAY #prints the value of myVar on
#screen
SET myVar TO myVar + 1 #adds 1 to the value of myVar
ENDWHILE
Awhile statement is also often called an iterative/iteration (looping)
control statement as it repeatedly executes a set of statements that
are based on a Boolean expression or condition.
As long as the condition of a while statement is true, the statements
within the loop are (re-)executed. Once the condition becomes
false, the iteration terminates and control continues with the first
statement after the while loop.
In pseudo-code you use the following syntax for a count-controlled
loop. The number of times <command> is executed is determined
by the expression. The red brackets < > are only to show where
you add something — you don’t need to put them in your code.
Syntax:
REPEAT <expression> TIMES
<command>
END REREAR

Example:
REPEAT 100-Number TIMES
SENDY toqjOrDISPUAY
END REPEAT

Even number count


Say we want to write pseudo-code that will count all the even
numbers up to 5 numbers.
The given criteria are:
@ The user wants to see the first 5 even numbers starting from 0.
@ We know that even numbers are 0, 2, 4, etc. and the first 5 even
numbers are 0, 2, 4, 6, 8.

116
Averages, highest and lowest number algorithm

So the pseudo-code using a condition controlled


loop could be:
GET count (INTEGER) FROM KEYBOARD
SET even TO 0
Sel xe0O.0:
WHILE(x < count)
SET even TO even + 2
Soc Uwe a1
SEND even TO DISPLAY
ENDWHILE

Or if we look at the while loop part of this same


problem as a flowchart, see Figure 14.3.
In pseudo-code you use the following syntax for
a count-controlled loop. It executes <command> a
fixed number of times. The red brackets < > are
only to show where you add something — you
don’t need to put them in your code.
Syntax:
Figure 14.3 FOR <id> FROM <expression here> TO
<expression here> DO
<add command here>
END FOR
A count-controlled loop iterates
a specific number of times. Example:
In Python you use the FOR FOR Index FROM 1 TO 10 DO
Statement to write a count- SEND ArrayNumbers[Index] TO DISPLAY
controlled loop. END FOR

Averages, highest and lowest


number algorithm
Say we want to work out an average of five numbers and to sort through
the data to find the smallest (minimum) and largest (maximum) of the
An algorithm is a sequence of five user-entered numbers. We then want to write the results found
steps and decisions to solve a with a message describing what they are.
problem.
14 Comparing pseudo-code, flowcharts and code

We could write:
SEND ‘please enter 5 numbers' TO DISPLAY _ #string asking i
#the user to enter
#numbers

GET n1,n2,n3,n4,n5 FROM KEYBOARD #the variables


#being defined by
#the user input

SEND 'The average is' TO DISPLAY #string being


#printed to screen

SET avg TO (n1+n2+n3+n4+n5)/5 — #process for working out the


#average

SEND avg TO DISPLAY #output the variable (avg) value


#to screen
IF(n1 < n2) THEN #decision loop process to find
#smallest number
SE imax Omn2
BEE
SElrnax hO«ni
lF(n3 = max)
SEenraxc TO-ns
iF(n4 > max)
SE Tmax TO n4
IF(n5 > max)
SEipmaxi@ nd
SEND Ihemnax iss © DISPEAY #string
SEND imax T@mDISPEAY #output the value of the
#variable (max)
#showing maximum number
aa) 0) #process to find lowest
#number
SET mina © nZ
EES
SE min Cunt
Ne (preh< eania))
SE TatoiinmliOun3
IF(n4 < min)
SET min TO n4
IF(n5 < min)
SET min TO n5
SEND 'The min is' TO DISPLAY #string
SEND min TO DISPLAY #output the variable value
#(min) to show minimum
#number

118
SSS
Nested loops
SSS SSS

In pseudo-code you use the following syntax for a count-controlled


loop using a step. The red brackets < > are only to show where
you add something — you don’t need to put them in your code.
Syntax:
FOR <id> FROM <add expression here> TO <expression
here> STEP <expression here> DO
<add command here>
END FOR

Example:
FOR Index FROM 1 TO 500 STEP 25
DO SEND Index TO DISPLAY
END FOR

§ Nested loops
It is time to explore what are called nested loops. They consist of an
outer loop and one or more inner loops. Each time the outer loop is
Nested loops consist of an outer repeated, the inner loops are re-entered and started again as if new.
loop and one or more inner
loops. Each time the outer loop
is repeated, the inner loops are
re-entered and started again as
if new.

Figure 14.4 Nested loop

Let’s look at another example of a nested loop, this time using


Python. We will explore these concepts in further detail later in
the book, for now we just want to explore the concept behind the
code and its use. First we need to imagine a few students all taking
different subjects for their GCSEs.
14 Comparing pseudo-code, flowcharts and code

students = [(‘Sally', ['CompSci', 'Physics’]),


(‘Robert’, [‘Art', 'CompSci', 'Stats’]),
(‘Charlotte’, ['CompSci', ‘French’, 'Economics’}),
(‘Steve', ['CompSci', ‘French’, ‘Economics, ‘CommLaw']),
( Carole’, ['Sociology’, ‘French’, ‘Law’, ‘Stats’, ‘Music’])]

We have assigned a list of five students called elements in


programming to a variable called students.
We could print out each student name, and the number of subjects
they are enrolled for:
#print all students with a count of their courses
for (name, subjects) in students:
print(name, ‘takes’, len(subjects), 'GCSEs’)

Python would give us the following output:


Sally takes 2 GCSEs
Robert takes 3 GCSEs
Charlotte takes 3 GCSEs
Steve takes 4 GCSEs
Carole takes 5 GCSEs
Let’s say we want to ask how many students are taking Computer
Science, ‘CompSci’.
This needs a counter, and for each student we need a second
embedded loop that tests each of the subjects in turn:
#count how many students are taking CompSci
counter = O
for (name, subjects) in students:
for s in subjects: #a nested loop!
ifs == 'CompSci':
counter += 1
print('The number of students taking CompSci is', counter)
The output will be:
The number of students taking CompSci is 4
The next nested loop is for a game. Suppose we want to design a
game with a menu on the screen where a user can select to drive
; Key point one of three types of car:

A menu is an element displayed 1 Porsche


on the screen that gives the user 2 Mercedes
a choice of options.
oO ahord

120
|
NE STE NestedMNOOPS
loops
|

We must pay particular attention to where the IFs end. The nested
IF must be completely contained in either the IF or the ELSE part of
the containing IF. Watch for and line up the matching END IF.
GET gameNumber (INTEGER) FROM KEYBOARD
IF gameNumber = 1 THEN
Car = ‘Porsche’
ELSE
IF gameNumber = 2 THEN
Car = 'Mercedes'
EOE
Car = 'Ford'
END IF
END IF

As a flowchart this would be:

= Porsche =:

ce Car = Ford
Mercedes

Figure 14.5 Nested selection


14 Comparing pseudo-code, flowcharts and code

As the code gets more complex you will realise how important it is
to work out what we need before starting to program. Flowchart
symbols and pseudo-code are the building blocks needed to
express any desired logic in the form of a computer program.
In Python we can use all the following list functions for our list (L):
lo= [1-2 Seas) #populates a list
Using flowcharts or pseudo- L[n] #element n (where n = 0, 1, 2, 3, ...)
code we can easily describe the L[O:n] #the first n elements
purpose of a given algorithm and
L[-n:] #the last n elements
explain how a simple algorithm
eA 7, © #substitute
works.
del L[n] #remove element with index n
L.remove('x’) #removes item x from the list
! Key points L.append(x) #appends x to end of L
4 Functions should be explainable L.remove(x) #removes first occurrence of x from L
in one sentence that doesn’t
L.pop() #returns and removes the last item in the list
contain AND or OR.
L.sort() #puts the list in order
4 You can use functions in your
L.count(x) #returns the number
programs again and again.
eile #does L contain x?
4 A function has three parts: a
name, parameters and a body. L.index(x) #returns index of the first occurrence of x

. Derived structures
e Question
As we have already seen, flowcharts and pseudo-code can be
2 Write a program in pseudo-
represented by basic structures. However, sometimes it is useful
code to take four input values
to use some additional structures, each of which can themselves
and output:
be constructed from the above structures. These are sometimes
4 the total value called derived structures, which are simply structures constructed
4 the highest value from combining other pre-existing structures. Derived structures
4 the lowest value allow a programmer to group different kinds of data or parts of
A the average. code that belong to a single entity.
Choosing the best structure is based upon a few rules:
@ Are you repeating or copying/pasting code you have already
used?
Produce a DO...UNTIL e Which way uses the fewest lines of code? Normally less is
flowchart to show how to make better.
a breakfast of tea and toast. @ There are times when using more lines of code is more efficient
but you should never repeat yourself when coding.

122
Derived structures

Chapter review

In this chapter we have explored loops in more detail and also compared
flowcharts, pseudo-code and Python to see how similar loops would be
shown using the correct syntax.
Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain what an algorithm is, what
algorithms are used for and be able to interpret algorithms
(flowcharts, pseudo-code, written descriptions, program code)
(More on this topic in Chapters 1, 2, 3, 6, 7, 8, 5, 11, 10)

™® Understand and be able to create an algorithm to solve a particular


problem, making use of programming constructs (Sequence, selection,
iteration) and using appropriate conventions (flowchart, pseudo-
code, written description, draft program code) (More on this topic in
Chaptelsi2534 0.1, 6.0, 1040)
® Understand and be able to explain the purpose of a given algorithm
and how an algorithm works (More on this topic in Chapters 2, 3, 4, 6,
1, 8, See)
® Understand and be able to code an algorithm in a high-level language
(More on this topic in Chapters 7, 6, 8, 5, 11, 10)
® Understand and be able to explain how the choice of algorithm is
influenced by the data structures and data values that need to be
manipulated (More on this topic in Chapters 18, 6, 7, 8,5, 11, 10)
™® Be able to use sequencing, selection and iteration constructs in your
programs (More on this topic in Chapters 3, 4, 6, 7, 8, 5, 11, 10)
® Understand and be able to explain the need for, and how to
manipulate, strings (More on this topic in Chapters 6, 7, 8, 14, 5,
115810)
® Understand and be able to write code that accepts and responds
appropriately to user input (More on this topic in Chapters 2, 3, 6, 7,
S, 5 120): et
15 The language computers
actually use

co Specification references

You should:
4.5.1 understand what is meant by high-level and low-level
programming languages and understand their suitability for a
particular task
understand what is meant by an assembler, a compiler and an
interpreter when translating programming languages and know
the advantages and disadvantages of each

Now we know some basics about computational thinking, algorithms


and the number system computers use we need to understand how
Data is information, such as text, computers work and how they handle data. Raw data is unprocessed
| symbols, and numerical values. data and refers to a collection of numbers and characters. Data can
be something simple and can seem random. When data is processed,
organised, structured or presented in a given context that makes it
useful, it is called information.
ie petipanepoints
" § Key :
sh | Machine code
A In computing, a loader is part
of the operating system and Although you are learning a programming language using numbers
is responsible for loading and characters that are used in common English, computers only
programs and libraries. understand machine code. Computers do not understand our language
A Machine code is a set of which uses words and letters. But machine code, while easily
instructions that a computer understood by computers, is almost impossible for humans to use
can execute directly. Machine because it consists entirely of numbers.
code is written in a binary The code the loader uses to add two numbers will look something
code, and each statement like this:
corresponds to one machine
A9 00 FE 49 00 FF 91 00 22 F1 00 FA D1 00 FE 91 00 22 71
action. 00 FA Al 00 20 F1 00 1F 51 00 1F 00

§ High-level and low-level languages


You may be wondering how a computer will understand your
programming if it can only read machine code made up of ones
and zeros and you are using words and numbers. It is similar to
you speaking English but the person you are giving instructions
to only speaks a foreign language. To enable the computer to
understand us we use another form of abstraction, and a compiler or
15 The language computers actually use

interpreter. First we write our code in a format we can understand.


We say that this is a high-level language. Our language is different
from the low-level language due to abstraction. A low-level
programming language is one that provides little or no abstraction
from a computer’s instruction set; a high-level language is one a
human can easily understand.

] Key points
4 Acomputer’s CPU can only understand instructions that are written in
machine code.
A A compiler is a computer program that translates C, C++, BASIC,
Pascal, and similar high-level programming languages into machine
language.
A An interpreter is a program that executes a source program by reading
it one line at a time and doing the specified operations immediately.
A A high-level language is a computer programming language designed
to allow people to write programs without having to understand the
inner workings of the computer.

You may be a little confused here as we used the same term


abstraction when we were looking at computational thinking
For the examination you will in the first chapter. We identified the problem by removing
need to understand what is unnecessary information and data. Abstraction here is similar — it
meant by high-level and low-level is the process of extracting the underlying essence of a concept,
programming languages and removing any dependence on the low-level code needed by the
understand their suitability for a computer. It refers to the distinction between the properties of
particular task. an entity and its internal needs. It is abstraction that allows us to
ignore the internal details of a complex device such as a computer,
car or microwave oven and just use it. To drive a car you don’t
need to know how the steering wheel turns the wheels. You just
need to know that it does. This is also an abstraction.

! Key point i Assembly language


The internal, logical structure Assembly language is at the level of telling the processor what to do;
and organisation of the computer you can’t get much more low level than that although machine
hardware is called the computer code is lower level than assembly.
architecture.
The word ‘low’ refers to the small or nonexistent amount of
abstraction between the language used and machine code.
Because of this, low-level languages are often described as being
‘close to the hardware’. This code is what the hardware uses.
A low-level language does not need a compiler or interpreter to
run, the processor for which the language was written is able
to run the code without using these to translate the code into
something it understands.

126
Different languages and their level of abstraction

If we look at an example of assembly language to add two numbers


together you will understand this:
.model small
data
oprl dw 1234h
opr2 dw 0002h
result dw 01 dup(?),'$'
.code
mov ax,@data
mov ds,ax
mov ax,opr1
] Key points mov bx,opr2
cle
4 Machine code and assembly
code are low level. Everything add ax,bx
else is high level. mov di,offset result
A Assembly language is a low- mov [di], ax
level programming language. mov ah,O9h
It uses mnemonic codes and
mov dx,offset result
labels to represent machine-
int 21h
level code. Each instruction
corresponds to just one mov ah,4ch
machine operation. int 21h
end

2 Questions Different languages and their level of


1 Briefly describe the term high- abstraction
level language.
Let’s explore a few different programming languages to see how
2 What are the advantages they differ in levels of abstraction.
of writing in high-level code
compared to machine code? C
C is a step higher up from assembler language, because you get to
specify what you want to do in more abstract terms, but you're still
fairly close to the low-level language needed. C++ does everything
that C can do but adds the capability to abstract things into classes.

Caras
Advantages of C++:
@ C++is best suited for general-purpose and low-level programming.
@ C++ is extremely fast —it works very well for GUI programming
on a computer.
® C++ is a good language to write operating systems, drivers and
platform dependent applications with. It is a good language to
learn to program with.
@ C++ is a good language for engineers.
15 The language computers actually use

Disadvantages of C++:
e Although it is platform independent, C++ is mostly used for
platform-specific applications.
@ C++ is overly complex for very large high-level programs.
@ C++ is difficult to debug.
Java and C# do similar things to C++, but without the opportunity
to do everything you can do in C (like pointer manipulation in
Java’s case).

C#
Advantages of C#:
® C# can be used for web applications on Microsoft computers.
@ C# works well with the Microsoft product line but can be compiled
on OSX/Linux as well.
Disadvantages of C#:
® C# was originally developed largely for the Microsoft platform.

Java
Advantages of Java:
® A Java program can run on any computer.
@ Java can also run on most cell phones.
@ Java is very robust. Robust means reliable and no programming
language can really assure reliability.
® Java is simple: Java was designed to be easy to use and is
therefore more easy to write, compile, debug and learn than
other programming languages.
® Java uses automatic memory allocation and garbage collection.
® Java is object-oriented because programming in Java is centred on
creating objects, manipulating objects and making objects work together.
This allows you to create modular programs and reusable code.
® Java can be distributed and involves several computers on a
network working together.
®@ Java is one of the first programming languages to consider security
as part of its design. The Java language, compiler, interpreter and
runtime environment were each developed with security in mind.
® Java is multi-threaded: this means Java programs have the
capability to perform several tasks simultaneously.
Disadvantages of Java:
® Java is a lot more complicated than C.
® Java is slow, not only slow to run, but also slow to develop.
@ Java works best for high-level enterprise web applications.
® An interpreter is needed to run Java programs. Programs are
compiled into Java virtual machine code called bytecode.
® Java can be significantly slower and more memory-consuming
than natively compiled languages such as C or C++.

128
Different languages and their level of abstraction

Python
Python/Ruby are higher level as they let the programmer forget
about a lot of the details they would need to specify in something
like Java or C#.
Advantages of Python:
@ Python, like the other languages mentioned, has the advantage
of being a high-level language. It is good for rapid prototyping.
® Python code has to be strictly indented but the indentation
helps to make it much easier to read.
® Python is easier to learn than Java or C.
© Python is easy to write, easy to read and easy to understand.
® Python is a little more general than other languages; it does
pretty much everything: web, stand-alone GUI, graphics,
mobile, quick scripts, etc.
@ Python does not use any syntax; instead tabbing and spacing
play an important role in program flow.
@ With the absence of syntax, developers wishing to use Python
need not learn any new rules.
® Python is very robust because of its lack of syntax. Users with
little to no experience with Python can quickly determine
program functionality and begin re-factoring code for upgrades
or bug fixes.
@ Python does not enforce a strict type on containers or variables.
® The integration of Python with languages such as Java and C/
C++ allows Python to remove some stress from the interpreter.
Disadvantages of Python:
® Python is an ‘interpreted language’ and C++ is a ‘compiled
language’. Because of this Python code is slower than C++ code.
@ Python programs also run slower than Java codes.
® Python doesn’t really do multi-processor/multi-core work very
well.
e@ Python adds the overhead of interpretation to the runtime of
the program which can lead to a slower runtime. It is estimated
that Python runs 1-5 times slower then Java or C/C++.
® Because of its easy syntax Python is an easy language to
program in; however it’s not very simple to translate a Python
program into any other language.
15 The language computers actually use

] Key points § Assemblers, compilers and interpreters


A Binary is a system of numbers As you will know by now computers can only understand
using only two digits 0 and machine-level language (binary, 0 and 1).
1 (also called the base-2
You also know that it is difficult to write and maintain programs
system), unlike the decimal
in low-level machine code. Programs written in high-level
(or denary) system in everyday
languages and some low-level languages need to be converted
use that uses O to 9 (base-10).
into machine-level language using translators.
A A translator is a program
to convert high-level or Translators are just computer programs which accept a program
assembly-level commands into written in a high-level or low-level language and produce an
machine code. equivalent machine-level program as output.
4 A compiler is a computer Translators are of one of three types:
program that translates C, ® assembler
C++, BASIC, Pascal and ® compiler
similar high-level programming ® interpreter.
languages into machine
language.
Converts the program into machine code Converts all the code of the
one statement at a time or calls a routine program into machine code at the
in its own code to execute the command. same time.

Takes less time to analyse the source Takes a large amount of time
code, but the overall program execution to analyse the source code but
time is slower. the overall execution time is
comparatively faster.
! Key point
Memory efficient. Requires comparatively more
For the examination you will need memory.
to understand what is meant by Continues translating the program until the Generates the error message
a compiler and an interpreter first error is met, in which case it stops. after searching all the errors of
and know the advantages and This makes debugging easier. a program and then listing them.
Hence debugging is comparatively
disadvantages of each. harder.

Machine code and assembly language


Machine code is the only form of program instructions that the
computer hardware can understand and execute directly. It’s the
basic language that all the computer’s instructions will be in.
All other forms of code must be translated into machine code in
order to be executed by the computer’s hardware. Machine code
is represented by using binary. We say ‘binary’ because machine
language is a base-2 numbering system (made up of 0s and 1s),
and ‘bi’ means two.
Machine code is different for each type of computer. A program
in machine code for a PC will not run on another system using a
different processor.

130
Assemblers, compilers and interpreters

Assembly language is the next step up, using specific keywords


and terms. Assembly language is a low-level language that is
specific to the architecture of the CPU. Assembly language
is therefore a representation of machine code using symbols,
not the machine code itself. Because machine code is specific to
each type of computer hardware, assembly languages are also
specific to each type of computer. However, all machine languages
and assembly languages look very similar, even though they are
not interchangeable.
The use of easy-to-understand representations rather than binary
helps programmers to write programs in machine code without
having to deal with very long binary strings. The machine code
for an instruction to add two numbers could be 01101110, but in
assembly language this can be represented by the symbol ADD.
! Key points |
A high-level language is a language that is convenient for human
A An assembler is a program
beings to understand. High-level programming languages must
that translates assembly
be translated into machine code for execution. This process is
language programs into called compilation if a compiler is used.
machine code.
4 Assembler directives are
An assembler is used to assemble the code of some low-level
instructions to the translating
language into machine-level language. An assembler translates
programs. each instruction in the source program into a single machine code
instruction.

Compilers and interpreters


Compilers and interpreters are used to convert the code of high-
level languages into machine code.
Although both compilers and interpreters perform essentially the
same task, there is a difference in the way they work.

e Question An interpreter checks the errors of a program statement by


statement. After checking one statement, it converts that statement
3 What is a low-level language? into machine code or calls a routine in its own code to execute
the command and then executes that statement. The process
continues until the last statement of the program occurs. Rather
than producing a machine-language copy of a program that will
be executed later, an interpreter actually executes a program from
its high-level form.
Compiled languages are normally faster and more efficient than
interpreted languages during execution. Systems that require high
1 Use this book and other
performance are therefore often created in a compiled language.
sources such as the internet
On the other hand, interpreted languages are easier to write code
to research into how assembly
in. Most interpreted languages have built in abstractions and
languages are used now, what
extensive libraries that can be used to help the programmer write
they are used for and why.
the code.
15 The language computers actually use

§ Writing in low-level and high-level languages


There are both advantages and disadvantages of writing in low-
level languages. Low-level languages allow you to directly interact
! Key point with the hardware and memory. Since you are writing at machine
Assembly language is also often level you control the level of performance of the code, whereas when
used in embedded systems. writing in a high-level language you have to rely on the compiler to
optimise your code. By using pointers, you can directly access and
set the value of addresses in memory. However, low-level languages
are very hard to read or learn. They are procedural and can even
require more lines of code. There is also less support than for high-
level languages, in both development and debug environments.
When writing things like device drivers, it’s easier to communicate
with hardware devices and I/O ports in a low-level language, as
not all high-level languages support low-level I/O.
High-level languages have a major advantage over machine and
assembly languages as they are similar to the language we use every
day of our lives. This makes high-level languages easier to learn and
use. High-level languages are also problem oriented because the
instructions are suitable for solving a particular problem.

L Hardware and software interaction


Understanding machine code on its own is not enough; you need to
explore the interaction between code (programming language) and
databases/text-files, prototyping, software development, computer
models and websites. It is also important to understand the
interactions between the code and the developments in hardware.

Chapter review

In this chapter we looked at machine code and high- and low-level


programming.
We also explored different programming languages.
Remember, before tackling any computer science task or examination
question on this topic you must:
™® Understand and be able to explain what is meant by high-level and
low-level programming languages
® Understand the suitability of a high-level language for a particular task
® Understand and be able to explain the terms compiler and interpreter
® Understand the advantages and disadvantages of using compilers
and interpreters
® Understand how compilers and assemblers translate programming
languages.

132
16 Binary and hexadecimal numbers

Specification references
: You should: 3.1.3 be able to convert between binary and denary
) 3.1.1 understand that computers use binary whole numbers (0-255)
to represent data (numbers, text, sound, 3.1.4 understand how to perform binary arithmetic
= graphics) and program instructions (add, shifts (logical and arithmetic)) and
= 3.1.2 understand how computers represent and understand the concept of overflow
manipulate numbers (unsigned integers, 3.1.5 understand why hexadecimal notation is used
signed integers (sign and magnitude, two’s and be able to convert between hexadecimal
complement)) and binary

§ Ones and zeros


The central processing unit Every central processing unit (CPU) has its own unique language
(CPU) is the electronic circuitry (called machine code). Programs must be rewritten or compiled
=, within a computer that carries to work on different types of computers. We will explore machine
P| out the instructions of a code in a little more detail in the next chapter but first we will
-| computer program. look at how the system functions.
reece Have you ever wondered why switches have a 1 for on and a 0 for
off? It’s based on binary computer code. 1 means ‘on’ and 0 means
‘off’. Everything a computer does is based on ones and zeros.
Imagine the computer is made up of switches, and each switch
controls a light which can either be On or Off, One or Zero.

Each sequence of On-and-Off lights could represent a different


number. Let’s imagine that we have two lights, each with their
own switch. They could be:
e Both Off
® First Off, Second On
® First On, Second Off
@ Both On

Figure 16.1 On/off symbols Binary code takes each of those combinations and assigns a
number to it, like this:
@ Both Off = 0
e First Off, Second On = 1
All data that is stored in a
@ First On, Second Off = 2
computer is converted to
@ Both On=3
sequences of Os and 1s.
16 Binary and hexadecimal numbers

Maybe you are thinking that it would take rather a lot of switches
and lights to make a computer work like this, but if we had six
lights, and they were like this:
On On Off On Off Off

On On
Figure 16.2 Bulbs on or off

and rather than just giving a light just one score, if we said that
different lights in the sequence had different scores: the first light
32, the second 16 then 8, 4, 2, 1, then the value of those six bulbs
(called a point value) would be:
32+ 16+0+4+0+0 (remember, we only give points if they’re
turned ON!)

and that adds up to 52. So we would say the sequence of lights is


worth 52, but we would write it as: 110100

Humans use a different number system to computers as they


use what are called decimal (or denary) numbers. Don’t worry
about the name, it just means that our numbers have a base of 10.
Many people believe that we have a base-10 system because we
The number of different bit
originally learnt to count using our ten fingers. A base of 10 just
patterns that can be formed
means that it has 10 digits. Humans use the numerals 0, 1, 2, 3, 4,
using 2 bits is four.
5, 6, 7, 8, 9 but computers do not count this way.
Unlike humans, computers do not have ten fingers; computer memory
is based on transistors which have only 2 states, on or off (based upon
? Questions
base voltage low or base voltage high). Each transistor is an on/off
1 What is decimal numbering? switch. But computers have millions of transistors. Maybe humans
2 Describe the term binary. use the decimal system because we have two hands and ten fingers
to count on; the computer may only have one finger but it does have
the equivalent of lots and lots of hands (the transistors).

§ Binary numbers
Even though computers are based on a binary system, they have
to convert the numbers into our 10 unit decimal system so that we
can understand them.
‘Hello World’ in binary is:
010010000110010101101100011011000110111100100000010101110
11011110 11100100110110001100100
How it is represented in machine code is shown at the top of the
next page.

134
a Converting to binary numbers
demain Mamata

C7 28 2a 2a 3c 5c 2b 00 00 00 00 00 00 6f'00 00
3c 5c 2b 5c 28 2a 2a 00 00 00 00 00 00 72 00 00
4 The word binary comes from
2a 2a 2a 3c 5c 2b 00 00 00 00 00 00 00 6c 00 00
‘bi-’ meaning two. We see
3c 2b 5c 28 2a 2a 00 00 00 00 00 00 00 64 00 00
‘bi-’ in words such as ‘bicycle’
2a 2a 3c 5c 2b 5c 01 00 00 00 00 00 00 21 00 00
(two wheels) or ‘binocular’
2b 5c 28 2a 2a 3c 00 00 00 00 00 00 00 00 00 00
‘(two eyes).
2a 3c 5c 2b 5c 28 00 00 00 00 00 00 00 00 00 00
4 When we say a binary number, 5c 28 2a 2a 3c 5c 00 00 00 00 00 00 64 00 00 00
we pronounce each digit (e.g. 3c 5c 2b 5c 28 2a 00 00 00 00 00 00 48 00 00 00
the binary number ‘101’ is said 28 2a 2a 3c 5c 2b 00 00 00 00 00 00 65 00.00 00
as ‘one zero one’). This is so 5c 2b 5c 28 2a 2a 00 00 00 00 00 00 6c 00 00 00
that we don’t get confused 2a 2a 3c 5c 2b 5c 00 00 00 00 00 00 6c 00 00 00
with the decimal number. 2b 5c 28 2a 2a 3c 00 00 00 00 00 00 6f 00 00 00
A For the examination you will 2a 3c 5c 2b 5c 28 00 00 00 00 00 00 2c 00 00 00
need to be able to convert 5c 28 2a 2a 3c 5c 00 00 00 00 00 00 20 00 00 00
between binary and decimal 3c 5c 2b 5c 28 2a 00 00 00 00 00 00 57 00 00 00
whole numbers (0-255).
§ Converting to binary numbers
Converting decimal numbers to binary numbers is relatively
simple. Let’s explore counting in binary and decimal to see how
the numbers compare.
Decimal |
number How we convert Binary number

0 We start at O for the number of bits required. Here OOO000000


we have 8 bits as there are 8 digits

a Then 1 we add a 1 on the right 00000001

Z We add one to the number on the right but as we 00000010


already have a 1 at the right it changes back to O
again, but we carry a 1 one place to the left

3 Now we add a 1 on the right again as itis a O 00000011

4 As we want to add a 1 to the right and it is already 00000100


a 1 it also goes back to O and 1 is added to the
next position on the left but it is also a1 so it
becomes a zero and we carry the 1 one place to
the left

we Now we add a 1 on the right as itis aO 00000101

6 As we want to add a 1 to the right and it is already 00000110


a 1 we make it a O and carry a 1 to the left

if Now we add a 11 on the right as itis a O 00000111

8 Start back at O again (for all 3 digits), add 1 is 00001000


carried on the left

9 And so on 00001001
16 Binary and hexadecimal numbers

If we look at a binary number we can see that the first right-


most digit has a weight of 1 (2°), the second digit has a weight of
2 (21), the third a weight of 4 ( 27), the fourth a weight of 8 ( 2° )
and so on up to 128 ( 27).
We can easily construct a table to show this:

= : Binary digit table


93 22 21 20
2 26 2° 24

64 32 16 8 4 9) 4
128
2 Questions
If we have an 8-bit binary number 01100110 and place this in our
ee iiaihe piney code (ialiewes) table it would looklike thie:
decimal number 67. Use eight
binary digits. Decimal digit value 642 32) AGQ i) Samhain
4 Give one reason why we use Binary digit value Mee Me a ce hank li42 2!|
binary to represent data in
computers.
We can now easily convert it to a decimal number as we have:
64+ 32+4+2=102

§ Adding binary numbers


Adding binary numbers is also relatively simple — we will use
4 bits this time to make it easier to see what is happening.
Suppose we want to add the binary numbers 0111 and 1110 (the
decimal would be 7 + 14 = 21).
You first align the numbers as you would if you were adding
decimal numbers.
0 1 i 1

J Key points : : : :
A A single binary digit (like ‘O’
You then add the two numbers in the far right column, again
or ‘1’) is called a ‘bit’. For
as you would for decimal numbers. Add the numbers following
example 11010 is five bits
the rules of decimal addition (1 + 0 = 1, 0 + 0 = 0) unless both
long.
numbers are a 1.
A The word bit is made up from If they are, we write ‘0’ below and carry a ‘1’ to the next column.
the words ‘binary digit’. (Remember, it is not ‘ten’ but ‘one zero’). We use the rule 1 + 1 +
4 For the examination you will T= Tcarry 1.
need to understand how to 0 ‘ cf Al
perform binary arithmetic
(add, shifts (logical and 1 1 1 0
arithmetic)) and understand
the concept of overflow.

136
i
Adding binary numbers

Move on to the next column to the left.

0 it 1 1

1 il 1 0

Move on to the next column to the left.

0 a Al i}

a i) ll 0

ul i
We start on the right and move across the columns to the left. If
there are no more columns we add a new one.

0 i 1 1
il sl 1 0

ut 0 if 0 fi
1 1 1
If we want to add three binary numbers this is achieved in exactly
the same way, e.g.

Answer = i 0 1 1 0 1
1 1 1 1
Add rightmost column = 1 carry 1
Add next column = 0 carry 1
Add next column = 1 carry 1
Add next column = 0 carry 1 and another 1
The next column will be 0 carry 1
(The decimal would be 13 + 11 + 5 = 29)
” , 16 Binary and hexadecimal numbers acs DA IA
Pg easiness
ne
ons

sa § Signed and unsigned integers


In mathematics representing negative numbers is easy, we just
add a - sign.
So far we have only looked at unsigned integers. Unsigned
integers do not have a + or — sign which means they can only
have positive values.
Sometimes we don’t need variables to take negative values, for
example int age; age will not be negative, moreover we can double
the range and store large numbers by using unsigned values.
A signed integer is one with either a plus or a minus sign in front.
That is it can be either positive or negative.
This is important in computing because the numbers are stored
(usually) as a fixed number of binary digits. For a signed integer
one bit is used to indicate the sign.
Two common methods are used for this:
® sign-magnitude
® two’s complement.
Although sign-magnitude or two’s complement can be used for
both positive and negative numbers, sign-magnitude has a number
of problems so two’s complement is usually the best method to
represent signed integers.

Sign-magnitude
Sign-magnitude uses the most-significant bit as the sign bit. The
rest of the bits represent the number in the ‘binary’ format. It is a
bit like the number line used in mathematics.

rie abe a4 33) HZ OS Vee ol ee


—Negative +Positive (sign usually omitted)

Performing addition and subtraction with signed integer numbers


is easy, you just move a certain number of units in one direction
or another. To add two numbers, you find the first number on
the scale and move in the direction of the sign of the second as
many units as specified. Subtraction is the same, moving along
the number line as dictated by the sign and the operation.

138
Signed and unsigned integers

Let’s look at the binary representation:

0111 : +7
0110 6 +6
0401 5 +5
0100 4 a
0011 3 +3
0010 2 +2
0001 1 +1
0000 0 +0
1.000 8 -0
1004 9 a
1010 A e
1014 B -3
1100 C -4
1101 D -5
1110 E -6
14141 F 4

Two’s complement
Two’s complement is the method computers use to represent
signed numbers.
The most-significant bit is the sign bit:
® 0 = positive value
® 1 = negative value.
Because of this a 16-bit signed integer only has 15 bits for data
whereas a 16-bit unsigned integer has all 16 bits available.
To create the negative representation of a number:
® Start with the positive number
@ Flip the bits (change ‘0’ to ‘1’ and ‘1’ to ‘0’)
@ Add the value 1.
Addition and subtraction are accomplished in the same way as in
10’s arithmetic:
— 127 10000001
+) ail 00000001
— 126 10000010
]6 Binary and hexadecimal numbers

aN
O11. uf +7
0110 6 +6
0101 5 +5
0100 a ae
0011 3 +3
0010 2 +2
0001 1 +1
0000 fo) +0
1444 F oi
1110 E =o
1104 D -3
1100 Cc -4
1044 B -5
1010 A -6
1004 9 -7
1000 8 -8

Advantages of two’s complement representation:


1 One value for zero
2 Conversion from positive to negative and negative to positive
numbers is easy and consistent.
Unsigned integers can have a value twice as high as signed
integers because 1 bit is used for the sign, but unsigned integers
can only have positive values. You may be asking why this is of
any importance.
On older 16-bit computers this was important, as it translated
into a difference between a value of 32,000 on a signed integer or
65,000 on an unsigned integer. On 32-bit computers it is far less
significant since the maximum values are 2 billion or 4 billion,
and on 64-bit computers it becomes of academic interest.
Python has large integers which effectively are unlimited in size,
so it does not matter whether the integer is signed or not.

Overflow
One problem that can occur with signed binary numbers is that
of overflow. This is where the answer to an addition or subtraction
problem exceeds the magnitude which can be represented with
the allotted number of bits. Remember that the place of the sign
bit is fixed from the beginning of the problem.
When a positive number is added to a negative number there will
never be an overflow error. The reason for this is apparent when

140
SSS SSS SSS SSS sss sss
Binary shift

the nature of overflow is considered. Overflow occurs when the


magnitude of a number exceeds the range allowed by the size of
! Key point the bit field. The sum of two identically signed numbers could
exceed the range of the bit field of those two numbers, and so in
When a number becomes too this case overflow is a possibility.
large to fit into the number
of bits allocated it is said to However, if a positive number is added to a negative number, the
‘overflow’ and some bits are sum will always be closer to zero than either of the two added
‘lost’ leaving an incorrect value. numbers, therefore its magnitude must be less than the magnitude
of either original number, and so overflow is impossible.

§ Binary shift
If we shift the decimal point in a decimal number to the left it
multiplies the number by 10. So 1.2 would become 12. If we move
the point two places it would become 120. This is because we use
a 10-digit numbering system.
Binary uses a 2-unit system and therefore a shift 1 place to the left
Binary shifts can be used to in a binary number is the same as multiplying by 2.
perform simple multiplication/
division by powers of 2. A left shift 1 place looks like this:

O bk

1 O

O Al

O O

A left shift 3 places is the same as multiplying by 8:

ot
Recut | 2 Oe re ne

In general, shifting N places left is the same as multiplying by 2


to the power N (written as 2).
If we shift to the right 1 place this is the same as dividing by 2:

input Oe | To lou | pee


Recut Os oe
Itis worth noting that for odd numbers this will be an approximation
for integer values, e.g. 15 shifted would give 7. This is because we
lose the last digit in the table.
A right shift 2 places is the same as dividing by 4:
16 Binary and hexadecimal numbers

A right shift 3 places is the same as dividing by 8:

In general, shifting N places right is the same as dividing by 2 to


the power N (written as 2%).
This shift can lead to what are called overflow errors. An example
of an 8-bit overflow error is:
Say the binary sum is 11111111 + 1 (the decimal would be 255 +
17256):
In binary we have:
i 1 il 1 il 1 1 i
0 0 0 0 0 0 0 1

dl ) 0 0 0) 0 0 0 0
The leftmost 1 is an overflow number as we only have 8 bits
and our binary number is now bigger than 8 digits. When this
happens the CPU drops the overflow digit because the computer
cannot store it, so the computer thinks that 255 + 1 = 0.

! Key point | Hexadecimal numbers


The word hexadecimal means Computers can also use what are called hexadecimal numbers. There
‘based on 16’ (from Greek hexa: are 16 hexadecimal digits. As computers are binary machines
‘six’ and Latin decima: ‘a tenth working with 1s and 0s you may be asking the question as to
part’). why hexadecimal (hex) is used. After all everything in a computer
consists of combinations of 1s and Os. The answer is the number
of bits used for.each number. You need 4 bits to code numbers
between 0 and 15. To optimise the coding possibilities with 4 bits,
the hexadecimal code is perfect because it includes all the existing
10 decimal numbers. The reason for using hex is that one hex digit
corresponds to exactly four bits and converting between the two
is very easy as it can be done on a digit-by-digit basis. Humans
find hex representation easier to remember and understand than
the equivalent binary representation that a computer uses.
Hex numbers look exactly the same as decimal numbers up to 9,
2 Question
but them theresare,thedetters,(A, BOCs ain place of the
5 What is hexadecimal? decimal numbers 10 to 15.

A single hexadecimal digit can show 16 different values like this:

4 6) 6 i 8 2) 10 eels eZ 13 14 ALS
4 3) 6 U 8 9 A B C D E F
Hexadecimal to binary

Hexadecimal to binary
Converting from hexadecimal to binary and binary to hexadecimal
is fairly simple. We simply look up each hexadecimal digit to
obtain the equivalent group of four binary digits.
Hexadecimal 0) al D 4 S 6 i 8 9 A B C D E F
O al 10 ALL 10094101 0) 11055 4445.1000 4001 10101011. 1100° 4101 A110 1114

So the hexadecimal number A2ZDE


would give the following4binarydigits 1010 0010 1101 1110
which written in binary is 1010001011011110

| Decimals to hexadecimal
Converting decimal numbers to hexadecimal numbers is much
harder than converting decimals to binary numbers.
We always work backwards to convert these numbers.
2 Question
If we have a decimal number of 1128 the hex equivalent is
6 How many possible values are calculated as follows.
there in a single-digit base-16
number system? Remainder (in
What to do :—— Division | hexadecimal)
Start by dividing the number by 16, that
is (1128/16).

1128 divided by 16 is 70.5. So the


A Any number to the power 0
integer division result is 70. Record
equals 1. We write powers in this in the Integer column.
computing using “ so 16 to 1128 / 16 70 8
The remainder is (70.5 — 70) or 0.5
the power 3 is written as 1643 which is then multiplied by 16, giving a
result of 8.
A For the examination you will
need to understand why We now record this in the Remainder
column.
hexadecimal notation is
used and be able to convert We now divide the result number again
by 16, so 70/16
between hexadecimal and
binary. In this case, 70/16 = 4.375. So the OV/AG 4 6
integer division result is 4. Again the
remainder is 0.375 is multiplied by 16,
which is 6.

The next stage is to repeat again so


2? Question 4/16 = 0.25. The integer division
result is O. 4/16 re) 4
7 True or False: 51 is hex for ‘Q’?
The remainder is 0.25 multiplied by 16,
which is 4.

Stop because the result is already O (O


divided by 16 will always be 0).

We now have the hexadecimal


equivalent of the decimal number by
468
working up from the bottom of our
remainder column.
16 Binary and hexadecimal numbers

| Hexadecimal to decimal
What if the number 1128 was a hexadecimal number and not a
decimal number and we wanted to find its decimal equivalent?
This would be calculated as follows:

The last number is 8. It represents 8 x (16%0) which equals 8.


? Questions
The next number is 2. This represents 2 x (161) = 32
8 What is a base-2 number
system also known as? The next number is 1. This will be 1 x (16%2) = 256
9 What is a base-16 number And lastly 1 x (1643) = 4096
system also known as?
If we add the totals together 1128 in hexadecimal = 4392 in denary.
You may want to remember some of the powers of 16.

oe
4640 | vO 9
ae a

TOM a6 16

1642 =16 x 16 256

ASV} ante} S< AUS) 3% AS 4096

1644 =16x 16x 16x16 655536

You may be wondering about the letters. They work the same way
2 Questions
as digits so FA8 working backwards would be:
10 Convert the number 188 to
Sie lhe=tce)
hexadecimal. (Remember to
write down the remainder in 10 x 16 = 160 (remember A = 10 and this has to be multiplied by
hexadecimal, not decimal.) 1600)
11 Work out the decimal
15 x 256 = 3840 (remember F = 15 and this has to be multiplied
representation of the
by 1642)
hexadecimal number D48.
The total in denary would be 4008.

Chapter review
® Be able to convert between binary and decimal
In this chapter we have explored binary and whole numbers from 0-255
hexadecimal numbers. ® Understand and be able to explain and apply binary
Remember, before tackling any computer science task arithmetic including shifts (logical and arithmetic)
or examination question on this topic you must: and demonstrate and understand the concept of
overflow
® Understand and be able to explain how computers

unsigned integers, signed integers (sign and notation is used


magnitude, two’s complement) ™ Be able to convert between hexadecimal and binary.

144
17 Computing and data
representation

Specification references
You should:
3.2.3 understand how sound, an analogue signal, is
| 3.2.1 understand how computers encode characters represented in binary
using ASCII 3.2.4 understand the limitations of binary
= 3.2.2 understand how bitmap images are representation of data (Sampling frequency,
represented in binary (pixels, resolution, colour resolution) when constrained by the number of
depth) available bits

§ Translating data
Let’s consider what happens when we put data/information into
a computer. When you enter data into your computer you use a
format that you understand, using characters and numbers that
you are able to read. But you know now that a computer cannot
use the data in this form. It has to be translated into a computer-
readable low-level format. As this is done through an input device,
we call it input. Of course once the computer has processed the
data, for us to understand it, it needs to be converted back into
a format that we understand using an output device, so this is
referred to as output. Together these are referred to as I/O.
In pseudo-code you will often see the words ‘get’, ‘input’ and
‘read’ used for inputting data. For outputting data you will see
the words ‘print’, ‘display’, ‘write’ and ‘put’.

| Character sets
You may be wondering, if computers can only work in ones and
zeros how can things like text be represented? Text is represented
A stringis a data type used by character sets. A character set is simply a list of characters such
to store a sequence of letters, as a, b, c, d, ... and the numerical codes used to represent each
symbols and/or numeric letter or character. By agreeing to use a particular character set,
characters. computer manufacturers have made the processing of text data
easier.
We will explore two character sets: ASCII and Unicode.
~ 17 Computing and data representation

“ Fi

§ Representing text using ASCII |


Ls

Mb nw I ] Key points
4 Computers only recognise ASCII stands for American Standard Code for Information
binary, computers don’t Interchange. As we have seen computers can only understand
recognise the alphabet. numbers, so an ASCII code is the numerical representation of
A ASCIii (American Standard characters such as from ‘a’ to ’z’, ‘@’ or even an action of some
Code for Information sort.

Interchange) is a 7-bit system ASCH was originally developed to use one byte which is 8 bits.
to code the character set on a You will know from earlier sections of the book that a bit is a
computer. binary digit (0 or 1). You can store 2° or 256 possible values in a
single byte as it has 8 bits.
The original ASCII only uses 7-bit numbers to represent the
2 Question
letters, numerals and common punctuation used in the English
1 What will the following language. As there are 2 possibilities per bit, we have 2’ hence 128
pseudo-code do? possible values it can represent, from 0 to 127 inclusive. Remember
CHAR_TO_CODE('a’) 0 is also a reference so we have 128 possible values to store our
characters.
Each of those 128 values is assigned to a character. For example,
in ASCII the number 65 represents an uppercase letter ‘A’ and
61 represents an equals sign. So if the output sent to a display
receives an ASCII value of 65, it displays an uppercase letter A
on the screen. There is no real reason that A has to be character
number 65, that’s just the number the developers working on
telegraph systems chose when ASCII was first develuag

ee) NULL 0 @
foooa | SOH DC4 1 A Q a, =) me
SIX DC2 “ 2 B R b ;
Spc o debect # 3 c S ¢ 5
eee EDT Dc4 $ 4 D T d t
ENO NAK % 5 E uU e u
ACK SYN & 6 F V . v
BEL ETB a G Ww g Ww
es CAN ( 8 H x h x
= HT EM ) 9 | vy. y
SUB = 2 z} HL J Z.

2 Question ESC + j K [ k {
2 In the ASCII code, what is the FS ' J L \ |
relationship between the code GS , : M m }
for an uppercase letter and RS , S N A n ey
the same letter in lowercase?
US j 2 0 : @ 1) DEE

146
The Unicode character set

! Key points If we want to translate ‘Hello’ into ASCII it would look like this:

A Standard ASCII characters use 01001000 01100101 Od


OL 100%, 001001005 81701145
7 bits. H e | | (e)

4 Character codes are commonly


grouped and in sequence. Extended ASCII
A An Extended ASCII character ASCII works fine for English strings such as ‘Hello World’, but
uses 8 bits. what if we want to store a German string such as: ‘Es gefallt mir
nicht’ (translates as: ‘I do not like it’). Basically we can’t use ASCII
for this.
It was decided that, as ASCII had an extra bit which was not in use,
if ASCH was made into an 8-bit code rather than a 7-bit code, it could
3 What does this say using ASCII store another 128 values. This system is called Extended ASCII.
code?
The problem was that different groups used the same initial ASCII
01000001 01000011 01000001 characters, but came up with different Extended ASCII character
01000011 01101111 01101101 set numbers. For example, in the Western Europe Extended ASCH
01110000 01110101 01110100 character set, number 224 represents a lowercase letter A with a
01100101 01110010 00100000 grave accent. But in Eastern Europe the same number represents
01010011 01100011 01101001 a lowercase letter R with an acute accent. Added to this a
01100101 01101110 01100011 256-character set is no use in languages like Chinese and Japanese,
01100101 where there are literally thousands of characters in common use.
4 How many bytes are in each
ASCII character?
For languages with lots of characters and/or accents, including
Chinese, Japanese, Hebrew, etc., different coding systems are
5 What is ASCII?
needed. The most common choice is Unicode.

B the Unicode character set


Although originally designed for 16 bits, the latest version of
Unicode stores each character in 32 bits. Unicode can also use 8
Unicode is a system of up to bits. It presently contains over 110,000 characters and has space
32 bits to code the character set for 1,114,111 different values that can be used for characters, so at
of a computer (usually 16-bit or present only around 10% are used.
32-bit versions).
The chart below shows just a few of the foreign language characters
supported. The first 128 characters are the same as ASCII.
| Code (Hex) | Character — Source
e Questions
a eo
6 What is a character set?
042F A
7 Using an ASCII table, convert
46 6F 6F 74 62 61 6C 6C to 262F
text. 03A3 2
2116
210C
28FF
2EDD
‘F 17 Computing and data representation

§ Writing in hexadecimal
Using this knowledge we can see how this would work if we
wanted to explore the use of hexadecimal.
When we start to program we normally start with “Hello World’.
This looks easy, but let’s explore what is happening if we write
Hello in hex.
We type Hello. This consists of separate letters ‘H’ ‘e’ ‘I’ ‘I’ ‘o”
We can use binary or hex for this. You learnt about these in other
sections of the book, but for this we use hex. First we’ll revise the
equivalents in each system.

om 0000 0
4 0004 aL

2 0010 2

3} 0011 3

4 0100 4

5 0101 5

6 0110 6

7 0111 i

8 1000 8

¢) 1001 ©)

10 1010 A

tae LOMA: B

Ae 1100 C

AES} LENO D

14 1110 FE

LS dala lal lf

1 Write ASCII ‘Computer’ in So F3C2 in HEX would look like this in binary: 1111 0011 1100
binary. 0010
If we now convert each letter to ASCII we will have:
2 Question
H = Write 48 (hex)
8 What is a universal encoding
system that provides a unique e = Write 65 (hex)
number for every character,
1 = Write 6C (hex)
regardless of language,
program or platform? | = Write 6C (hex)
0 = Write 6F (hex)

148
Data standards

! Key points | Data standards |


A All data are numbers, and all Of course computers need to show more than text. To help
numbers are data. programmers, each data system is controlled by a set of data
A Data standards are the rules standards. This is needed so that different systems can still read
by which data are described the language we code in. Some of the most common file data
and recorded. standards are shown in the table.

Images JPEG, GIF TIEF, GIF ClCe

Sound WAV, MP3,


AU, etc.
Moving images Quick Time, MPEG-2, MPEG-4, etc.

Alphanumeric ASCII, Unicode

Outline graphics/fonts True Type, PDF, PostScript

Difference between analogue and


digital data
In the natural world everything is continuous and infinite. A
number line is continuous, with values growing infinitely large
and small. You can always come up with a number larger or
smaller than any given number. The numeric space between any
two integers is infinite. For example, any number can be divided
in half.
Theoretically, you could always close the distance between you
and your home by half, and you would never actually reach home.
But the world is not just infinite in a mathematical sense. The
spectrum of colours is a continuous rainbow of infinite shades.
Computers, on the other hand, are finite. Computer memory and
other hardware devices have only so much room to store and
manipulate data. In computer science we can only work towards
representing enough of the world to satisfy our computational
needs and our senses of sight and sound.
e Analogue data is continuous, analogous to the actual
information it represents. For example, a mercury thermometer
is an analogue device. The mercury rises in direct proportion
to the temperature. Computers cannot work with analogue
information.
® Digital data breaks the information up into separate steps.
This is done by breaking the analogue information into pieces
and representing those pieces using binary digits.
Computers need to convert analogue data to digital data as they
are binary devices and cannot store continuous values.
Now we will explore the differences in more detail, starting with
digital images.
17 Computing and data representation

§ Bitmaps and binary images


The quality of an image you see output on a display is based upon
! Key points the resolution. The first type of resolution you are likely to come
A A bitmap is a graphical image across on any computer device is the number of pixels.
represented as an array
When we talk about bitmap images we are talking about a regular
of brightness values. For
rectangular mesh of cells called pixels. A pixel (short for picture
example, in a black and white
element) is the smallest area of an image that can be individually
image O represents white and
altered; it is a single dot. Each pixel contains a colour value.
1 represents black.
Images contain parameters: height, width, the number of pixels
A Pixels are the dots that make
the image on screen. They are and the information content per pixel.
the smallest element of an
image.
A The resolution is the number
of pixels or dots, for example
ppi (pixels per inch), often
referred to as dpi (dots per
inch).

? Questions
9 What do we mean by image
size? Figure 17.1 Pixels on a PC screen
10 How does the resolution of
Bitmaps are always orientated horizontally and vertically. Pixels
an image affect the size of
are usually defined as being square although in practice they may
the file?
be rectangular or have other aspect ratios.
In the majority of situations bitmaps are used to represent images
on the computer.
There are a large number of pixels making up the computer’s
display. To give you some idea of what this means, a modern
monitor is 1920 x 1080 which represents a grid 1920 pixels wide
by 1080 pixels high.
If we change the display settings to 2560 x 1440 there will be many
0|0)0j)0]0j]0 000000 more pixels on the display. Because the monitor has not changed
0 010117! 0 lo1o010 Siz, the pixels will become smaller, making everything on screen
| smaller but much better defined. The advantage of using higher
0 Oric0 0 }010010 resolutions is that smaller pixels let us display images with much
0] 0}0]0}]0/0 {000000 more detail.
0 Pe RSA0 Jo11110
GaniaiAi ‘aaee
i Black and white image using binary
OF On Oc Rog iae | 0 |000000
Using what we now know we can easily describe a black and
Figure 17.2 Image in binary code white image using binary as shown in Figure 17.2.

150
Ss
Colour depth

] Key points § Colour depth :


4 Colour depth (or bit depth) Although colour depth affects the amount of memory a picture
refers to the number of bits requires, it does not affect the size at which the image is displayed.
used for each pixel or dot. The It has a major effect on the quality of the image.
more bits the more colours
that can be represented.
We now know that the resolution of an image is determined by
the number of pixels. These are individually addressable points
4 For the examination you
that make up the screen image. In a similar way it can be the
will need to understand
number of dots that make up a printed image. The more pixels or
how bitmap images are
dots that are used to create an image, the more detail the image
represented in binary (pixels,
can have and the sharper it will appear when we look at it.
resolution, colour depth).
A If you want to calculate the
When using any bitmap image, whatever resolution we choose,
size of an image you need
the picture’s data has to be stored for each pixel or printer dot.
to use colour depth and
This means the higher the resolution, the more data there will be
the number of pixels in your
that needs to be stored. But as well as the pixel data we also have
calculations.
colour depth data. This data is used to describe the maximum
number of colours that are used in the image. The higher the
A Size in bits = W x H x D
number of colours used the more realistic the image will appear.
Size in bytes = (W X H x D)/8 As with the number of pixels, the chosen colour depth affects the
where W = image width, H = file size of the image as the more colours we want to record the
image height and D = colour larger the data set will be for the given image.
depth in bits.
Each pixelina bitmap contains colourinformation. The information
content is always the same for all the pixels in a particular bitmap.
The amount of colour information can vary according to what the
application requires but there are some standards. Typical colour
depths include 1-bit (mono), 8-bit, 16-bit and 24-bit. To work out
how many colours that is, simply calculate 2 to the power of the
number of bits.

§ Colour and RGB

1 bit (black and white)


RGB stands for red, green and The smallest possible information content that can be held for each
blue. pixel is 1 bit. Pixels with a 0 are referred to as black and pixels with
a 1 are referred to as white. Although only two states are possible
they are often interpreted not as black and white but as any two
colours; 0 is mapped to one colour, 1 is mapped to another colour.
a

17 Computing and data representation


oe
8-bit greys
Each pixel takes 1 byte (8 bits) of storage resulting in 256 different
states. These states are mapped onto a range of greys from black
to white. The bitmap produced is referred to as a greyscale image.
0 is normally black and 255 is white. The grey levels are the
numbers in between, for example, in a linear scale 127 would be
a 50% grey level.

255
Figure 17.3 Grey levels

24 bit
Moving on from 8-bit grey we have 8 bits allocated to each primary
colour: red, green and blue. In each colour the value of 0 refers to none
of that colour, 255 refers to fully saturated colour. Each component
has 256 different states so there are 16,777,216 possible colours.

8-bit indexed colour


Indexed colour is a much more economical way of storing colour
bitmaps without using 3 bytes per pixel. Each pixel has one byte
associated with it. The value in that byte is not a colour value but
an index to a table of colours, called a palette or colour table.

If there are fewer than 256 colours in the image, the bitmap will
be the same quality as a 24-bit bitmap but will be stored with
one third the data. Colour and animation effects can be achieved
by simply modifying the palette; this immediately changes the
appearance of the bitmap.

32-bit RGB
This is the same as 24-bit colour but with an extra 8-bit bitmap
known as an alpha channel. This channel can be used to create
masked areas or represent transparency.

§ Calculating image sizes


Although file sizes will be slightly larger than a simple calculation
will suggest due to other data necessary to create the image, it is
possible to calculate an approximate file size for an image.
An image file size is calculated using the number of pixels divided
by 8 to convert it to bytes.

152
Photographic images

To calculate the number of pixels we'simple multiply the width in


pixels by the height:
100 x 100 = 1000 pixels
Therefore a 100 by 100 black and white image will be (100 x 100)/8 =
1250 bytes
We now need to divide by 1000 to get kilobytes: 1250/1000 = 1.25 kb
If we have a colour image we need to multiply by the number of
bits needed for the colours, so for a 256 colour bitmap of the same
size the equation will be:
((number of pixels x bits needed for 256 colours)/8)/1000
So first we need to work out the number of pixels, which is:
100 x 100 = 1000
Next we need the number of bits needed for our 256 colours. In
an 8-bit colour graphic image file, each pixel is represented by one
8-bit byte, if we want 256 colours we need either 18 bits (6 red,
6 green, 6 blue) or 24 bits (8 red, 8 green, 8 blue).
1000 x 18 = 180,000
1380/0007 Si=i22,500
22,500/1000 = 22.5 kb

i Photographic images
Originally cameras used film. They were based on the first pin-
hole camera.

Lens
Film
Subject
Image

Figure 17.4 How an analogue film camera works

An image was projected onto a sheet of light-sensitive paper


(film) placed at the back of a box. The photographic paper was
coated with light-sensitive chemicals (called emulsion), then
recorded an analogue image of the scene. Emulsion in the areas
17 Computing and data representation

on the photographic paper which received light would undergo a


chemical change based on the light that hit it and those areas not
receiving light would not.
In contrast, the process of taking a digital photograph is very
different. A camera lens is used to focus the light from a scene not
onto a film but onto a silicon detector (a sensor circuit). The sensor
surface is divided into a 2-dimensional pixel grid sometimes
called picture-elements.

Lens
Digital sensor
Subject
———

Image
=

Figure 17.5 How a digital camera works

The light intensity at each pixel (an analogue quantity) is sensed


and converted to a multi-bit binary number.

§iSound systems
Original sound is in analogue, it’s how our ears work. But to turn
this sound into something a computer can handle we need to
create a digital sound wave.

Original analogue sound wave

For the examination you will need


to understand how sound, an
analogue signal, is represented
in binary. Digitised sound wave

Figure 17.6 Analogue and digital sound waves

154
er Soundsystems
nee Ann |

To compare digital and analogue sound, you need to look at a


variety of factors.

Sample rate
In analogue recordings, the machine is constantly recording any
sound or noise that is coming through the microphones. In digital
A sample is a point in time where recording you don’t have a constant recording, you have a series
a measurement of an analogue of samples or snapshots which are a measure of amplitude at a
signal is taken (sampled). The given point in time and are taken from the sound being recorded.
unit of sample rate is ‘samples
per second’. This is often Each vertical line indicates a time when a sample in taken.
] ime
expressed in kiloHertz (kHz).

| |
Lower sample rates take fewer Faster sample rates take more
snapshots of the waveform... Snapshots...

resulting in a rough recreation resulting in a smoother and more


of the waveform detailed recreation of the waveform
Figure 17.7 Different sample rates

If you think about how animation works, a series of separate


i Key points
pictures that have a slight difference are shown one after another
A The sample rate is the number to make it look like something is moving. In this case, digital
of times the sound is sampled recording takes a series of ‘snapshot pictures’ of what the sound
per second. It is measured in is like and turns it into a digital recording.
Hz (100 Hz is 100 samples per
As with images, the more snapshots you have the better the sound
second).
quality, but at a cost of memory use and storage space.
A The sample resolution is
the space available for each Sample rates are measured in hertz (Hz) or thousands of hertz
sample, measured in kilobits. (kHz — kiloHertz). For example 44.1 kHz is equal to 44,100 samples
of audio recorded every second.
The sample rate you would choose depends on what the audio is
going to be used for. If you wanted to record a song to put onto
CD you would usually use either 44.1 kHz, 88.2 kHz or 176.4 kHz.
e Question
The other important factor in determining audio quality is the
11 How does the sample rate
sample resolution. This refers to how many different values the
affect the quality of the
samples can take. The higher the sample resolution, the more
playback for an MP3 sound
accurate the representation of the level of each sample. But the
track?
higher the sample resolution, the more memory that is required
to store each sample. Pass |
17 Computing and data representation

Digital audio is normally found in one of two resolutions:


@ 8-bit: used for some lower-quality recording formats
e 16-bit: the standard for compact disk audio and newer sound
cards. With 16-bit samples there are 65,536 different values
which is more than any human ear can detect.
We can calculate sound file sizes based on the sampling rate and
the sample resolution using the following formula:
file size (bits) = rate x res X secs = sampling rate

where res = sample resolution and secs = number of seconds.


By increasing the number of bits (units of information’), the
amount of detail contained in each sample is increased. An
example would be the sentence ‘The large black cloud opened up
! Key points covering the ground with white snow.’ contains more information
A A sensor is a device that can than ‘It snowed.’ The more data, the more information you have.
detect physical conditions
such as temperature, weight, Bit rate
light, sound, etc. Digital music files are measured by the amount of information
A For the examination you they can play per second. It is usually measured in Kbps or kilobits
will need to understand per second. This is the amount of sound information presented to
the limitations of binary the listener every second. The bit rate is the number of bits per
representation of data second.
(sampling frequency, Sound files played over internet radio are 56 or 64 Kbps to allow
resolution) when constrained faster transport over networks. The standard for near-CD quality
by the number of available is 128 Kbps, and some files go up to 320 Kbps.
bits.
If we have a 30-second audio file sampled at a rate of 44.1 kHz
and quantised using 8 bits we can calculate its size by:
bitRate = bitsPerSample x samplesPerSecond x number of
channels
2 Question A stereo recording has two channels so the bit rate for a recording
12 What effect does a high bit with 8-bit bit depth is:
rate have on the number of 8 x 44,100 x 2= 705,600 Kbps
sound files that can be stored
on a CD? To get the file size we would have to multiply the bit rate by the
duration (in seconds), and divide by 8 (to get from bits to bytes):
fleceee (bitsPerSample x samplesPerSecond x channels x duration)
8
So in this case 30 seconds of stereo will take up (8 x 44,100 x 2
x 30) / 8 = 2,646,000 bps. This could be converted to kilobytes by
dividing by 1000 again.

156
Sound systems

Chapter review

In this chapter we have explored how data is represented in a computer.


We looked at sample rates, bitmapped images, data size and the
differences between analogue and digital data.
Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain how computers encode characters
using ASCII
® Understand and be able to explain how bitmap images are
represented in binary including the use of pixels, resolution and colour
depth
® Understand and be able to explain how computers use binary to
represent data including numbers, text, sound, graphics and program
instructions
® Understand and be able to explain how sound, an analogue signal, is
represented in binary
® Understand and be able to explain the limitations of binary
representation of data, demonstrating a knowledge of sampling
frequency resolution when constrained by the number of available bits.
Co Specification references

You should:
1.1.7 understand how the choice of algorithm is influenced by the data
structures and data values that need to be manipulated
2.3.1. understand the need for, and understand how to use, data types
(integer, real, Boolean, char)
2.3.4 understand the need for, and how to use, variables and constants

§ Why are data types important?


Most programming languages use statically typed variables. This
means that when a variable is created the programmer must
specify what type of data the variable will be using and the data
type cannot change once it has beet set.

! Key point
The word variable in programming describes a place to store information
that can change, such as numbers, text, lists of numbers and text.
Another way of looking at a variable is that it’s like a box with a label on
to store something.

Setting the type of a variable helps to prevent errors that might occur
when a program is executed. It also makes the program run more
efficiently. Operations such as data sorting rely on knowing what
type of values are stored. Strings need to be sorted into alphabetical
order whilst reals and integers need to be sorted into numeric order.

§ Data types
We will now explore how data is used in more detail. The most
common types of data and the ones you will be using are called
primitive data types.
Primitive data types are predefined types of data, which are
supported by the programming language.
Data can be stored in many different forms and the proper term
for these forms is data types. It is these forms that determine what
158
sl
Integer data types

actions, for instance searching, sorting or calculating, can be


performed on the data when it is held within a field of a database
or a spreadsheet.
Let’s look at some of the common primitive data types that you
will be using:
Primitive data type

Integers Real numbers

byte short int long float double char Boolean


Figure 18.1 Common primitive data types

In Python we have the following data types:


long #integer > 32 bits
float #floating point number
bool #Boolean
str #string

fi Integer data types


Integer data types deal with whole numbers, NOT decimal
numbers.
An integer is a data type for
An integer is a whole number (not a fractional number). But it can
representing whole numbers.
be positive, negative or zero.
Examples ot integers are: —-9 3, 5,8, 93 arid 9,103:
e Questions
Examples of numbers that are not integers are: —1.33, 1 3/4, 3.14,
1 In mathematics which of the
and 1500.45
following are integers?
6e=fe2.6 10 4.5 17 17.0
Different programming languages state the data types in different
ways. Some versions of Python can be forced to use what is called
2 What will the following
a floating point real number.
pseudo-code do?
int(17.0 / 8.0) #this will force Python to return an
STRING_TO_INT('16') #integer rather than a float

but
] Key point float(16 / 8) #this will force Python to return a float
A bit is a binary digit, ‘0’ or ‘1’. #value

Integers are usually sub-categorised in accordance with their


capability of representing negative values — for example within
18 Data types and structures

C and C++ the data type unsigned short can deal with whole
numbers from 0 to 65,535, whereas short can deal with whole
numbers from —32,768 to +32,767.

You will need to research your chosen programming language to


2 Question
see how it deals with integers.
3 What is the difference
between ‘integer’ and ‘real’ i String data type
data types?
A string is a data type storied as a sequence of characters, often
implemented as an array of bytes. As we have explored before,
a string is usually stored as either a constant or as some kind of
variable. A string may also denote more general arrays or other
sequence (or list) data types and structures.

i Casting
Casting is when you convert a variable value from one type to
another. In Python, this is done with functions such as int() or
float() or str().

Often you want to change a string into a proper number. For


example this code:
x = '80'
y = '-/0'
1 Explore how your chosen print(x + y)
programming language deals
with integers. prints the following as the numbers are strings:
80-70
If we use the int() function:
x = '80'
y = '-/0'
print(int(x) + int(y))

2 Write the pseudo-code to ask


we will get:
the user to input 60 positive 10
numbers using the keyboard; A float() does basically the same thing:
Add up the numbers, and
x = ‘80’
send the total to the screen.
If a negative number is y = '-70'
encountered, the program print(float(x) + float(y))
should terminate, and send
The output would be:
the sum so far to the screen.
10.0

160
ene
Real data types
ee

2 Question fiReal data types |


4 What will the following A real data type contains numeric data in a decimal form. It is
pseudo-code do? used in situations where more accurate information is required
STRING_TO_REAL('16.3') than an integer can provide as an integer is a whole number.
Here are a few examples of where a real data type may be used:
@ Distance in kilometres (km): 23.62, 3.51222, 109.33
® Speed in metres per second (m/s): 62.5, 10.2
® Weight in kilograms (kg): 20.666, 32.7
However real data types cannot store the measurement symbol
(km, kg etc.) or the units of measurement, for instance kilometres
or metres/second. If you want to use the real data type you must
remember to add the measurement symbol separately and print
the units after the field displaying the data type.
It is worth noting that in the case of money (currency) the data
! Key points
type can be real or integer. For small values it is most likely that
4 Booleans are variables that decimal places will need to be included and therefore a real data
store just two values, e.g. type is required, whereas if the values being considered are large,
TRUE or FALSE. such as the cost of a house, it is doubtful that decimal places
4 A real number can have would be important in which case only whole numbers would be
decimal orfractional parts. considered and therefore the integer data type would be used.
You may be asking yourself that if the real data type can hold any
number, why do we use the integer data type?
There are two reasons why the integer data type would be used:
® Processing speed: The time it takes a computer to calculate
using ‘real’ numbers is a lot longer than for whole numbers held
as integer data types.
® Storage: Real data types take up more memory than integer data
types; therefore if decimal points are not required it is better to
use integers.
A byte is a group of binary (‘0’ One of the common problems encountered by people learning
or ‘1’) digits, 8 bits. programming is choosing the wrong data types. The use of
unsuitable data types can lead to programs behaving unexpectedly
and lots of time can be wasted in trying to understand what is
going wrong.
3 Explore how your chosen
programming language deals § Char data types
with the real data type.
Another primitive data type we will explore here is char. This is
simply a character, for example ‘a’.
18 Data types and structures

Python and data type conversion


Python provides several built-in functions that allow you to
convert one data type to a different data type. Whilst in practice
these functions do not actually convert the original value, they
do return a converted representation of the original value. In
programming this is referred to as a ‘cast’ operation.

int(x) Converts x to an integer whole number

float(x) Converts x to a decimal floating-point number

str(x) Converts x to a string

We will explore this using some Python code:


num! = input('Please enter a number: ') #asking user to
#enter a number

num2 = input('Please enter another number: ’) #asking user to


#enter a second
#number

print(‘Input is: ' , type(num’) , type(num2)) #outputs the


#input data types

totalNum = num1 + num2

print(‘Total:' , totalNum , type(totalNum)) #outputs the total


#and its data type

totalNum = int(num1) + int(num2) #converts data


#type to integer

print(‘Total:' , totalNum , type(totalNum)) #outputs the total


#and its data type

totalNum = float(num1) + float(num2) #converts data


#type to float

print(‘Total: ' + str(totalNum) , type(totalNum)) #outputs the total


#and its data type

162
Review of data types

Output:

Please enter a number: 4 #will be based upon first


#entered number

Please enter another number: 4 #will be based upon second


#entered number

Input is: <class 'str'> <class ‘str'> | #each number is actually


#stored as a string

otal: 44° <classsstr= #result also gets


concatenated #as a string
data type
Tota 6 <class int> #we have now converted the
#data type to an integer

Votals o.0eclags‘toat'> #we have now converted the


#data type to a float

§ Review of data types


Basic primitive data types may include:
@® character (character, char)
A character is a data type that ® strings
Stores a single character. ® integer (integer, int, short, long, byte) with avariety of precisions
® floating-point number (float, double, real, double precision)
® fixed-point number (fixed) with a variety of precisions and a
programmer-selected scale
® Boolean, logical values true and false.
More sophisticated data types that you will come across and
which can be built-in include:
® tuples in ML, Python
® linked lists in Lisp
® complex numbers in Fortran, C (C99), Lisp, Python, Perl 6, D
® rational numbers in Lisp, Perl 6
® hash tables in various guises, in Lisp, Perl, Python, Lua, D.

Chapter review ined

In this chapter we have explored data types.


Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain the need for, and understand how
to use, data types including integer, real, Boolean and char (More on
this topic in Chapter 19)
® Understand, be able to explain and apply variables and constants.
Cs Specification references
You should:
2.3.1 understand the need for, and understand how to use, data types
(integer, real, Boolean, char)
2.3.2 understand the need for, and understand how to use, data
structures (records, one-dimensional arrays, two-dimensional
arrays)

Complex data structures include complex numbers, lists, tuples,


dictionaries, arrays, strings, and objects.
*<; A For the examination you will
need to understand how
The study of data structures is about organising data so that it is
the choice of an algorithm
suitable for computer processing.
is influenced by the data Computer hardware views storage devices like internal memory
structures and data values and disks as holders of elementary data units (bytes), each accessible
that need to be manipulated. through its address (usually an integer). The address is the location
A A string is a data type which in memory just like your physical address is where you live.
holds a series of characters.
Most data structures can be viewed as simple containers that
can be used to store a collection of objects of a given type. The
container could be a sentence. The objects (actions within the
sentence) are called the elements of the container.
You will have thought about data structures when you first learnt
about using computers; the files and folders you use are part of a
simple data structure. In the same way programmers have to think
about the data structure in their code. In your friend’s address the
city has areas, the areas have roads, roads have houses and your
friend is one of the people in a particular house.

§ Choosing the correct structures


As we explored in the last section, a data structure can be defined
as a collection of different data elements, which are stored together
in a clear, structured form.

In programming, one of the most important design decisions


involves which data structure to use. Arrays are among the most
common data structures.
164
How the data is stored
Dee
EFCCee

Arrays and linked lists are both designed to store multiple elements, “%
most often of the same type. An array is an ordered arrangement of ¥
data elements that are accessed by referencing the location within the
array and a linked list is a group of elements, each of which contains
a pointer that points to the following element.
It is fair to say that data structures are used in almost all of today’s
J Key points program or software solutions as they provide a method of
managing huge amounts of data efficiently. For example, this
A For the examination you
could be in a large database or an internet indexing service. In
will need to understand the
most programming situations, efficient data structures are a key
characteristics of structured
to designing efficient algorithms. In fact some formal software
and unstructured data.
design methods and programming languages emphasise data
4 A telephone directory is structures, rather than algorithms, because they are regarded as
essentially a collection of the key organising factor in software design.
pointers called telephone
numbers which link to people. § How the data Is stored
A An integer is a whole number
value, positive or negative, To explain complex data types we will first revisit some of the
with no decimal or fractional knowledge you gained in earlier chapters. In a computer, data is
part. saved or stored in a place called memory. Whatever type of memory
this may be, memory is always divided into units. Memory units can
only contain one item at a time. The amount of data that will fit into
a memory unit varies from computer to computer; understanding this
e Question
is beyond the scope of this book. But each byte of memory is like a
1 What will the following building with its own address called a memory address.
pseudo-code do?
The address of a memory unit never changes, but obviously what
LEN(Edexcel) is stored there will change frequently. Imagine a town with people
living inside the buildings as bits of data. The small town inside
your computer is a very neighbourly place. A program refers to
buildings by name rather than by address, and puts Steve’s data
inside Steve’s building. However, if you need to come up with
hundreds of names for hundreds of buildings it would be very
An array is a data structure made | complex, unless you use what is called an array.
up of a series of variables all of Mathematically, a complex number is a number of the form
the same type, grouped under A + Bi where iis an imaginary number. Complex numbers have a
one identifier. Elements are real and an imaginary part.
accessed using an index.
A list stores a group of items one after the other. It keeps them in
order for you. You can add or remove items as you wish, sort your
list or search through it to see if it contains a specific value.
Imagine a local DVD rental shop; when you want to check if a
particular film is available you would conduct a search. If the film
is available, but has been borrowed by a user, a ‘delete from list’
option could be used to remove the film from the list of those
available. When the film is returned it will be inserted back in
the list.
. 19 Data structures
nS
ie

Tuples are a lot like lists in that they are used to store sequences
> of information. Unlike lists they can’t be altered after they have
been created. Tuples are covered in more detail in Chapter 19
when we explore records.
Dictionaries store values with keys, so you can look them up with
the key. You can use them to build complex, nested data structures
or just for storing values. Dictionaries also support operations like
inserting, deleting and searching.
In programming we need to be able to do all of these operations.
At a high level, these are often called comparison methods and
direct access methods. You may come across the term hashing
which is an example of a direct access method.

§ Arrays
An array is a way to reference a series of memory locations
] Key points (buildings) using the same name. Each memory location is
A An element is an item of an represented by an array element. An array element is similar to
array. Each element can be one variable except it is identified by an index value not a name.
accessed by referencing its An index value is a number used to identify an array element.
location in the array.
In pseudo-code you use the following syntax to assign a value to
4 A variable is a name/location an element of a one-dimensional array. The red brackets < > are
in memory/identifier, used only to show where you add something — you don’t need to put
to store a value which can them in your code.
change during execution of the
code. Syntax:
SET Array[index] TO <add value here>
Examples:
SET arrayClass[1] TO ‘Steve’
SET arrayMarks[3] TO 66

There are many situations during the writing of code when


programmers need to hold related data as a single item and then
! Key points use an index value to access it, for example, a list of employees’
names or makes of car.
A A one-dimensional array is a
One method of doing this would be to assign a variable to each
single variable that can store
item in the list, such as:
a list of values. To create an
array, you first must define an name = Jim’
array variable of the desired girl = ‘Susan’
type. man = ‘Bill’
A For the examination you will
Now, although this method does work, what if you wanted to find
need to understand the need
out what the second name was? The answer is that with this
for, and understand how to
system you have no way of knowing, as there is no positional
use, one-dimensional arrays
information contained within the assigned variables. To be able
and two-dimensional arrays.
to do this we need what is called a one-dimensional array.

166
Ss
One-dimensional arrays
sssssssnsnsnsssssesees
Pre:
P

§ One-dimensional arrays
Look at this box of eggs, they are an example of a one-dimensional
array. Each has something in common: its ‘egginess’. But each egg
also has a difference. It will be different in weight, size, colour
or even its shape. But what is important here is that each of the
eges has a specific location in the box, an index. It has a position
relative to the other eggs. If we say we want egg 2, it is not egg 11
even though they look alike.

Figure 19.1

In mathematics we would call egg 2 egg-sub-two. In pseudo-code


egel2].
In pseudo-code you use the following syntax to initialise a one-
dimensional array with a set of values. The red brackets < > are
only to show where you add something — you don’t need to put
them in your code.
Syntax:
SET Array TO [<add value here>, ...]

Example:
SET arrayValues TO [1, 2, 3, 4, 5]

A one-dimensional array is a data structure that allows a list of


items to be stored with the capability of accessing each item by
pointing to its location within the array. In Python you would
need to either use a module or create a list rather than use an
array as Python does not support arrays without a module:
carMakers = ['Ford', ‘Land Rover’, ‘Vauxhall’, ‘Nissan’, 'Toyota’]

Python does not support arrays


The first line of code defines a variable called carMakers as a list,
without use of a module but lists
which is storing a number of items. Now, let’s say that you wish
to access the fifth car maker (Toyota), what you do is use a position
are a Suitable alternative.
number, for example:
car_name = carMakers|[4]
This line of code will return the 5th car maker in the array as the
first item is at 0. The process of using a position number is called
indexing and the position number is called the index. The items
within the array are called the array elements.
Arrays are useful in programming when you want to use a list of
values with a common link.
F 19 Data structures
eee

In pseudo-code, if you wanted to use the alphabet in an array you


would use:
SET Amrayn ay e.
Ol ayOnc,

Then if you needed the letter ‘e’ for example you would call it by
saying:

alohabet[5] #the number 6 refers to the position of e in the


#array

#when finding the position using pseudo-code we


#start at 0

Item number el 2 3 4 5
Index in array (i A 2 3 4
a b @ d e
2 Question
2 Explain the term one-
The reason why this type of array is referred to as a one-
dimensional array.
dimensional array is because it only uses a single number to point
to the position of array elements.
In the carMakers example above the elements would be indexed as
follows:
It is worth noting that it is
common practice within car_name = carMakers[0] #(would return Ford)
programming for the first element
car_name = carMakers[1] #(would return Land Rover)
within an array to be given an
index of zero rather than one. car_name = carMakers[2] #(would return Vauxhall)

car_name = carMakers[3] #(would return Nissan)

car_name = carMakers[4] #(would return Toyota)

2 Question § Two-dimensional arrays


3 Write the pseudo-code to
Two-dimensional arrays are a little more complex than one-
create a two-dimensional array
dimensional arrays, but really they are nothing more than an
with the following numbers:
array of arrays, in other words an array in one row and another in
19 dpe: the next row. So now our eggs have row A and row B, each with 6
2,4, 6 eggs. Egg Al and egg B1 are different eggs.
30,0
4,8, 12

You can declare all elements of


an array in one statement.
Figure 19.2 Two-dimensional array of eggs

168
Two-dimensional arrays

2 Question The best way of understanding a two-dimensional array is to


think of it as a way of holding and accessing information within
4 Given the array shown below a matrix or grid made up of rows and columns. You may have
and an index of 1 what used this method to define a cell in a spreadsheet — you give the
number will tables[3][1] location by row and column.
equate to.
tables = |1t.2,.3],
[2, 4, 6],
(Son0];
[4, 8, 12]]

! Key point
The element in the first row and
second column in an array is
element 11. Rows are the first
dimension. Columns are the
second dimension.
Figure 19.3 Two-dimensional array

Let’s give this array the name letters. Notice that we have five
rows and four columns.
If you wished to define this two-dimensional array as a data
structure, in code you would write it as follows:

letters[5][4]

What we have here is the name of the array, letters, followed by


a declaration (in square brackets) of how many arrays there are
within the main array, which in this case is five (0—4) and finally
a declaration (again in square brackets) of how many elements
there are in each of the sub-arrays, which in this case is four (A,
BSCAD of EeG ail etc.)

The output from the array looks like this:


letters [O] [0] #returns element ‘A’

letters [O] [1] #returns element ‘B’

letters [O] [2] #returns element ‘C’

letters [0] [3] #returns element ‘D’

letters [1] [0] #returns element ‘E’

letters [1] [1] #returns element ‘F’

Note: This declaration will not work in all languages.


19 Data structures

In pseudo-code you use the following syntax to assign a value to


an element of a two-dimensional array. The red brackets < > are
only to show where you add something— you don’t need to put
them in your code.
Syntax:
SET Array [Rowlndex, ColumnIndex] TO <add value here>
Example:
SET arrayClassMarks[2,4] TO 33

We can even use this type of array to define an image. Think


about the pixels in a digital photograph.
It is simply a two-dimensional array. Conceptually, the pixel values
for any image would be represented as a two-dimensional array.
The number of columns corresponds to the width of the image (in
pixels) and the number of rows corresponds to the height of the
Because an array contains image (also in pixels).
similar data, you can use a loop
to perform similar operations on For example, an image that is 640 pixels wide by 480 pixels high
all the elements.
would be stored in memory as a two-dimensional array having
640 columns and 480 rows.
Often these two-dimensional arrays are used extensively in
gaming too.
Suppose you want to store information about a chess game. We
e Question want to store information about what pieces are in which locations.
5 Explain the difference between The most natural way to store it would be to index locations by
one- and two-dimensional row and column. This is easily done with a two-dimensional
arrays. array and to access the elements within your two-dimensional
array you would need to write a small iteration (looping) routine.

Figure 19.4 Close up of a colour photograph’s Figure 19.5 Chess board


pixels

170
Records and tuples

1 Identify the pieces in the


game in Figure 19.6 using a
two-dimensional array.
Figure 19.6 Chess board part way through game

§ Records and tuples

Records are a fundamental component of most data structures,


especially in linked data where the data is related in some way
and so belongs together. Records are dissimilar to arrays as the
number of fields is fixed and each field in a record can have a
different type, for example, a record might contain three integers,
a floating-point number and a character string. Each field has a
name to identify it. Records may also be stored in arrays. Many
computer files are organised as arrays of logical records.
You might use a tuple to store several related pieces of data about
a car owner and then a list to store a list of all the car owners
that use a garage. This is because tuples are fixed size but lists
are dynamic. In other words, a tuple cannot be changed, it is
immutable whereas a list can be changed, it is mutable.
@ You can’t add elements to a tuple but you can add to a list.
@ You can’t remove elements from a tuple but you can remove
from a list.
@ You can find elements in a tuple, since this doesn’t change the
tuple.
e@ You can also use the in operator to check if an element exists in
the tuple.
So the programmer has to decide whether the data structure
needs to be mutable or not. You may wonder why you would not
want it to be mutable, but immutable structures can be used for
things like dictionary keys, whereas lists cannot.
Inrelational database management systems, records are sometimes
called tuples. Python also uses tuples to group items into a
19 Data structures

single value. In Python, a tuple is similar to a comma-separated


sequence of values. Whilst Python tuples are not exactly the same
as records, they support the same sequence operations as strings.
An index operator is used to select an element from a tuple.
Remember, whilst a tuple is similar to a list it uses different
syntax and with tuples we cannot change elements. This makes
programs more predictable. But once Python has created a tuple
in memory, it cannot be changed.
Tuples are easy to create in Python. You give the tuple a name,
then after that the list of values it will contain. For example:
aa) #creates a zero-element tuple
print(a)
print(len(a))

Output would be:


()
@)

Also:

b = (‘one’) #creates a one-element tuple


print(b)
print(len(b))

Output would be:


(‘one',)
|

And:

c = (‘one’, 'two') #creates a two-element tuple


print(c)
print(len(c))

Output would be:


(‘one’, 'two’)
2
We can also easily create a grouped tuple:
pair = (‘man', 'woman'’) #create grouped tuple

(key, value) = pair #unpack grouped tuple

print(key) #display grouped variables


print(value)

172

SSS
Records and tuples

Output would be:


man

woman

Lists are extremely similar to tuples but we use different syntax


as we use square brackets. Lists are modifiable (the correct term
is ‘mutable’), so their values can be changed.
cats = ['Tom’, ‘Cheshire’, ‘Tabby’, ‘Lion’, 'Cheta’] #creates a list,
#NOT a tuple

cats = (‘Tom’, ‘Cheshire’, ‘Tabby’, ‘Lion’, 'Cheta') #creates a tuple,


#NOT a list

Chapter review

In this chapter we have built upon the last chapter and explored complex
data structures.
We also explored arrays and how they can be used.
Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain and apply data types (integer, real,
Boolean, char) (More on this topic in Chapter 18)
® Understand and be able to explain and apply data structures including
records, one-dimensional arrays, two-dimensional arrays.
20 Data and program validation
and veritication

You should:
2.4.1 understand how to write code that accepts and responds
appropriately to user input
2.4.2 understand the need for, and how to implement, validation

For any device to function correctly the input needs to be checked


prior to any process and output. The name for this is data
verification.

§ Verification and validation |


The terms verification and validation are commonly used in
software engineering and usually means two different types of
system analysis.

Validation
Verification

Figure 20.1 Verification and validation in action

174
Program validation
SSeS
SSS i rss sss sss

§ Program validation
4 Often programmers get errors Program validation is about asking are you building the right
because they do not validate system? Validation is therefore concerned with checking that the
the user input. A program system will meet the customer’s actual needs.
must be very careful whenever
it accepts external input. § Program verification
4 For the examination you will
Program verification is about asking the question are you building
need to understand the need
the system right? Verification is concerned with whether the
for, and how to implement,
system is well engineered, error free and so on. Verification will
validation.
help to determine whether the software is of high quality, but it
will not ensure that the system is useful.
? Question
Verification therefore includes all the activities associated with
1 Briefly describe the term producing high-quality software: testing, inspection, design
validation testing. analysis, specification analysis.
Validation and verification of data are two ways to check that the
data entered into a computer system is what is required.
1 Use this book and other
sources such as the internet c Data validation
to research how to implement
Validation is an automatic check performed by a computer to
validation.
ensure that entered data is sensible/feasible. It does not check the
accuracy of the data entered. The most common validation checks
are shown in the table.
| Typeofcheck Explanation. —
A Input validation is commonly Length check Checks than an entered data value is no longer or shorter
done with a loop that iterates than a set number of characters.
as long as an input variable Presence check Checks that some data has been entered into a field.
references bad data.
Range check For example, a user of a computer system is likely to
A Input validation is the process be aged between 3 and 110. The computer can be
of inspecting data that has programmed only to accept numbers between 3 and 110.
However, this does not guarantee that the number typed
been input to a program, to
in is correct. For example, a user could be 5 years old but
make sure it is valid before it say that they are 16.
is used in a computation.
Type check also Checks that the data is of a particular type, e.g. check that
A Validation checks that input called a format age data is numeric.
data is sensible, it does not check

check that it is correct.


There are a number of other validation types that can be used to
check the data that is being entered.
@ Cardinality check: checks that a record has a valid number of
related records.
® Check digit: the last one or two digits in a code are used to
check the other digits are correct.
@ Consistency check: checks fields to ensure data in these fields
corresponds, e.g. if Title = ‘Mr’, then Gender = ‘M’.
® Spell check: looks up words in a dictionary or array.
20 Data and program validation and verification

I Key point Why use validation in your code?


Your program could crash if incorrect data is input by a user, for
Unexpected user inputs cause
example if the user types a string when the code expects an integer.
errors or unexpected behaviour.
Your code may also produce incorrect results when processing the
data. Because of this we often use validation in coded solutions,
particularly when we have a user input.
For example in Python we could use:
def checking!nput():
2 Write the pseudo-code where
the user inputs the length of WAR SATINe:
the side of a square, and print try:
its area. Produce an error a = input(‘enter’)
message if the length entered ifa =="'y' or 1 <= int(a) <= 10:
by the user is negative. cetuinea

else:
print(‘Invalid input!’)
except ValueError:
print('Value error! Please try again!’)
If we want a user to enter a number greater than 18 for example,
we could use the following:
det valid_user_input():
try:
return int(input(/Maximum number: ')) > 18
except ValueError:
return False
while valid_user_input():
print('You have successfully entered a valid number’)

2 Oueation Validating the presence of data


One common error is no data in a field where data is required,
2 State four types of data
for example, id is frequently required, as is name. A pseudo-code
validation.
example testing for the presence of a name is shown below:
if name = spaces
report missing name
set invalid indicator to yes
end if

Validating data type


Often non-numeric data is put into a numeric field. However you
can validate for character data in a character field.
Assume that myNumber is a numeric field and you want to make
sure that no non-numeric data is entered in the field.

176
Data validation

Set invalid indicator to no prior to entering the validate routine.


if myNumber is not numeric
report non numeric data in myNumber
set invalid indicator to yes
end if
In Python, say we want someone to enter their age, but also want
to stop them entering something other than an integer as this will
3 Write the pseudo-code to ask cause an error. We could do this using the following code:
the user to input ten numbers,
while True:
and send to the screen their
total but any numbers over ye
100 should be ignored, i.e. age = int(input('Please enter your age: '))
should not be summed. except ValueError:
Write the pseudo-code to print(Sorry, | didn’t understand that.’)
ask the user to input two #better try again... return to the start of the loop
values into variables for start
mete continue
and finish, then send to the ee
screen the integers from start eee
to finish inclusive using a for #age was successfully parsed!
loop but, if the start is bigger #we’re ready to exit the loop
than finish, output an error break
message instead! l if age >= 18:

print('You are able to drink alcohol!’)


else:
print('You are not able to drink alcohol.’)

Validating uppercase characters


We will also assume that refCode should be an uppercase character
field. We could write the following:
IF refCode < ‘A’ OR refCode > 'Z' THEN
OUTPUT 'non-uppercase character in refCode'
invalid = TRUE
E NID
In Python we could use:
while True:
data = input('Please enter your first name in capital letters): ')
if not data.isupper():
orint('Sorry, your response was not in capital letters.’)
continue
else:
#we have validated the name is in capitals
#we can now exit the loop
break
20 Data and program validation and verification

Validating data codes


The valid titleCode may be Mr, Dr, Ms and those are the only codes
you want entered in that field.
IF titleCode = 'Mr' OR titleCode # 'Dr' OR titleCode #
‘Ms' THEN:
OUTPUT 'non-valid character in titleCode’
invalid = TRUE
END IF

Maximum and minimum length checks


For a maximum length check in Python we can use:
input_str = input('Please provide your first name: ')
if len(inout_str) > 8: #we use more than
print (‘Error! Only 8 characters are allowed!’)

Or for a minimum length check:


input_str = input('Please provide your full name: ')
if len(input_str) < 4: #we use less than
print('Error! You must enter a minimum of 4 characters!’)

Range checks
We can use similar code for a range check. Say we want to limit
the maximum age someone can enter:
myData = input('Please provide your age: ')
myData = int(myData)
#we must convert the user input to an
#integer
if myData > 120:
print(‘Error! You cannot be that old!')
Or the minimum age:
myData = input('Please provide your age: ')
vyData = int(myData)
if myData < 6:
print('Error! You cannot be that young!')

Data verification
Verification is performed to ensure that the data entered exactly
matches the data in the original source.
There are two main methods of verification:
© Double entry: data is entered twice and then a check is done
comparing the two copies.
© Proofreading data: this method involves someone checking
the data entered against the original document.

178
Data verification

Chapter review

In this chapter we have explored data verification and validation.


Remember, before tackling any computer science task or examination
question on this topic you must:
™® Understand and be able to explain the need for validation
® Understand and be able to implement validation
® Understand and be able to write code that accepts and responds
appropriately to user input (More on this topic in Chapters 2, 3, 6, 7,
Omid, 11) :
cs Specification references
You should:
3.3.1 understand how to convert between the terms ‘bit, nibble, byte,
kilobyte (KB), megabyte (MB), gigabyte (GB), terabyte (TB)’
3.3.2 understand the need for data compression and methods of
compressing data (lossless, lossy) and that JPEG and MP3 are
examples of lossy algorithms
understand how a lossless, run-length encoding (RLE) algorithm
works
understand that file storage is measured in bytes and be able to
calculate file sizes

§ Analogue and digital data


Computing systems can only store a limited amount of
information, even if the limit is very large. To be able to fully
understand data representation you need to first understand the
differences between digital and analogue data. Analogue data is
continuous, analogous to the actual information it represents.
For example, a mercury thermometer is an analogue device. The
mercury rises in direct proportion to the temperature. Computers
cannot work with analogue information. Digital data breaks the
information up into separate steps.

§ Bits and bytes


Bit (b) A bit has a value of 1 or 0.
Byte (B) A byte is eight bits.
We use bytes to measure file sizes.

180
Bits and bytes

| ! Key point Microsoft decided to use the following conventions for file size
and now the whole world uses them:
The magic numberto remember
used to be 1024 but now it’s
1000. kibibyte (KiB) kilobyte (kB) 1024 bytes

mebibyte (MiB) megabyte (MB) 1,048,576 bytes (or it’s easier to


remember as 1024 Kibibyte)

gibibyte (GiB) gigabyte (GB) 1024 megabytes or 1,048,576


kilobytes

tebibyte (TiB) terabyte (TB) 1024 gigabytes or 1,048,576


megabytes

Now base-10 definitions have been officially standardised,


software is phasing out the base-1024 usage and often refers to
powers of 10. The only place you will find the computer industry
2 Question referring to powers of 1024 is in regard to the size of RAM modules.
The new base-10 definitions are shown below.
1 What is the mathematical
difference between a
megabyte and a terabyte? kilobyte (kB) 1000 bytes

megabyte (MB) 1,000,000 bytes (or it’s easier to remember as 1000 kilobytes)

! Key points gigabyte (GB) 1000 megabytes or 1,000,000 kilobytes

terabyte (TB) 1000 gigabytes or 1,000,000 megabytes


A For the examination you will
need to understand how to
Just to give you an example of what these numbers all mean, if we
convert between the terms
stored photos of 3 MB in size on a hard drive with these memory
bit, nibble, byte, kilobyte (KB),
volumes this would be the outcome:
megabyte (MB), gigabyte (GB),
terabyte (TB). |Hard drive capacity | Number of 3 MB digital images that could be stored
A Data compression is a 1 kilobyte (kB) None
reduction in file size to reduce
1 megabyte (MB) None
_ download times and storage
1 gigabyte (GB) 341
requirements.
1. terabyte (TB) 349,525
1 petabyte (PB) 357,913,941
2 Questions
1 exabyte (EB) 366,503,875,925
2 What is TB the abbreviation 1 zettabyte (ZB) 375,299,968,947,541
for?
1 yottabyte (YB) 384,307,168,202,282,000
3 Put these in order from
smallest to largest: gigabyte, This assumes that our photos are only 3 MB in size; RAW
kilobyte, megabyte, petabyte, photographs can be over 100 MB in size. To achieve a size of 3 MB
terabyte. the file must be compressed.
21 Data size, storage and compression

§ Data compression
Data compression is a set of steps for packing data into a smaller
‘electronic space’ (data bits), while still allowing for the original
data to be accessed and used. This is often achieved by eliminating
the repetition of identical sets of data bits (redundancy).
Compression results in much smaller storage space requirements
and often much faster communications.

J Key points Run-length encoding


We know that compressed data works more effectively on our
For the examination you will need
mobile phones and portable computers, so now we need to learn
to understand:
how this works. First let’s look at a bitmap image. You will have
A that file storage is measured in learnt about bitmapped images in the last chapter.
bytes and be able to calculate
file sizes Run-Length Encoding (RLE) is a data compression algorithm
supported by most bitmap file formats, for example TIFF, BMP
A the need for data encryption.
and PCX.
RLE is suitable for compressing any type of data, but the content
of the data affects the compression ratio achieved so let’s explore
what it is best used for by looking at how it works.
RLE works by reducing the size of a repeating string. A repeating
string is called is a run, and is typically encoded into two bytes.
The first byte represents the number of characters in the run and
is called the run count.
The second byte is the value of the character in the run, which is
] Key point in the range 0 to 255, and is called the run value.
For the examination you will need
Let’s look at how this works.
to understand how a lossless,
run-length encoding (RLE) A string character run of 20 X characters:
algorithm works.
XXXXXXKXKXXXXXKXKKK
XX KK
would require 20 bytes to store the string in its uncompressed
form.
The same string after RLE encoding would require only two bytes:
A character is a single alphabetic
Or numeric character. 20X
The 20X code generated to represent the character string is called
an RLE packet.
In our example the first byte, 20, is the run count and contains the
number of repetitions.
The second byte, X, is the run value and contains the actual
repeated value in the run.

182
St
Data compression
ss ss sss

It is like going to a restaurant with 20 of your friends; if there are


four things on the menu, the waitress will not write down a long list
of 20 orders, she will simply write the menu item and put alongside
this how many people want each item, for example burger x 6.
A new packet must be generated each time the run character
changes, or each time the number of characters in the run exceeds
the maximum count.

Compressing images
The most straightforward way of storing a bitmap is simply to list
Lossless and lossy compression
the bitmap information, byte after byte, row by row. Files stored
are terms that describe if, in the
by this method are often called RAW files.
compression of a file, all original
data can be recovered when The following is a description of the simplest lossless compression
the file is uncompressed. With technique called run-length encoding (RLE) on a simple black
lossless compression, every bit and white image. As stated this works best for bitmaps with only
of data that was originally in the a few colours. Consider the following small, 16 x 10 pixel, 1 bit per
file remains, nothing is lost, when | pixel image of the pi sign.

ee SSS eas 3

Figure 21.1 A simple bitmap image

If this was to be stored in RAW form it would need 16 bits per


row for all 10 rows. The first two rows and the last two rows are
the same colour so it is much more efficient to simply save the
number of same colours in a run along with the run colour. If we
do this the first two rows and the last two rows would need only
2 bits for each row not the full 16 bits.
21 Data size, storage and compression

In RAW format this would be:


0000000000000000
000D0000000000000
OO TL Tp etal alee at ON
0000100000010000
0000100000010000
0000100000010000
0000100000010000
0000100000010000
0000000000000000
00D00000000000000
Using run-length encoding the first three rows would be:
160
160
2 Ogle ie240)
While there are more details involved in actual implementations
of RLE than described here, and our image only uses a few bits
because we have not added colour depth to each pixel, this is the
basic principle behind run-length encoding of an image.

Text
Of course long runs are rare in certain types of data. For example,
! Key point
it is unlikely that our example of 20 of the same characters would
ASCII (American Standard Code occur in a typed document. In a standard ASCII document only a
for Information Interchange) few words would even have two characters the same. To encode
is a /-bit system to code the a run in RLE requires a minimum of two characters worth of
character set on a computer. information as a run of single characters actually takes more
space. Even data consisting entirely of 2-character runs would
remain the same size after RLE encoding.
If only two of your friends go to the same restaurant, and each
person orders a different item from the menu, there is no saving
of time or space for the waitress to add how many people require
each item.
But if we compare an ASCII document with a black and white
image that is mostly white, such as a scanned page of a book, it
will encode very well, due to the large amount of contiguous data
that is all the same colour — white.
Anything with relatively few runs of the same colour will not
work as well as images with large areas of the same colour.

184
al
er
Data compression <~-g

SS SSS sss sss set ..


Fe
£ >

Before we look at how to deal with images which do not lend


themselves to this type of encoding, it is worth noting that
different algorithms can store the RLE data in different ways.
There are a number of variants of run-length encoding.
In the sequential processing we have just looked at, a bitmap is
encoded starting at the upper left corner and proceeding from
left to right across each scan line to the bottom right corner of
the bitmap. Each scan line is separated by a code to help put the
image back together.
But alternatives to simply going from left to right exist such as
those shown in the image.

Encoding along the x-axis Zig-zag encoding


1S SS ee ae 7
Ge a ee

ooo
———e———e———o——ooToosa—oo
SS

Soe Cy

Encoding along the y-axis

LE
Encoding (4 X 4 pixel) tiles

SS ce eae
rea eae ee

ae oain Pepe

Figure 21.2 Run-length encoding variants

Dictionary-based compression
For an easy-to-understand example of compression with text, we
will look at a sentence:
‘Run length encoding makes files smaller; smaller files use run
length encoding.’
Now imagine this as a file. If each character and space in this
sentence made up one unit of memory, the whole thing would
have a file size of 79 bytes.
7 21 Data size, storage and compression

There are patterns in our sentence. Most words in the sentence


appear twice. Only ‘makes’ and ‘use’ appear once. But to make
use of this pattern we would need to create a dictionary, which
is simply a way of cataloguing pieces of data, in this case words.
If we created a dictionary using a numbered index to represent
each word, it could look something like this:
run
length
encoding
makes
files
smaller
FS
NS
WwW
FP
OF
NO use
We could now write the sentence using our numbered index as:
123-4
iG; Oo 123
The compressed file would use less memory. In practice it will be
a little larger as we will need to store our index alongside the data
or it will be impossible to read the file, but you will have a good
understanding of how this system could work.
The way in which actual compression algorithms work is a
little more complicated than in our example. Most compression
algorithms recognise other types of patterns.

Lossy compression
So far we have seen how all the data in a string can be compressed
without losing anything. The name for this type of algorithm is
lossless = we lose nothing. But we also know that it will only work
where data is repeated in the string. So what can we do with data
which is not like this?
If you take a photograph of a country view, large parts of the
picture may look the same — the whole sky is blue and the grass
is green, for example — but most of the individual pixels are
a little bit different. The lossless system will not compress the
file very well; it could even make the file larger. To make this
picture smaller without compromising the resolution, you would
have to change the colour value for certain pixels to make them
the same before compression. If the picture had a lot of blue sky,
the algorithm could pick one colour of blue that could be used for
every pixel and replace all the variations with the same colour.
If the compression scheme works well, you would not notice the
difference, but the file size could then be significantly reduced.
You would simply lose, hence lossy, some of the more subtle
changes. The more you lose the better the compression.

186
Understanding bandwidth nal

Lossy compression is not just used for images. MP3 sound files
use the same system, disregarding small changes in sounds that
that a listener will not perceive. Again most listeners will never
e Question notice the difference, but to a connoisseur, MP3 could never
4 What is file compression? replace live music or uncompressed sound files, even if they do
take up a large amount of storage space.

§ Understanding bandwidth
We have looked at file size and how images, sounds and text can
be compressed using an algorithm. But most data also needs to be
communicated over wired and wireless networks. If you imagine
a road network, the data size is the size of the vehicle or vehicle
load which you wish to transport. But also important is the size of
the roads, and in computer terms this is called bandwidth as it is
the amount of traffic that can flow at the same time.

Chapter review oxi


In this chapter we have explored data size, storage and compression.
Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to convert between the terms bit, nibble,
byte, kilobyte (KB), megabyte (MB), gigabyte (GB), terabyte (TB)
® Understand and be able to explain the need for data compression
® Understand and be able to explain methods of compressing data
including both lossless and lossy
® Understand that JPEG and MP3 are examples of lossy algorithms
® Understand and be able to explain how a lossless run-length
encoding (RLE) algorithm works
® Understand and be able to explain how file storage is measured in
bytes and be able to calculate file sizes.
You should:
3.5.1 understand the characteristics of structured and unstructured data
3.5.2 understand that data can be decomposed, organised and
managed in a structured database (tables, records, fields,
relationships, keys)

F Characteristics
Databases use a series of tables to store the data. A table simply refers to
a two-dimensional representation of data stored in rows and columns.
For example:
Steve Cushing [email protected]

John Wilson [email protected]

Jess Hadden [email protected]

So we have a table with rows containing a person’s details, and columns.


First, each table needs a unique name so that the Database
Management System (often referred to as DBMS) can find the
right table; we will call this table ‘contacts’.
] Key point Next each column needs a name.
For the examination you will
need to understand that data
Steve Cushing [email protected]
can be decomposed, organised

database (tables, records, fields, Jess Hadden [email protected]


relationships, keys).
To make the database usable we need to add a unique key to the
table. Most database developers create their own column with
a computer-generated unique number to act as the unique key,
referred to as the database’s key field.

Steve Cushing [email protected]

John Wilson [email protected]

Jess Hadden [email protected]

188
Databases are like spreadsheets... aren't they?

We could just keep adding fields for any additional data. This
would be called a flat file database, but these can be very slow, so
most professional databases have different tables linked together.
Let’s look at how that works.
Each of the people in our table buy a product from an online shop.
The company produces a table of sales information.

product_name product_code = ftem ie


10 Rise and Fall B607

2 Times Square H766

ab Home L678 1003

Now we need to link the two tables together, to relate the data in
one table with another. This is where the relational database gets
its name.

1001 1004

1002 1002

1003 1003

So by referring to the linking table above you can see that Jess
Hadden purchased 1 copy of the product L678 Home.
You are probably wandering why the item_id and key are the same.
What happened to the unique key field? In relational databases
nothing is unique in combined tables. The second key is called a
foreign key which means that the number used in a foreign key
column is not necessarily unique to the linked table, but it must
be unique to the table it is referring to.
In addition to the comprehensive functionality of a relational
database there is aneed to not only develop but also to maintain the
database. IT personnel such as data analysts, database designers
and database administrators all need the ability to modify the
data within a database to produce useful information for both
day-to-day organisational activities and for long-term planning.

Databases are like spreadsheets... aren't


they?
Most of us are familiar with spreadsheets. We find them simple to
create and they are often used in schools, much like a database,
but is this entirely correct? Let’s analyse the features and
functionality of spreadsheets to investigate how good a database
tool they actually are.
In a similar way to databases, spreadsheets are frequently
used to capture and store information in tables. They have the
;a
a
es
4
i"

22 Databases
4

ee $$
= N
ee
capability to link table cells on one worksheet to those on another
worksheet, and can specify that data be entered in a particular
cell in a particular format. It is also a simple operation to calculate
formulae from a group of cells on the spreadsheet, create charts
and process data in other ways.
Having said this, there are many areas in which spreadsheets are
nothing like databases and the table below highlights this.

Cells can be formatted as a formula Database columns have a fixed value

A number of different data types can be In a column of a database only a single


stored in a column of a spreadsheet data type can be stored

Accessible to only one user at any one Accessible to a multiple of users at


time any time as well as offering a choice
of ‘read and write’ permissions across
various areas of a database

Corrupted spreadsheets cannot usually There is a range of tools for repairing


be repaired databases

No way of making a row (record) unique Individual rows within databases have
the capability to be identified by a
unique ‘primary key’

fyRelational databases
As we have seen, a relational database is a group of data items
assembled into a set of formatted tables in which data can be
searched, accessed and interrogated in a multitude of different
ways without having to reorganise the tables.
The standard user interface to any relational database is the
structured query language (SQL). By using SQL statements,
interactive queries for information can be made for the purpose
of retrieving information from a relational database and for
gathering data for reports.
As well as being quite simple to create and access, a relational
database has the significant benefit of being relatively
straightforward to extend. After the original database is produced,
a new data category can be included without the need to modify
all the existing applications as new tables can be added and linked.
As mentioned, relational databases are a set of tables consisting of
data organised into predefined categories. The tables, which are
sometimes referred to as relations, are comprised of one or more
categories that are arranged into columns. Each row contains a
unique instance of data for the categories defined by the columns.
Databases can be designed so that different users see a bespoke
view of the database that is customised to suit the user’s needs.
For example, a sales manager might wish to view a report on all

190
ee Components

customers that had bought products after a certain date, whereas


a finance manager within the same company could, from the
same database, acquire a report on accounts that need to be paid.
This is achieved by simply relating the tables needed by that user.

§ Components
The foundation for any relational database management system
(RDBMS) is the relational model and this has three basic
components:
® a store
® a method of creating and retrieving data
® a method of ensuring that the data is logically consistent.
Let’s look at the fundamental components of a traditional
relational database system and how it is designed.
A table in a relational database is also referred to as a ‘relation’. It
is a two-dimensional structure used to store related information.
A relational database consists of two or more related tables.

In databases, records are a complete single set of information.


Records are comprised of fields (see below). A set of records
constitutes a file. For example, a personnel file of employees
might contain records that have fields such as age, gender, house
number, street name, town, telephone number, national insurance
number etc.
A row within a ‘relation’ table is an instance of one record, such
as one employee and their respective details that are contained
within the fields of the records (see below).
Columns within a database table contain all the information of
a single type, such as all the employees’ names, all the phone
numbers or all the address details. As part of the validation and
verification of the information, columns are usually formatted to
accept certain types of data such as integers, Booleans, decimals
(to a stated number of decimal places) or strings.
Within relational database tables, a field is a single snippet of data
that is at the intersection of a row and a column. It is the smallest piece
of information that can be retrieved using the database’s structured
query language (SQL) and forms part of an individual record.
A database query is fundamentally a question that you put to
the database. The outcome of the query is the information that is
returned by the database in answer to the question. Queries are
created using SQL, which looks like a high-level programming
language.
Every relational database should contain one or more columns
that are assigned as the primary key. The important and crucial
22 Databases

fact for the primary key to work is that the value it holds must be
unique for each of the records contained within the table.
If we take our personnel database example referred to above,
we know that it contains personal information on all our
organisation’s employees. Now, we need to choose a suitable
column of information, where each field within that column can
uniquely identify each employee’s record, which can act as our
primary key. So why don’t we choose the column containing the
employee’s surname?
Well, this would not work correctly because there may be two
or more people working within the business called Jones, Davies
or Smith. The same problem would occur if we used the column
containing first names or street names. You could use national
insurance numbers, however, their use is quite controversial as a
consequence of certain privacy issues. A better idea would be to
create a column with unique employee identifiers and, these days,
this is what most companies do.
When the primary key has been selected and built into the database,
the database management system will ensure the uniqueness of
the key, so that should you attempt to add a duplicate record with
a key that already exists within the table, the system will disallow
the addition and present you with a warning window.

§ Relationships
Database relationships work by comparing data in key fields. This
occurs between fields that have corresponding names in both
tables. In almost all cases, the fields contain the primary key for
one of the tables, which then supplies the unique identifier for
each record and the ‘foreign’ key in the other table. The foreign
key is a column identified to establish a connection between the
data in two tables.
A link is established between two tables by adding the column
that holds the primary key in the first table to the other table.
The duplicated column within the second table then becomes the
foreign key.
Although the most important responsibility of a foreign key
is to manage the data that is stored in the foreign key table, it
additionally manages changes to data in the primary key table.
The link between the two tables ensures that data integrity is
maintained between the two tables by ensuring that alterations
cannot occur to data in the primary key table, if the alterations
undermine the link to the data in the foreign key table.
If someone tries to delete the record row in a primary key table
or to alter a primary key value, an error will occur if it is linked

192
Index
SSS SSS SSS SSS sss

to a foreign key value in another table. In order to carry out a


successful alteration or deletion to a record ina foreign key table,
initially, you should either delete or alter the foreign key data
contained within the second table, therefore linking the foreign
key to different primary key data.

BiIndex
Database indexes assist database management systems to find
and sort records more quickly. It is really important that when
you produce a database, you should also create indexes for the
columns used in queries to find data.
Indexes in databases can be compared to indexes in books. If you
look at a book’s index, it enables you to find information you want
quickly without having to read through the entire book. Within
a database, the index enables the database software to search for
and find data in a table without having to scan the whole table.
Indexes can be based on a single field or on multiple fields. Indexes
that utilise a number of fields enable the user to distinguish
between records in which the first field may have the same value.
It should be realised, however, that even though indexes greatly
improve the efficiency of a database, tables that possess indexes
need more storage space within the database. Also, the commands
that insert, update or delete data within databases need more
processing time.
When choosing the fields to use for indexes, you should consider
using fields you search frequently, or fields you sort regularly, or
fields that you combine frequently with fields in other tables when
queries are created. It should be realised that if a large number of
the values within a field are identical, then an index might not
significantly improve the speed of queries.
Primary keys within tables are automatically indexed.

Chapter review

In this chapter we have explored databases.


Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain the characteristics of structured
and unstructured data
® Understand and be able to explain how data can be decomposed,
organised and managed in a structured database using tables,
records, fields, relationships and keys.
Bs Specification reference
You should:
2.4.3 be able to write code that reads/writes from/to a text file

Principles of writing to / reading from


a text file
To write to an external file we need to understand the term
delimiter. A delimiter is characters used to specify the boundary
between regions in a plain text file. An example of a delimiter
is the comma, used to separate fields in a sequence of comma-
separated values (CSV). In programming pipe-delimited (| ) text
files are also common.
A delimiter is simply a character used to denote the end of one
value and the beginning of the next. It is needed because the
values for each variable may be in different locations on each
record. The advantage of the pipe over comma or space delimiters
is that the pipe is not used very often in the actual data values. In
a text file the end of a line is also a delimiter. We can read the file
based upon lines delimited by line breaks.
When you declare a variable for a text file you usually create the
variable and open the file at the same time.
For the examination you will need
to be able to write code that §iCreating a new file
reads from / writes to a text file. If you want to create a new file called computerScience.txt you could
use this pseudo-code:

Open 'computerScience.txt' #computerScience.txt is


1 Use this book and other For output as compScienceFile #the file
sources such as the internet #name and compScienceFile
to research how to write code #is the variable name you
that reads from / writes to a #wish to place it into
text file.

194
Open an existing file and changing it

§ Open an existing file and changing it


If you want to open an already existing file you could use this
pseudo-code:
Open 'computerScience.txt' For Append as compScienceFile
You could then add or change the file using this pseudo-code:
Open ‘computerScience.txt' For Input as compScienceFile

So once opened you can add to the variable:


Write compScienceFile, 'Steve',{Cushing','Grade A’

If you already have specified variables you would use these to add
the values like this:
Write compScienceFile, 'fName','IName’','grade'
Read compScienceFile
Basically this reads an entire file into your code.
computerScience[ ] = Read compScienceFile
Or if you want to just read each line at a time you could use
something like this (assuming that the file contains that data in
the order below):
Read compScienceFile
fName = compScienceFile[1]
IName = compScienceFile[2]
grade = compScienceFile[3]
End Read
Each time the above code runs it will put one line of the file into
those variables.
In pseudo-code you use the following syntax to read in a record
from a <file> and assign to a <variable>. Each READ statement reads
a record from the file. The red brackets < > are only to show where
you add something — you don’t need to put them in your code.
Syntax:
READ <File> <record>
Example:
READ myFile.doc Record
mm > 23 Reading and writing to a text file

In Python |
Python has a built-in open() function. This takes a filename as
an argument. Let’s first create a new text file. We could name it
anything we like; in this example we will name it testdoc.txt. We
can also add a combination of a directory path and a filename.
The open() function will only take one argument but, whenever
you need a filename, you can include some or all of a directory
path. The directory path does not begin with a slash or a drive
letter as it is a relative path
#write to a file
with open(‘testdoc.txt', 'wt') as out_file:
out_file.write('This is Computer Science GCSE\nHodder
] Key point Education\nSteve Cushing\n Edexcel’)
Python has built-in methods for #now we will read our file
reading a whole file which makes with open('testdoc.txt’, 'rt') as in_file:
it easier than reading one line at text = in_file.read()
a time.
print(text)

Now look at the output from this file:


Computer Science GCSE
Hodder Education
Steve Cushing
Edexcel
Notice how the \n command adds the text to a new line.
#write to a file
with open('testdoc.txt’, 'wt') as out_file:
out_file.write(‘This is Computer Science GCSE\nHodder
Education\nSteve Cushing\n Edexcel’)
#read a file

If we only want a few characters from our text file we can use:
with open('testdoc.txt’, 'rt') as in_file:
text = in_file.read(1)
print(text)

This outputs the first letter in memory which is T.

196
SSS SSS
In Python
SSS

If we add more letters:


with open('testdoc.txt', 'rt') as in_file:
text = in_file.read(12)
orint(text)

Output:
This is Comp

Or, to give us more flexibility we can read the lines of our text
directly into an array:
with open(‘testdoc.txt'’, 'rt') as in_file:
content = in_file.readlines()
print(content)

Output:
[This is Computer Science GCSE\n', 'Hodder Education\n’,
‘Steve Cushing\n', 'Edexcel’]

Now we have an array we can manipulate it as any other array to


get the data we require from the text file.
If you want to take out the line breaks or strip whitespace you can
use the following code:
for line in open(‘testdoc.txt’) :
print line.rstrip(‘\n') # .rstrip('\n') removes the line break

Output:
This is Computer Science GCSE
Hodder Education
Steve Cushing
Edexcel

Chapter review aad]

In this chapter we have explored reading and writing to an external text


file.
Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand how code can be used to read from / write to a text file
® Be able to write code that reads from / writes to a text file.
E23Specification references
You should:
1.1.5 understand how to identify and correct errors in algorithms
2.1.3 be able to differentiate between types of error in programs
(logic, syntax, runtime)
2.1.4 be able to design and use test plans and test data (normal,
boundary, erroneous)
2.1.5 be able to interpret error messages and identify, locate and fix
errors in a program
2.1.6 be able to determine what value a variable will hold at a given
point in a program (trace table)
2.1.7 be able to determine the strengths and weaknesses of a program
and suggest improvements
3.4.1. understand the need for data encryption
3.4.2 understand how a Caesar cipher algorithm works

§ Algorithms in security
If you program anything that works over the internet and needs
to handle confidential information you will have to use what are
called ‘cryptographic algorithms’ to keep the system secure.
Cryptographic algorithms are sequences of rules that are used to
encrypt and decipher code. They are algorithms that protect data
by making sure that unauthorised people can’t access it.
2 Question Most security algorithms involve the use of encryption, which
1 Briefly describe the term allows two parties to communicate but uses coded messages so
hackers. that third parties such as hackers cannot understand the
communications.
Encryption algorithms are used to transform plain text into
something that cannot be understood. The encrypted data is then
decrypted to restore it, making it understandable to the intended
party.

There are hundreds of different types of cryptographic algorithms.

198
sss sss
Caesar cipher
sss

i Caesar cipher |
Caesar ciphers are symmetric. There is one shared key. In a Caesar
cipher, an algorithm replaces each letter in a message with a letter
further along in the alphabet using a number key.
e Key point The cipher simply shifts the alphabet and is therefore also called
For the examination you will need a shift cipher. You need to know what the number key is to
to understand how a Caesar decipher the message. The number key is simply the number of
cipher algorithm works. letters you shift.
The Caesar cipher is one of the oldest types of cipher. It is named
° Question after Julius Caesar, who is said to have used it to send messages
to his generals over 2000 years ago.
2 Briefly describe the term
encryption. Let’s look at a couple of examples.
With a shift key of 1:
Riddle: What is the clumsiest bee?
Answer pee Van CMJOF Ger

The answer is a bumbling bee!


With a shift key of 2:
Question: Decipher the following Caesar cipher: gzco swguvkqp
Answer: exam question

Each letter in the first encrypted message above is just the next
letter along in the alphabet because the key is 1.
If we look at this mathematically the encryption formula is:
E (x) =(x+n) mod 26

and here is the decryption formula:


D(x)
n
=(x-— n) mod 26

where the alphabet we are working with contains 26 letters


(English) and n is a number less than or equal to 25.
You may be puzzled by what the mod function is and why you
would ever want to use it. Mod is short for modulo (modulo means
the remainder, e.g. 10 mod 3 = 1 because 10 / 3 = 3 remainder 1).
Integer division functions/operators are found in nearly all
programming languages. Once you start to write real code you
will certainly meet it.
If we consider a clock face with numbers from 0 to y — 1 then
counting mod y would be a process of advancing from number
to number until you reach y — 1 then you ‘roll over’ to 0 and start
the sequence again. So counting mod 6 would be 0, 1, 2, 3, 4, 5,
0, 1, 2, ... etc. Notice that in counting mod 6 the integer 6 doesn’t
EVEISOCCUT:
24 Encryption
ECT

These rules can be modified for any alphabet language of your


choice. If the alphabet contains m letters, then n has to be less
than or equal to m — 1. Here is a general outline and picture of
how the algorithm above works. You should be aware that for
our example here the key n is set to 3, but it will work with any
number.
1 Assign numeric values to each letter in the alphabet.
2 Enery pt:
e for each letter in plain_text
e new letter is letter_numeric_value + n
3 Decrypt:
¢ for each letter in cipher
e plain_text_letter is letter_numeric_value - n

Figure 24.1 A cipher with a key of 3.

So to create an encrypted text we need to create a shift.


Ask user for the number key:
® createShift
Ask user for text to convert:
® convert string
® create index

Calculate the new letter:


® index + shift

We also need to consider what happens if the index + shift is past


the letter Z. In this instance we need to return to the start of our
alphabet by deducting the length of the alphabet.
Let’s look at how this simple encryption could work in Python 3:
import string
def createShift(shift, convert):
alphabet = string.ascii_lowercase
finished = "'
for char in convert:
if char in alphabet:
for index, alphChar in enumerate(alphabet):
if char is alohChar:

200
Encryption and networks

if indextshift < len(alphabet):


finished += alphabet[index+shift]
else:
finished += alphabet|[(index + shift) - (len(alohabet))]
break
else:
finished += char
return finished
falnante: ==. main
shift = int(input('What is the shift key? '))
1 Write a program in your chosen convert = input('What text would you like to convert? ') .ower()
code to create AND decode a print(‘\n' + createShift(shift,convert) + '\n')
Caesar cipher. The user should
enter the key and the text. Note how the programmer has forced the user input into lower
case ASCII.

§ Encryption and networks


The main purpose of encryption is to protect the confidentiality
of digital data that is stored on computer systems and to be
transmitted via the internet or any other computer network.
Modern encryption algorithms play a vital role in security as they
provide not only confidentiality, but also the following essential
elements of data security:
® Authentication: the origin of a message can be verified
® Integrity: proof that the contents of a message have not been
changed since it was sent.
Most data transmitted over a network is sent in clear text, making
it easy for unauthorised persons to capture and read the sensitive
information. By using encryption algorithms, networks can
protect data from intruders and make sure that only the intended
recipient can decode and read the information.

Chapter review

In this chapter we have explored encryption.


We also explored the Caesar cipher.
Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain the need for data encryption
® Understand and be able to apply a Caesar cipher algorithm.
25 Search and sort algorithms

a Es Specification §iSearching
reference
There are two types of searches used in computing, linear search
You should:
and binary search.
=, 1.1.8 understand how standard
algorithms (bubble sort, Linear search
merge sort, linear search, A linear search is the most basic search algorithm. A linear search
binary search) work sequentially moves through your collection (or data structure)
-
looking for a matching value.
Let’s look graphically at how a linear search works. We will search
epiulestOmthe keye 25.
Start here ... go through these, to the end ... Stop

14°38 10 —2 615

to Find (eae

Figure 25.1 Searching for ‘25’


We will not know that there wasn’t a record in the file that matched
the key until the end of the search.
The number we are looking for is called the key. If we have a key
of 3 we could show this as in the diagram below where each line
is the next loop in the search.
Key List
Cl: BRE
CED El«
[+B l7 [2]5)
in: Gee
ioge Be
Bonne:oo
Figure 25.2 Searching for ‘3’

202
eFC
Searching

When and if the code finds the key, it stops. As you discovered in
the first example, when it can’t find the key it keeps going until
the end.

In pseudo-code it could look like this:


Get the search criterion (key in this instance the number 3)
Get the first record from the file
While ( (record <> key) and (still more records) )
Get the next record |
End_while
If (record == key )
Then success
Else there is no match in the file
End_else

Creating a linear search in Python:


def linearSearch (myltem, myList): #define our linear search
#function
found = False
position =0Q
while position < len(myList) and not found:
if myList[position] == myltem:
found = True
position = position +1
return found
Neenane. == main:
cards = ['King Spades’, ‘Queen Spades’, 'Ace Spades’,
‘King Hearts’, 'Queen Hearts’ #create a list
item = input (‘What card do you want to find? ')
#ask user for card to find
isFound = linearSearch(item, cards)
if isFound:
print(‘Your linear search has found the card requested’)
#if found is True
else:
print('Your card is not in the list.’) #if found is False

Output (based upon entering Ace Spades as user input):


What card do you want to find? Ace Spades
Your linear search has found the card requested
25 Search and sort algorithms

Comparing searching a sorted list and


an unsorted list
If the order was alphabetical on last names, how would the search
for Anthony Adams on the ordered list compare with the search
on the unordered list?

~ Unorderelist
d ' 1 — Ordered list

Carole Cren Anthony Adams

Xian Xui Carole Cren

Steve Cushing Steve Cushing

Anthony Adams Xian Xui

Unordered list:
@ If Anthony Adams was in the list?
@ If Anthony Adams was not in the list?
Ordered list:
@ If Anthony Adams was in the list?
@ If Anthony Adams was not in the list?
How about searching for Xian Xui?
Unordered list:
® If Xian Xui was in the list?
® If Xian Xui was not in the list?

Ordered list:
e If Xian Xui was in the list?
e If Xian Xui was not in the list?

The search is faster on the ordered list if the item is not in the list,
as the ordered list search can terminate sooner because it does
not have to search to the end of the list.

Binary search
If we need a faster search, we need a completely different
algorithm.
If we have an ordered list and we know how many things are in
the list (i.e. number of records in a file), we can use a different
strategy.

The binary search gets its name because the algorithm continually
divides the list into two parts.

204
Searching

It looks at the centre value and disregards anything below or


above what we are trying to find. Let’s say we are looking for
value 6 from 7 ordered items. The red shows the place where
the list is being divided. Orange shows the active items and
white the discarded items.
So each time you get to discard half of the remaining list.
However, the list has to be sorted before we can search it with
binary search. To be really efficient, we also need a fast sort
algorithm or to build the list in a way that maintains its order.
Figure 25.3 Binary search for ‘6’ If we have a sorted list the following Python code performs a
binary search.
def binarySearch(list,element):
if len(list) ==
return element == list[O]
mid = int(len(list)/2)
if list{ mid] > element:
return binarySearch(list[:mid], element)
if list[ mid] < element:
return binarySearch(list[mid:], element)
return True
array = [1, 2, 3, 4, 5, 6] #define our sorted array
num = 4
print(binarySearch(array, num))

Output:
True

Comparing a linear search with a binary search


As long as the list is sorted, in almost all cases, a binary search
is a more time-efficient algorithm than a linear search. A linear
search looks down a list, one item at a time, without jumping. The
time it takes to search the list gets longer at the same rate as the
list gets longer. A binary search works in a similar way to how you
use a dictionary. You start with the middle of a sorted list (with
the first and second letter of the word you are trying to find),
and see whether that’s greater than or less than the value you're
looking for, which determines whether the value is in the first or
second half of the list. If you’re looking for ‘computer’ you don’t
start off at ‘A’ in the dictionary. The number of search operations
grows more slowly than the list does, because you're halving the
‘search space’ with each operation.
: 25 Search and sort algorithms

Because of this:
@ Binary search requires the input data to be sorted but linear
search does not.
@ Binary search requires an ordering comparison; linear search
only requires equality comparisons.
@ Binary search requires random access to the data; linear search
only requires sequential access.

§ Searching for the highest value


Let’s say you have a large number of playing cards,
each card has a number written on it, and the cards
have been shuffled and put in a pile we will call a
stack. You have been asked to find the card with
the highest number on it but you don’t know that
this is the ace of spades card. You can only move
one card at a time from the top of the stack and you
must then re-stack the cards in a new stack after
you have read them.
So you would remove a card from the top of the
stack, and then write the number on a piece of
paper. Then you would take the next card, and if
the number is higher than the number written on
the piece of paper, you will cross it out and write
the new number. You will keep doing this until the
stack was empty.
We could represent this as a flowchart (see Figure
25.4) or as pseudo-code.

Print Largest

Largest
new number |

Figure 25.4

206
Sorting
SSS
SSS SSS SSS SSS

§ Sorting
Sort algorithms
There are a number of sort algorithms:
For the examination you will need @ bubble sort
to understand how standard @ heap sort
algorithms (bubble sort, merge @ selection sort
sort, linear search, binary search) | ® Merge sort
work. ® insertion sort
® quick sort.
As with searching, the faster the sorting algorithm, the more
complex it tends to be.

Bubble sort
The simplest sorting algorithm is bubble sort.
The bubble sort works by repeating (iteration). The array is sorted
e Key points
from the first element to the last by comparing each pair of
4 The simplest sorting algorithm | elements in the array and switching their positions if necessary.
is bubble sort.
This process is repeated as many times as necessary, until all of
A
eee ounce standieree | the array is correctly sorted.
sort and bubble sort.
The worst-case scenario is that the array is in reverse order, and
the first element in the sorted array is the last element in the
starting array, for example we have 8, 7, 6, 5, 4, 3, 2, 1 and we want
a sorted list in ascending order.
Let’s explore the sort with these numbers in a random order:

6 |2 | 4 | 3 | Let’ take this array

5 itera yi. 3
Here we can see the array
1 ) 6 2 4 3 | afterthe first iteration.

| Sern «3
Similarly, after other
| 5 y. 4 6 3 | consecutive iterations, this
array will get sorted.
ligudesatuk eee > 2
Figure 25.5
25 Search and sort algorithms
co

Let’s look at a bubble sort in Python:


def bubbleSort(array): #declaring a function
#called bubble sort

for index in range(len(array)):


#run a for loop to
#iterate through the
#array list
for element in range((len(array)-1) - index):
if array[element] > array[element + 1]:
array[element] , array[element + 1] = \
array[element + 1], array[element]
#we will add a print
#statement so we can see
#the progress of the loop
print(‘\tResolving element[', element, ‘]to ', array)
array = [6, 4, 3, 5, 2, 1] #define our unsorted
#array
print('‘Bubble Sort..\nArray :', array) #output the unsorted
#array
bubbleSort(array)
print(‘Array :', array) #output sorted array
Output:
Bubble Sort...
Array : [6, 4, 3, 5, 2, 1]
Resolving element|[ 0 ] to [4, 6, 3, 5, 2, 1]
Resolving element|[ 1 ] to [4, 3, 6, 5, 2, 1]
Resolving element|[ 2 ] to [4, 3, 5, 6, 2, 1]
Resolving element[ 3 ] to [4, 3, 5, 2, 6, 1]
Resolving element[ 4 ] to [4, 3, 5, 2, 1, 6]
Resolving element[ 0 ] to [3, 4, 5, 2, 1, 6]
Resolving element 2 ] to [3, 4, 2, 5, 1, 6]
Resolving element[ 3 ] to [3, 4, 2, 1, 5, 6]
Resolving element[ 1 ] to [3, 2, 4, 1, 5, 6]
Resolving element[ 2 ] to [3, 2, 1, 4, 5, 6]
Resolving elementl[ 0 ] to [2, 3, 1, 4, 5, 6]
Resolving element[ 1 ] to [2, 1, 3, 4, 5, 6]
Resolving element[ 0 ] to [1, 2, 3, 4, 5, 6]
Array: (\t,2) 0,4 s5 6)

208
Sorting

Merge sort
Merge sort algorithms are simple and very efficient algorithms for
sorting a list.
In merge sort algorithms the idea is to take an array or list and
break the data into smaller pieces, solve the issue with each list
on each of the small pieces and then combine the pieces again.
Summarising, the main elements of a merge sort are:
@ Divide the data into halves
@ Conquer — solve each piece, by applying To eee
repeatedly (recursively) to them, and then
® Combine the pieces together into a global solution.
Let’s say we are given a sequence of n numbers which we will
first store in an array A[1 ... n]. We want the numbers in our array
sorted in increasing order. This is done by changing the order of
the elements within the array.
We simply:
@ Divide: Split ‘A’ down the middle into two subsequences, each
of size roughly n/2.
e@ Conquer: Sort each subsequence by calling mergeSort recursively
(repeating) on each.
@ Combine: Merge the two sorted subsequences back into a
single sorted list.
Input: [75241630 Output, |0 1234567
as mes
Split

: meraeer(3igs OO
ry
Figure 25.6 Merge sort
J,
25 Search and sort algorithms

Let’s look at a merge sort in Python:


def mergeSort(array): #declaring a function called merge sort
if len(array) > 1:
middle = int(len(array) / 2)
left = array[O:middle]; right = array[middle:]
print('\tSplit to ', left, right)
mergeSort(left); mergeSort(right)
#we now need to add an algorithm to divide the array elements into
#sub-sections then merge them together again
a=b=0
for element in range(len(array)):
L = left[a] if a < len(left) else None
R = right[b] if b < len(right) else None
if ((L and R) and (L < R)) or Ris None :
array[element] =L;a+=1
elif (Land R) and (L >= R)) or Lis None:
array[element] = R; b += 1
print(‘\t\tMerging ', left, right)
array = [6, 4, 3, 5, 2, 1] #define our unsorted array
print('Merge Sort..\nArray :', array)
mergeSort(array)
print(Array :', array)

Output:
Merge Sort...
Artay (6, 4.3). 5>2; 1]
SplintorlG, AAS by 2 2)
Split to [6] [4, 3]
Split to [4] [3]
Merging [4] [3]
Merging [6] [3, 4]
Split ter5] [2a]
Split to [2] [1]
Merging [2] [1]
Merging [5] [1, 2]
Merging [8, 4, 6] [1, 2, 5]
Arraycdil, 2a 3e4seG]

210
Comparison of sort methods
As you will now know, some algorithms like selection and bubble
sort work by moving elements to their final position, one item at a
time. They sort an array of size N, by putting 1 item in place, and
continue sorting an array of size N — 1. Other algorithms such
as insertion and quick-sort put items into a temporary position,
close(r) to their final position. The algorithm then re-scans the
data, moving items closer to the final position with each iteration.
One technique is to start with a ‘sorted list’ of one element, and
merge unsorted items into it, one at a time. To compare sort
systems we need to think about the complexity of the algorithm,
programming costs, memory space requirements and the number
of function calls required. We also need to compare the best- and
worst-case behaviours and to do this we must make assumptions
about the input data, caching, and the behaviour on already-
sorted or nearly-sorted data.

Comparing merge sort and bubble sort


If we take small data sets into account, bubble sort and merge sort
are almost equal in terms of the time it takes to sort the data.
Bubble sort is not efficient in terms of time but is quite good in
terms of memory as the data is sorted in situ whereas merge sort
is much more time efficient but generally uses more memory as
copies of the lists are created as they are split up.

Chapter review

In this chapter we have explored search and sort methods.


Remember, before tackling any computer science task or examination
question on this topic you must understand and be able to apply
standard algorithms including:
® bubble sort
® merge sort
® linear search
® binary search.
26 The computer systems architecture

Specification references
4.2.3 understand the concept of a stored program
J : You should: and the role of components of the CPU (control
4.2.1 understand the function of the hardware unit (CU), arithmetic/logic unit (ALU), registers,
: components of a computer system (CPU, main clock, address bus, data bus, control bus)
memory, secondary storage) and how they in the fetch-decode-execute cycle (the Von
work together Neumann model)

Hardware is the name given to a collection of physical ‘things’ that


A. ! Key point when put together in a certain way form a system. The hardware
. \ The physical devices that a is the machine. To be able to write programs you must understand
= =| computer is made of are called the function of hardware components of a computer.
& the computer hardware. The
The human body could be looked upon as a collection of hardware.
programs that run on a computer
First you have your brain — not the thought processes that go
are called the software.
on inside it, but the physical organ. Then you have the internal
hardware that keeps your body working such as your heart, lungs
and digestive system.
You also have ‘devices’ for taking on board different information,
such as your eyes for visual data, your hands that use the sense of
touch for tactile information, your ears for sound information and
not forgetting your tongue for your sense of taste.
All this information is processed and stored in your brain but only
whilst you are still alive.
Your body has ways of outputting information too. This involves
using speech as well as movements, expressions and gestures that
you can make with your face and body.
Hardware basically refers to parts or components of a system that
can be physically touched — although there are not many of us
that could truly say that they would like to touch someone’s brain
or internal organs.
Now, let’s take a look at the hardware components of a computer
and how they fit together to form a system.
cs 26 The computer systems architecture

7? The main components are:


Bs.

aaa
(T aa
(a a ae
Ae ,

Figure 26.1 Computer hardware components

Input/output

Inputs and user interfaces


Every interactive device has to have an input process and output. In
computer science we are interested in the interaction between the
computer and the human brain.

Figure 26.2 The brain and the computer

214
SSS
Input/output
SSS sss ss

Inputs to humans are via our receptors. We have a very wide


range of input receptors.
A Every interactive device has
to have an input, process and Computers need input and output devices too. A keyboard and a
output. mouse are input devices. Most screens on desktop computers are
output devices. Screens on mobile phones and tablet computers
A A user interface is the access
are both output and input devices as they have a touch screen.
point and the boundary
A camera is an input device and a printer is an output device. A
between the computer and
games controller can be just an input device, but if it can vibrate
the user.
it is an output device too.
A An operating system is the
software that controls all Of course any input device is only of use if it can link to the user
the hardware. It acts as an in some way. The user needs to show the computer what they
interface between the user want it to do.
and the hardware and also A user interface (UI) is the means by which a user can control a
between applications and the software application or hardware device. A good user interface
hardware. provides a ‘user-friendly’ experience, meaning it is easy to use.
The user interface is one of the most important parts of any
program because it determines how easily you can make the
? Question
program or device do what you want it to do.
1 What is an interface?
Input can be from physical devices such as the mouse, game
controller, keyboard etc. But it can also be from virtual devices
within the software.

1 Use this book and other Nearly all software programs now have a graphical user interface
sources such as the internet (GUI). This means the program uses graphical controls, accessed
to research the input-process- via a mouse, touchpad, touchscreen or keyboard.
output model. A typical GUI includes a menu bar, toolbar, windows, buttons
2 Identify the inputs, processes and other controls.
and outputs for:
Macs and Windows PC operating systems have very different
4 a mobile phone
user interfaces, but they share many of the same elements, such
A a modern television set. as a desktop, windows, icons, pop-ups etc.
Hardware devices also include a user interface. A typical TV
remote has a numeric keypad, volume and channel buttons, mute
and power buttons. The remote is a user interface.
While user interfaces can be designed for either hardware or
software, most are a combination of both.

e Questions
2 List six input devices.
3 Briefly describe the term operating system.

3 Use this book and other sources such as the internet to research the
function of the hardware components of a computer system.
26 The computer systems
ee Aa architecture
%
4

a
lia F Output devices
Humans can also output data and information. We can do this
a

with physical pressure and movement using our hands, feet, head
and eyes. We can output noise by clapping, speech and singing.
The most popular output devices for a computer used to be the
2 Question screen/monitor, printers and speakers. Today we have a plethora
4 List six output devices. of new output devices from 3D virtual glasses and heads-up
displays to electronic paper and voice synthesisers.
Of course these components must work together. They are all
connected via the motherboard.

§ Von Neumann architecture


For the examination you will need In the 1940s, a mathematician called John Von Neumann described
to understand the concept of a a basic architecture arrangement of a computer.
stored program and the role of Most of today’s computers follow the concept that he described.
components of the CPU (control
unit (CU), arithmetic/logic unit Von Neumann architecture relates to the relationship between
(ALU), registers, clock, address the hardware that makes up a computer system.
bus, data bus, control bus) in the
fetch-decode-execute cycle (the
A Von Neumann-based computer:
|Von Neumann model). @ Uses one memory for both instructions and data
® Cannot distinguish between data and instructions in a memory
location
® Executes programs by doing one instruction after the next in a
serial manner using a fetch—decode—execute cycle.

Advantages of Von Neumann architecture:


® The main advantage is that it simplifies the micro-controller
chip design because only one memory is accessed.
@ In micro-controllers, the most important thing is the contents
of RAM (random-access memory) and in the Von Neumann
system it can be used for both variable (data) storage and
program instruction storage.
@ Another advantage is thatit allows greater flexibilityin developing
software, particularly in the development of operating systems.
4 Use this book and other
sources such as the internet
Disadvantages of Von Neumann architecture:
to research the fetch-decode- e Whilst the advantages far outweigh the disadvantages, the
execute cycle (the Von problem is that there is only one bus (pathway) connecting the
Neumann model). memory and the processor so only one instruction or data item
can be fetched at a time. This means the processor may have to -
wait longer for the data/instruction to arrive. This is referred to
2 Question as the von Neumann bottleneck.
5 What is RAM? @ The above reason can also lead to a system crash as there may
be confusion between data and instructions.

216
Motherboard

e Questions | Motherboard
6 What is the purpose of the The hardware within a computer system includes the CPU (central
CPU? processing unit), which is situated on a printed circuit board called
7 Define a computer system. the motherboard.
There is also the hard disk drive, the RAM (random-access
memory), optical drive and other circuit boards such as the graphics
and sound cards.
The motherboard is the central
These are all regarded as hardware because if you removed the
printed circuit board (PCB) that
case you could physically touch the devices; however, you should
| holds all the crucial components
of the system. not do that unless you know what you are doing! The hardware
refers only to the electronic and electro-mechanical parts of the
computer, not the casing.

Figure 26.3 A summary of a computer system’s hardware

The motherboard links the CPU to the memory and other


hardware.
26 The computer systems architecture

ay

For the examination you will


need to understand the function
of the hardware components of
a computer system (CPU, main Figure 26.4 A motherboard
memory, secondary storage, etc.)
and how they work together with The motherboard is like your nerves — the essential connections
input and output devices. that send and receive signals throughout your body. The CPU
computes data and uses the motherboard to receive and send
signals to things like the hard drive (storage). The chip on the
2 Question motherboard is also responsible for holding all of the computer
settings, such as time and date. As with the CPU, motherboards
8 What is a motherboard?
also have processing speeds.

| Buses
! Key points A bus is a collection of parallel wires used for data transmission.
A bus is part of the circuit that connects one part of the
A A bus is part of the computer
motherboard to another. The more data the motherboard’s bus
architecture that transfers
can handle at any one time, the faster the system. The speed of
data and signals between
the bus is measured in megahertz (MHz), as is the speed of the
all the components of the
CPU (see below). Motherboards have buses for data, addresses
computer.
and control and each one transfers data from one computer
4 A bus is a Collection of component to another.
wires through which data
is transmitted from one i The CPU — central processing unit
component to another.
This internal device is often referred to as the ‘computer’s brain’
and it is the piece of hardware that is responsible for the compute
in computer. If you did not have the CPU, you would not have a
computer.

218
The CPU — central processing unit

Just like our brain, the CPU’s purpose is to process data and it
does this by performing functions such as searching and sorting
data, and calculating and decision making using the data.
What this means is that every task you carry out on a computer
whether it is designing a spreadsheet, writing an email, playing a
game or searching the internet, the central processing unit deals
with all of the data processing which makes those and many more
tasks possible.

! Key points The history of the CPU is very interesting. People define the CPU
in different ways; some people include the main memory in the
A Main memory is any form CPU and some just use CPU as an alternative word for processor.
of memory that is directly
accessible by the CPU, except The first CPU chip was invented in November 1971. It was basically
for caches and registers. a 4-bit processor designed for a calculator. Its instructions were 8
bits long. Program and data were separate.
A A CPU is a type of processor
that runs a computer system. Since those early days of CPUs with a single-core processor there
The name processor is a more have been many changes to the ‘brain’ in our computers.
generic term but is often used
Processor speed has doubled every few years. One big change
to mean the same thing. The
to the CPU was the addition of a second core called a dual-core
only problem with using the
processor. Each core has its own cache and controller. The benefit
term processor when referring
of this is that each core functions as efficiently as a single core.
to the CPU is that there will be
Because the two cores are linked together, they can perform
other processors in a system
operations twice as fast as a single core can.
but only one CPU.
Imagine making your breakfast which includes buttered toast and
a cup of tea. However fast you work, some tasks will need your
full attention. For example, you can’t butter the toast and pour
the tea at the same time. But if you had a second person to help
you and they made the toast and you made the tea, the process
would be much faster — providing you were each given the correct
instructions of course.

Figure 26.5 Quad-core processor

The technology has not stopped there. The next step was quad-
core processors. You may be thinking that each addition multiplies
the processing power of a single-processor machine, but it does
not always work like this. The software running on the machine
has to be designed to take full advantage of all the processors.
._
i 26 The computer systems architecture
\
Some ARM processors designed for mobile devices can even
interpret Java bytecode directly.
The CPU undertakes instructions it receives from programs
in what is called a cycle. Going back to the making breakfast
example, obviously if you worked faster you could produce it
quicker. Maybe the addition of a faster kettle would help too.
The CPU not only has a number of cores, it also has speed. The
speed of the CPU is measured in how many cycles it can perform
in a second. The name given to one cycle per second is a hertz. A
CPU that processes 1 million cycles per second is said to have a
speed of a megahertz, and a CPU that can handle 1 billion cycles
per second is said to have a clock speed of one gigahertz.
Therefore to have a very high CPU speed is a good thing, because
] Key point more instructions per second get executed. The problem is that
The clock chip is the electronic the faster it is, the hotter it gets, and the more power it needs.
device in a computer that
Most CPUs now are at least 2 GHz.
controls the timing of signals.
Most people would not notice differences between one processor
speed and the next speed higher unless they play the very latest
2 Question
computer games. If we go back to our breakfast analogy, however fast
you work and however many friends you have to help you, if the
9 What is a chip and how is it instructions are not clear and the things you need are not available,
attached to the computer? you will not be any faster. With CPUs programmers have a part to
play in maximising the speed of the cores they have.
A processor contains the following components:
@ Arithmetic and logic unit: performs arithmetic and logical
operations on data
® Control unit: fetches, decodes and executes instructions
® Internal buses: to connect the components
® Internal clock: derived directly or indirectly from the system
clock.

Arithmetic and logic unit


The arithmetic logic unit (called the ALU) is a major component of the
! Key point
central processing unit of a computer system.
The arithmetic logic unit (ALU)
performs all the arithmetic and
The ALU routinely performs the following:
logical operations within the @ Logical operations: including AND, OR, NOT
Ce) @ Logical comparisons
® Bit-shifting operations: shifting the positions of the bits by a
certain number of places to the right or left, which is considered
! Key point a multiplication operation
@ Arithmetic operations: bit addition and subtraction.
The control unit (CU) is inside
the CPU and is used to control Control unit
the flow of data within the A control unit (called the CU) handles all processor control signals.
system. It directs all input and output flow, fetches code for instructions

220
Factors affecting processor performance

from microprograms and directs other units and models by


providing control and timing signals.
CU functions are as follows:
® Controls sequential instruction execution
® Interprets instructions
® Guides data flow through different computer areas
® Regulates and controls processor timing
@ Sends and receives control signals from other computer devices
® Handles tasks, such as fetching, decoding, execution handling
and storing results.

Internal buses
2? Questions
An internal bus is a type of data bus that only operates inside the
10 What is an arithmetic logic computer or system. It carries data and operations like a standard
unit (ALU)? bus; however, it is only used for connecting and interacting with
11 What does the Arithmetic internal computer components.
Logic Unit (ALU) do?
The CPU bus is internal to the CPU and is used to transport data
to and from the ALU.

Internal clock
A clock is a signal used to synchronise things inside the computer.
The beginning of each clock cycle is when the clock signal goes
from ‘0’ to ‘1’; marked with an arrow in the diagram. The clock
signal is measured in a unit called hertz (Hz), which is the number
of clock cycles per second. A clock of 100 MHz means that in one
second there are 100 million clock cycles.

Figure 26.6 The clock cycle

In computing, all timings are measured in terms of clock cycles.

§ Factors affecting processor performance


There are a number of things in the computer architecture that
can affect the processor performance. The most important factors
are:

Number of cores
A CPU can contain one or more processing units. Each unit is
called a core. Each core contains an ALU, control unit and registers.
Today computers often have two (dual-core), four (quad-core) or
more cores. CPUs with multiple cores have more power and can
therefore run a number of programs at the same time.
26 The computer systems architecture

You may think that a dual-core processor will have double the
speed of a single core but it does not work like that because each
CPU has to communicate with the other and this uses up some of
the extra speed. Also some tasks cannot be split up effectively so
do not benefit from more cores.

Clock speed
The clock speed of a processor is stated in megahertz (MHz)
or gigahertz (GHz). Basically the faster the clock, the more
instructions the processor can complete per second, but there
are limits to how fast a CPU can run. If the clock tells the CPU
to execute instructions too quickly, the processing will not be
completed before the next instruction is carried out and it can
also overheat.

On-board cache
J Key point The on-board cache is a type of high-performance RAM built
For the examination you will need directly into the processor. Cache has both size and type. It
to understand the function of enables the CPU to access repeatedly used data directly from its
different types of main memory own on-board memory. We will explore this in more detail in
(RAM, ROM, cache). Chapter 27 when we look at memory.

Instruction sets
The user has no control over the instruction set. It is built in to
the CPU and cannot be changed or updated. But together with
processor architecture, the built-in instruction set does affect the
CPU’s performance. Some instruction sets are more efficient than
others, enabling the processor to do more processing at the same
speed as others. Processor benchmark tests can be used to measure
the speed at which the processor can carry out instructions.

Chapter review

In this chapter we have explored computer system architecture.


We also explored the Von Neumann model.
Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain and apply the function of the
hardware components of a computer system (CPU, main memory,
secondary storage) and how they work together with input and output
devices
® Understand and be able to explain and apply the concept of a stored
program and the role of components of the CPU (control unit (CU),
arithmetic/logic unit (ALU), registers, clock, address bus, data bus,
control bus) in the fetch-decode-execute cycle (the Von Neumann
model) (More on this topic in Chapter 30).

222
2/ Memory

Es Specification reference
You should:
J Key points 4.2.2 understand the function of different types of main memory (RAM,
| 4 Just like humans, computers ROM, cache)
rely a lot on memory. They
need to process and store
A computer has to store both programs and data whilst they are
data, just like we do. However,
being used and when the computer is turned off. It does this
computers store data in digital
using a storage device which stores data and programs either
format in a reserved location
temporarily or permanently. The main storage or memory (often
and of a set location size.
~ called primary storage), is the area in a computer in which data is
me 4 Computers have two types
stored for quick access by the computer’s processor.
; of storage systems, primary
storage and secondary There are two fundamentally different types of storage:
storage. ® Primary store / main memory (often just called memory)
A Primary storage or main
® Secondary store (also known as backing store).
memory Is directly connected The main memory is where the operating system resides. The
to the motherboard and is main computer memory is divided into two types, Read-Only
the place where programs Memory (ROM) and Random-Access- Memory (RAM).
and data are kept while being
used.
Data within a computer’s main memory is actually stored in
individually addressable memory cells. Structures such as arrays,
lists and trees are simulated to make the data more accessible to
e Questions the data’s users. The computer is capable of determining what
an individual memory cell contains by a process referred to as
1 What is RAM? reading. The contents of a memory cell can also be changed (a
2 What is an array? process referred to as writing) but when something is written to a
memory cell its former contents are lost.
Main memory has the following characteristics:
1 Use this book and other
@ Its contents can be accessed directly by the CPU.
sources such as the internet
@ It has very fast access times.
to research how data is stored
@ It has a relatively small capacity.
on physical devices (magnetic, The contents of RAM are lost when the computer is turned off
optical, solid state). (volatile) but the contents of ROM are retained (non-volatile).
¥ 27 Memory
ee

Secondary store is always non-volatile. Secondary store can be


! Key points
external devices like CDs, magnetic disks etc. Secondary storage
4 On modern computers, cannot be directly accessed by the CPU and is also called external
memory is usually composed memory storage. We will explore these in more detail in Ehennext
of semi-conductors known chapter.
as RAM (random-access
memory).
The contents in secondary store cannot be accessed directly by
the CPU. The data must be transferred into main memory to be
A There are two technologies for
used through a process of loading and saving. This makes access
RAM chips: dynamic RAM and slower, but it does have much higher capacity.
static RAM.
! Key point
Firmware is programming that’s written to the read-only memory (ROM)
of a computing device. It is added at the time of manufacturing and is
used to run user programs on the device.

! Key points § ROM


A ROM (Read-Only Memory) ROM is memory that cannot be changed by a program or user.
retains data when the ROM retains its memory even after the computer is turned off.
computer is turned off. ROM is used to store the instructions for the computer to boot up
4 ROM (Read-Only Memory) is when it is turned on.
another variety of memory. It ROM is also used to store software that needs to be available
is similar to RAM except that it when the computer is turned on (such as the instructions for
has its contents permanently booting the computer) or software that will ‘never’ change such
embedded and thus will not as the BIOS.
be erased when the power is
turned off. With Von Neumann architecture the program(s) being run are
kept in the computer memory as well as the data that is currently
being processed.

! Key points RAM


A RAM (Random-Access RAM is a fast temporary type of memory in which programs,
Memory) is volatile, applications and data are held.
meaning that when power
RAM holds things while the computer is on and they are running:
is interrupted, even for an
instant, the contents are
® the operating system
erased.
® applications software.
A RAM is not suitable for long- If a computer loses power, all data stored in its RAM is lost.
term storage of software and
data.
A The contents of RAM can
be manipulated while the
computer is running.

224
Cache memory
SSS sss

§iCache memory
Cache memory is a special type of RAM and is part of the primary
store. The cache is almost always located on the same microchip
as the CPU. This means it can be accessed much quicker than
ordinary RAM. Processors often have separate caches for data
and instructions. By executing a program from cache memory, a
program also runs more quickly.
The basic purpose of cache memory is to store program instructions
that are frequently re-referenced by software during operation.
Fast access to these instructions increases the overall speed of the
software program. But cache memory is much more expensive
than ordinary RAM, so computers don’t have much of it.

The CPU has to decide which parts of a program to copy into


the cache to gain maximum performance improvements. A
microprocessor can access cache faster than it can access regular
RAM.
Cache memory is often integrated directly with the CPU chip or
placed on a separate chip that has a separate bus connection with
the CPU. The cache only stores a copy of the data also stored in
RAM but as the microprocessor processes data, it looks first in the
cache memory. If the data was used previously, it often finds the
instructions there and does not have to do a more time-consuming
reading of data from larger memory or other data storage devices.
Traditionally cache is categorised in ‘levels’ that describe its
closeness and the accessibility of the cache to the microprocessor:
@ Level 1 (L1) cache is extremely fast but relatively small, and is
usually embedded in the processor chip (CPU) which is why
it is called level 1. It is closest to the processor. Level 1 cache
usually ranges in size from 8 KB to 64 KB.
@ Level 2 (L2) cache is often larger than L1 but one stage further
from the processor. It may be located on the CPU or on a
separate chip.
® Level 3 (L3) cache is typically specialised memory that works to
improve the performance of L1 and L2. It can be significantly
slower than L1 or L2, but is usually double the speed of RAM.
The size of the cache refers to the size of the data store. A typical
level 2 cache is either 256 KB or 512 KB, but can be as small as 64
KB, or as high as 2 MB.
27 Memory
ee TT
ee ee

The more cache the system contains the better, because it is faster
! Key points
and fewer memory locations within it will be forced to share the
A Volatile memory is a type of same cache line. Most programs use very few resources once they
storage whose contents are have been opened and operated for a time. This is because
erased when the system’s frequently re-referenced instructions will be cached. The more
power is turned off or cache the system has, the more likelihood that a particular
interrupted. memory fetch will find the data it requires in the cache; this leads
4 Non-volatile memory is to fewer main memory accesses and better performance.
typically used for the task Computers with slower processors but larger caches tend to be
of secondary storage, or faster than computers with faster processors but less cache.
long-term persistent storage.
Examples are ROM and hard
drives.
Chapter review daxJ
A The computer stores all In this chapter we have explored computer memory.
program data in memory
Remember, before tackling any computer science task or examination
locations. It knows these
question on this topic you must:
locations by their addresses.
® Understand and be able to explain and apply the function of different
types of main memory (RAM, ROM, cache).
28 Secondary storage

Ds Specification references
You should:
4.2.4 understand how data is stored on physical devices (magnetic,
optical, solid state)
4.2.5 understand the concept of storing data in the ‘cloud’ and other
contemporary secondary storage

The majority of secondary storage devices are used for long-term


storage. They are also used to:
A There are three types of ® backup data
storage mediums: optical, ® add more storage space for files/pictures/videos/etc.
magnetic and solid state. ® transfer files between computers
A Secondary storage is the ® easily transport files
place where software and data ® share files over a network.
are kept when not in use.
Different types of non-volatile secondary storage include:
® magnetic storage
® optical storage
— compact disks (CD)
— digital versatile disks (DVD)
— Blu-ray disks
® solid-state disks
— USB flash drives (pen drive)
— flash memory cards.

Magnetised Magnetic storage devices (MSD)


data on disk
A magnetic hard disk drive (HDD) uses moving
read/write heads that contain electromagnets.
Tracks These create a magnetic charge on the disk’s
surface which contains iron particles that can be
Disk given a magnetic charge in one of two directions.
rotation Each magnetic particle’s direction represents
0 (off) or 1 (on). As you will remember these
represent a bit of data that the CPU can recognise.

Figure 28.1 A hard disk drive


i 28 Secondary storage

There are two physical limits to magnetic hard drives: data density
! Key point
and disk speed.
In magnetic storage, magnetic @ The data density defines how much information can be written
material is given a polarity. on any given area of the disk. Higher data density would give
That polarity is then read and higher capacity drives, but this is limited by what is called the
depending on the direction is super-para-magnetic effect.
output as a binary 1 ora 0. @ The spindle speed is how fast the disk spins. Spindle speed is
another way of increasing the data store size, but this is limited
by the speed of the motor used.

Advantages of magnetic storage:


@ Very large data storage capacity.
@ Stores and retrieves data much faster than an optical disk.
® Data is not lost when you switch off the computer as it is with RAM.
@ Cheap per MB compared with other storage media.
® Can easily be replaced and upgraded.

Disadvantages of magnetic storage:


@ Hard disks have moving parts which can fail.
® Crashes can damage the surface of the disk, leading to loss of data.
e Easily damaged if dropped.
@ Uses a large amount of power compared with other media.
@ Can be noisy.

] Key points § Optical storage devices (OSD)


A |n optical storage, pits and An optical drive uses reflected light to read data. The optical disk’s
grooves in a flat surface surface is covered with tiny dents (pits) and flat spots (lands), which
provide a reflection for a laser; cause light to be reflected off them differently. When an optical drive
a light sensor then assigns a shines light into a pit, the light is not reflected back. This represents
binary 1 or a O depending on a bit value of 0 (off). When the light shines on a flat surface (land)
the light intensity. it reflects light back to the sensor, representing a bit value of 1 (on).
4 CD, DVD and Blu-ray disks use
optical storage.
Artwork
4 CD, DVD and Blu-ray disks
are all the same physical size
and use the same spiral track A layer
layout for placing data on the of lacquer
platter.
Reflective ¢
layer

Polycarbonate |
layer

Laser beam -
and sensor
Figure 28.2 The layers of an optical disk

228
Solid-state disks (SSD)

] Key points The polycarbonate disk layer contains tiny dents (pits) and flat
spots (lands), which cause light to be reflected back off them.
4 CD-R (CD-Recordable), DVD-R
(DVD-Recordable) and BR-R The reflective layer is there to reflect the laser
(Blueray-Recordable) discs A layer of lacquer covers the reflective layer to protect it from the
can, with minor exceptions, artwork.
be written to only once. Data,
once recorded, cannot be Artwork can be either screen printed or written onto this surface.
overwritten or deleted. The laser beam reads and writes to the disk and is reflected back
4 Disks with the RW(Rewritable) to a sensor, which converts it into electronic data.
suffix can be written to
repeatedly, by deleting or Advantages of optical storage devices:
overwriting old data to make @ Easy to store and carry.
room for new data. ® Optical disks are read in a number of devices such as audio and
A The introduction of DVD led TV systems.
to a five to ten times increase @ Very easy to use.
in storage capacity compared @ Long lasting if looked after properly.
to CD. Blu-ray discs increased
Capacity by five to ten times
Disadvantages of optical storage devices:
over DVD. This was achieved @ Data on write-once disks (CD-R, DVD-R and BR-R) are
mainly by using a blue laser permanent and cannot be changed.
rather than a red laser and ® Optical disks require special drives to read/write.
improved lenses. This allowed ® Optical storage is expensive per GB/TB in comparison with
for a much smaller-focus other methods.
laser beam which enabled the @ There are no standards for longevity tests.
recording of much smaller and ® Can easily be scratched and damaged by heat and light.
higher density pits on the disc. ® Easily broken.

| Solid-state disks (SSD)


! Key points
Solid-state disks contain no moving parts and are found in three
A Solid-state disks do not
common formats: hard disk replacements, memory cards and USB
require physical motion so
flash drives. They record data using special transistors that retain
they have shorter response
their state even when there is no power to them. Because there is
times. They also do not suffer
no moving actuator arm like on a hard disk drive (HDD), they are
from physical wear.
faster in reading and, in some cases, writing data. They are also
A Solid-state storage uses more rugged so are not as easily damaged when dropped.
electrical signals to change
the state of a transistor. Advantages of solid-state disks:
This transistor can either be @ Read speeds are faster than for normal hard drives.
changed to binary 1 or O. @ Solid-state hard drives have non-volatile memory, which means
that data is stable.
@ They are lightweight.
@ They are very durable.
@ They are free from mechanical problems.
@ They require less power than magnetic drives.
e They are silent in use.
4
i 28 Secondary storage

! Key point
Disadvantages of solid-state disks:
@ They have limited storage capacity when compared with normal
“3

ae é
For the examination you will need magnetic hard Aneus

: ee ney gata: aren e Random write speeds of solid-state drives can be four times
“ Beeical mewers Unaghens: slower than for normal magnetic hard drives.
peel sere) @ The cost per MB stored is higher than for magnetic drives.

§ Memory sticks/pen drives


USB flash drives use the same technology as solid-state drives.
! Key point
A USB memory stick uses solid-
They are a more compact shape and operate faster than an external
state storage.
magnetic drive due to their lack of moving parts. Flash drives are
widely used to transport files and backup data from computer to
computer.

Advantages of memory sticks/pen drives:


@ The biggest advantage of flash memory sticks is that they are
small and easily portable.
® Memory sticks have non-volatile memory, which means that
data is stable.
@ They do not need internal power.
@ They are lightweight.
@ They are very durable.
e Adding or deleting files in flash memory is quick and tidy.
@ They are free from mechanical problems.
@ They require less power than magnetic drives.
lf the file is stored on a CD, its e They are silent in use.
physical organisation is most : - ; ‘
Beit janiiceeciratne Disadvantages of memory sticks/pen drives:
file is stored on magnetic disk, 8 The cost per MB stored is higher than for magnetic drives.
then it is most likely scattered @ Information can only be erased and written about 100,000
over various sectors on the disk. times.
Data is found using a pointer @ They can break easily.
system or some form of a list in e They can be lost, misplaced or broken/damaged.
which the sectors where the file @ They can be affected by electronic corruption and this can make
is stored are recorded. the entire data totally unreadable.
Flash memory cards

§ Flash memory cards


A memory card or flash memory card is also a solid-state drive
using flash memory. They are often used with digital cameras,
handheld computers, mobile telephones, music players, video
game consoles and other types of small electronics devices.

An SD card uses solid state Advantages of flash memory cards:


storage. @ They are small and easily portable.
@ They have non-volatile memory, which means that data is
stable.
e@ They do not need internal power.
® They are lightweight.
@ They are very durable.
e® Adding or deleting files in flash memory is quick and tidy.
@ They are free from mechanical problems.
® They require less power than for magnetic drives.
@ They are silent in use.

Disadvantages of flash memory cards:


® There are many different formats and sizes, making them often
2 Question only suited to one device and hard to read across devices.
1 Describe the benefits and ® The cost per MB stored is higher than for magnetic drives.
drawbacks of a using a @ Information can only be erased and written about 100,000
traditional magnetic hard drive times.
in a laptop computer when @ They can break easily.
compared with a solid state @ They can be lost, misplaced or broken/damaged.
hard drive. ® They can be affected by electronic corruption and this can make
the entire data totally unreadable.

Chapter review

In this chapter we have explored secondary storage.


Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain and apply how data is stored on
physical devices (magnetic, optical, solid state)
® Understand and be able to explain and apply the concept of storing
data in the ‘cloud’ and other contemporary secondary storage (More
on this topic in Chapter 29).
29 Cloud computing

Es Specification references

You should:
4.2.5 understand the concept of storing data in the ‘cloud’ and other
contemporary secondary storage
4.4.3 understand how software can be used to simulate and model
aspects of the real world

§jCloud storage
‘\ « Cloud storage uses magnetic Cloud storage means ‘the storage of data online in the cloud’. In
and increasingly solid state cloud storage, data is stored and is accessible from multiple
storage at a remote location. distributed and connected resources that comprise a cloud via the
S| 4 For the examination you internet.
will need to understand the
In cloud storage, data is stored on servers and can be accessed
concept of storing data in the
across the internet. The data is stored across multiple servers that
‘cloud’ and other contemporary
can be in many locations. The physical environment where the
secondary storage.
servers are is usually owned and managed by a hosting company.
Cloud storage systems are increasing in popularity as a cost-
effective back up for businesses and individuals. Some modern
software even backs up the data to remote cloud-based servers
1 Use this book and other automatically, sometimes every few seconds.
sources such as the internet to
research the storing of data in Examples of cloud storage systems include Dropbox, Google Docs,
the ‘cloud’. Xdrive and Microsoft Cloud. Amazon Web Services even allow
the user to map a drive in the cloud and format it.
Cloud computing now lets the user access all their applications
2 Question and documents from anywhere in the world.
1 List five criteria that you would Cloud computing has led to a major change in how we backup
need to consider in developing data and store information.
a policy for backing up your
data.
Given the many advantages, Apple, Google and Microsoft have
all developed cloud-based data services.

232
Cloud storage

ee
=

Figure 29.1 Cloud computing

A growing number of people want to access the latest version of


their documents on a range of devices, but the benefit of shared
documents in businesses where a number of people work together
on a single project is driving this cloud-based technology forward
For the examination you will need even faster.
to understand security issues
But as with all computing there are advantages and disadvantages
associated with the ‘cloud’ and
of cloud storage.
other contemporary storage.
Advantages of cloud storage:
@ Unlimited storage capacity. Cloud computing offers limitless
storage.
@ Automatic backup. On a computer, a hard disk crash can destroy
all your valuable data if it is stored on the device, but if it is in
the cloud a computer crash shouldn’t affect any of your data.
@ Universal access. You don’t carry your files and documents with
you, they stay in the cloud, and you access them whenever you
have a computer or mobile and an internet connection. All your
documents are instantly available wherever you are.
@ Device independence. The user is not limited to working on
a document stored on a single computer or network. You can
change computer and even change to your mobile device, and
the documents follow you through the cloud.
29 Cloud computing
OO
p
i Disadvantages of cloud storage:
@ Cloud storage requires a reliable internet connection. Cloud
wana

computing is impossible if you can’t connect to the internet.


@ Cloud storage will not work as well with low-speed connections.
Web-based apps and large documents and images require a lot
of bandwidth.
@ Loss of control. The user loses control over what happens to the
data as this is managed by the cloud service provider.
Given the many advantages, Apple, Google and Microsoft have
all developed cloud-based data.

§ Cloud computing systems


? Question Cloud computing offers more than just backing up and shared
documents. Cloud computing now lets the user access all their
2 List five criteria that you would
applications and documents from anywhere in the world.
need to consider in developing
a policy for backing up your Cloud computing has led to a major change in how we run
data. applications. Instead of installing applications (often referred to
as apps) on individual computers, everything can be hosted in
the ‘cloud’.

Advantages of cloud computing:


@ Lower computer/device costs. The user does not need an
Any software, hardware or
expensive computer to run cloud computing’s web-based
storage running on servers
applications. Applications run in the cloud, so they do not need
connected to the internet can
the processing power or hard disk space that you would need
be called cloud computing. In
for locally installed software. Your computing device requires a
cloud computing you use the
smaller hard disk and less memory; this has helped to enable
software on machines ‘in the
the growth in mobile devices with small solid-state hardware.
cloud’ instead of using your own
® Improved performance. With fewer programs using the computer’s
personal computer to carry out
memory, the user gets better performance from their computing
or store your work.
system.
@ Reduced software costs. Instead of purchasing expensive software
applications, you can get most of what you need online at a much
lower cost or even for free.
e Instant software updates. Cloud-based software is always up to
date.
e Improved compatibility. You don’t have to worry about the
documents you create on your machine being compatible with
your other devices; there are no format incompatibilities when
everyone is sharing docs and apps in the cloud.
e Unlimited storage capacity. Cloud computing offers limitless
storage.
@ Automatic backup. On a computer a hard disk crash can destroy
all your valuable data if it is stored on the device, but if it is in
the cloud a computer crash shouldn’t affect any of your data.

234
Cloud computing systems
i

@ Universal access. You don’t carry your files and documents with
you, they stay in the cloud, and you access them whenever you
have a computer or mobile and an internet connection. All your
documents are instantly available wherever you are.
® Device independence. The user is not limited to working on
a document stored on a single computer or network. You can
change computer and even change to your mobile device, and
the documents follow you through the cloud.
2 Questions Disadvantages of cloud computing:
®@ Limited features. Many web-based applications do not have as
3 Describe the benefits and
many features as conventional computer programs but this is
drawbacks to a rural school in
changing and some new apps have enhanced features.
moving to a cloud computing
@ Given the many advantages, Apple, Google and Microsoft have
model.
all developed cloud-based software services.
4 What are the implications of
cloud computing to computer
programmers? Chapter review

In this chapter we have explored cloud computing.


Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain the concept of storing data in the
‘cloud’ and other contemporary secondary storage (More on this topic
in Chapter 28)
® Understand the emerging use of cloud computing.
30 Fetch-decode-—execute cycle

Cs Specification reference

You should:
4.2.3. understand the concept of a stored program and the role of
components of the CPU (control unit (CU), arithmetic/logic unit
(ALU), registers, clock, address bus, data bus, control bus) in the
fetch-decode-execute cycle (the Von Neumann model)

The fetch-—decode-execute cycle is sometimes called the


instruction cycle. It is the basic operation cycle of a computer as it
is the process by which a computer retrieves a program instruction
from its memory, then determines what actions the instruction
requires.
First the processor fetches the instruction from the main memory.
Key point
The instruction is sent to the memory buffer register via what is
se| The register is a special fast called a data bus. Finally, the instruction is copied to the current
access part of the CPU that instruction register for decoding and execution.
| stores data in use by the CPU.
Next the decoder interprets the instruction.

Input devices Weliamasaelaae |Output devices


! Key points
An instruction is decoded by the
CPU into two parts - the operator
and the operand.
A The operator is the part of the
instruction that tells the CPU Figure 30.1 The CPU (computer’s brain) is connected to the other hardware
what to do. components by a bus
A The operand is the part of the
Let’s look at each of these steps in more detail.
instruction that tells the CPU
what to apply the operation to. The process starts with the address in memory of the first
instruction being stored in the program counter.

236
Fetch the next instruction

. Fetch the next instruction


The program counter is simply a At this point the instruction register contains the instruction to
register in the CPU that keeps the be executed. Before going on to the next step in the cycle, the
address of the next instruction. program counter must be updated to hold the address of the next
instruction to be executed when the current instruction has been
completed.
Because the instructions are stored contiguously in memory,
adding 1 to the program counter puts the address of the next
instruction into the program counter. So the control unit
increments the program counter by 1.
Accessing memory takes one cycle. Most computers can access
memory at least at 133,000,000 cycles per second, so one access
takes 7.5 nanoseconds or 7.5 billionths of a second.

a Decode the instruction


In order to execute the instruction in the instruction register, the
control unit has to determine what the instruction is. It might be
an instruction to perform some operation on a data item.
At this phase, the instruction is decoded. The logic of the circuitry
in the CPU determines exactly which operation is to be executed.
The instructions themselves are built into the circuits. This is why
a computer can only execute instructions that are expressed in
machine code.

© Guesiog § Get data if needed


1 What does the arithmetic logic
It may be that the instruction to be executed requires additional
unit (ALU) do? memory accesses in order to complete its task.

Control unit ALU

(2) Decode instruction (3) Get data

Fetch cycle Registers Execution cycle

(1) Fetch instruction (4) Execute the instruction

Figure 30.2 The fetch-execute cycle


30 Fetch-decode-execute cycle

flExecute the instruction


Once an instruction has been decoded and any operands (data)
! Key point execute the instruction.
fetched, the control unit is ready to
The arithmetic logic unit (ALU) Execution involves sending signals to the arithmetic logic unit to
performs all the arithmetic and carry out the processing. In the case of adding a number to a
logical operations within the CPU. register, the operand is sent to the ALU and added to the contents
of the register.
Prior to the execute cycle much data is transferred but nothing
gets done. After each fetch cycle the next clock pulse initiates an
execution phase. The instruction is ‘decoded’ and then acted upon.
What happens in the execute stage is automatic. Every instruction
consists of a number of parts or fields. Exactly how many and
what type depends on the processor’s architecture, but there are
usually at least two. This consists of a simple binary value that
specifies what the instruction must do. Each instruction has a
unique code that causes the computer to carry out the required
operation.
When the execution is complete, the cycle begins again.

Chapter review

In this chapter we have explored the fetch-decode-execute cycle.


Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand and be able to explain the concept of a stored program
and the role of components of the CPU (control unit (CU), arithmetic/
logic unit (ALU), registers, clock, address bus, data bus, control bus)
(More on this topic in Chapter 26)
® Understand and be abie to explain the fetch-decode-execute cycle
(More on this topic in Chapter 26)
® Understand and be able to explain the Von Neumann model (More on
this topic in Chapter 26).

238
You should:
4.4.1 know what an operating system is and how it 4.4.3. understand how software can be used to |
manages files, processes, hardware and the simulate and model aspects of the real world — |
user interface 6.1.3 understand the legal impact of using
4.4.2 understand the purpose and functions of utility technology (intellectual property, patents,
software (managing, repairing and converting licensing, open source and proprietary
files; compression; defragmentation; backing software, cyber-security) on society
up; anti-virus, anti-spyware)

So far we have looked at code and the hardware that makes


up a computing device. We have even looked at how code is
written, but computing devices have both hardware and software
infrastructures in order to function. In this chapter we will explore
some of the software infrastructures including system software
and application software.

Application Software
Word processors Datopaces
Spreadsheets
Computer Internet
Games Browsers
Hardware
CPU, disks, mouse,
printer, etc.

1 Use this book and other


sources such as the internet Figure 31.1 Link between hardware and software
to research the tasks the
operating system performs System software
and how it manages files,
The term system software refers to the operating system and all
processes, hardware and the
utility programs that manage the computer resources at a low
user interface.
level. They are types of computer programs that are designed to
run a computer’s hardware and application programs. If we think
of the computer system as a layered model, the system software is
the interface between the hardware and user applications.
4F 31 Software
hed
ee eA

Understanding the operating system 7


] Key point
At the very heart of the computer is the operating system (often
The operating system is a
called the OS). The operating system is one of the most important
program that allows application
‘parts’ of any computer. Almost every type of computer, including
software to communicate with
mobile telephones, note pads, video game systems and e-book
the hardware.
readers, needs an operating system in order to operate properly.
The operating system as we can see is actually not one, but a
collection of programs that control the system. The operating
system is responsible for the management and control of all the
computer’s resources. This includes memory, processors, hard
drives, monitoring I/O devices etc. It not only handles the system
resources, it also handles the application software that users run,
security and file managment. It also provides a link between the
hardware and software.
Recognises and
installs peripheral
Handles inputs devices Manages files
and outputs | and folders

- 7 Shares out
ee” system memory
Loads and runs
other software
applications Handles system
==> orrors and
alerts user
a | ~~
Moves data to Manages system
? Questions and from the Allows software to security
hard disk communicate with
i State three examples of hardware
operating systems which are Figure 31.2 Tasks of the operating system
used in different computer
devices. Some of the most popular operating systems are:
@ Windows
2 What is operating system
@ Windows CE
software?
@ Macintosh OS X (Mac OS X)
@ iOS GPhoneOS)
® Linux
@ Android Phone
2 Use this book and other
® Blackberry (RIM OS)
sources such as the internet
® Solaris (SunOS)
to research the purpose and
@ AIX
functions of utility software.
@ IOS (Cisco)
®@ XOS (Extreme Networks)
® IronWare OS (Foundry).
System software

e Key point Handling and managing all of the system resources


For the examination you will
The operating system handles the computer’s memory and the
sharing of the central processing unit (CPU). Its job is to make sure
need to know what an operating
that each application gets the correct and adequate resources. It is
system is and how it manages
also responsible for maximising the whole system. The operating
files, processes, hardware and
system is the most important program that runs on a computer.
the user interface.
Every general-purpose computer must have an operating system.
Operating systems send output to the display screen, keep track
of files and directories on the disk and control all the peripheral
devices like disk drives and printers.
It is the operating system that brings life to the computer and it is the
first program loaded into memory when the computer is turned on.
The key functions of the operating system include:
® memory management
® [/O management
® file management
® security
® error handling
® utility software management
® program management
® interaction with the user.

Memory management
The operating system manages the CPU, allocates memory,
accesses disk drives, controls printers and much more. All the
input/output functions of the computer are controlled by the
operating system. The operating system is totally responsible for
memory management. It keeps tracks of memory use and decides
which process will get memory, when and how much.
The operating system memory management functions include:
® controlling the location of memory, dealing with the transfer
of programs in and out of memory when the process no longer
needs it or when the process has been ended
® carrying out a process called scheduling where it manages the
CPU, organising the use of memory between programs
® organising processing time between programs and users
® keeping track of processors and the status of any process
running.

I/O management
The operating system manages all the input and output devices
including controlling the backing store and all peripherals such as
scanners and printers. It does this using what are called drivers.
31 Software

Use
Software driver Specially written program which translates the commands from
the operating system into commands that the hardware will
understand.

The operating system does the following activities for device


management:
@ translates instructions sent by application software into a
format that I/O devices can understand, and vice-versa
@ decides which process gets the device, when and for how much
time.

File management
The operating system handles the organisation and tracking of
files and directories (folders). It also saves or retrieves these from
a computer disk.
The operating system does the following file management activities:
@ allows the user to perform tasks including the creation of files
and directories
® allows the user to save files to a backing store
@ allows the user to rename, copy, move and delete files
@ keeps track of where files are located on the hard drive through
either a file allocation table (FAT) or the New Technology File
System (NTFS).

Security
Computer systems often have multiple users. These users are often
running multiple processes and these processes must be secure. The
operating system maintains security and access rights of users.
The operating system does the following security activities:
® controls the access of programs, processes and users to the
computer resources
@ ensures that all access to system resources is controlled
® ensures that external I/O devices are protected from invalid
access attempts
® provides authentication features for each user by means of a
password.

Error handling
In any computer, the operating system deals with errors and user
instructions. Errors can occur anytime and anywhere including
errors in the CPU, in I/O devices or in the memory hardware.
The operating system does the following error management
activities:
® monitors the system for any errors that occur
®@ takes appropriate actions to ensure correct operations
® closes the system if errors are terminal.

242
System software |=
: si
Be Pe

Utility software management


Other types of system software include utility software such as:

For the examination you will Virus scanner (utility) To protect the system from trojans and viruses
need to understand the purpose Disk defragmenter To speed up the hard disk. Defragmentation picks up
and functions of utility software (utility) all of the pieces of data that are spread across the
(managing, repairing and drive and puts them back together again.
converting files; compression; System monitor (utility) To look at the current system resources
defragmentation; backing up;
anti-virus, anti-spyware). Utility software is a kind of system software designed to help
analyse, configure, optimise and maintain the computer.

Application management
It is the operating system that provides information about all user
programs running on your computer. It does this by handling
everything from user programs to system programs including
everything from the printer spooler to the servers.
The application packages, such as word processors and
spreadsheets, are what most users buy a computer for. Without an
e Questions operating system they could not execute (run). The OS also provides
these applications with tools and services such as printing and
3 What is the purpose of a
fetching data from a hard disk. Without an operating system, you
device driver?
cannot use your word processing software spreadsheet software
4 What is utility software? or any other applications.
5 What is the purpose of disk
With graphical operating systems, the application also manages
organisation?
the menu facilities and windows supported and provided with the
6 What is a defrag? operating system.
The operating system does the following application management
activities:
® loading a program into memory
® executing the program
® the complete execution of code, data manipulation, register
management etc.
® provides a mechanism for processing all internal communication.

Interaction with the user


For a computer to be of any use, users must have some means of
interacting with it. The operating system provides a means of
doing this which is easy, consistent, flexible and structured.
Without an operating system, a user could not communicate with
a computer. When a user gives the computer a command, the
e Question operating system relays the instructions to the CPU. As you are
7 Briefly describe the term aware, a user cannot speak directly to the CPU because it only
graphical user interface. understands machine language. Originally all instructions were
input using what is called a command line interface. Now most
devices use a graphical user interface.
31 Software

Command line interface 2 Graphical user interface


| Advantages j Disadvantages | Advantages Disadvantages

Relatively quick for users Smaller range of commands


User directly communicates Users sometimes have to
communicate using complex to enter commands, just can be used.
with the computer and wider
range of commands can be commands and need to type click on the icons.
used than using a graphical a number of lines for quite
interface. simple tasks.

Back screen with text requires Higher chance of user making Lower chance of users Requires more electrical
less electrical energy. errors when typing in commands. making errors. energy.

Human-computer interaction
Human-Computer Interaction (HCI) is a branch of computer
science dedicated to understanding the relationship between
people and computers.
You may think that communication with the user is the least
important function of the operating system, but system users
often judge the complete system by its user interface rather than
its functionality. A poorly designed interface cannot only cause
a user to make errors, but poor user interface designs have, in
recent years, been the reason why so many operating systems
are rejected by users. Most modern operating systems use a GUI
(Graphical User Interface) so we will explore this in more detail
by looking at the main characteristics.

Gul characteristics
Characteristic Description
windows Most GUI designs have multiple windows to allow different
information to be displayed simultaneously on the user’s screen.
These windows can be anything from folders to software applications.

icons Icons are ‘symbols’ or small images/graphics that are used


to represent files and commands. Icons are used for different
types of information. On some systems, icons represent files; on
others, icons represent processes. Icons can be clicked to carry
out a function or to open a file etc.

pointing Pointers are little arrows that you move across the screen by
directing your mouse or touch. Pointers can be used to select
and use icons, select options in menus and to reposition folders
and icons on the screen.

menus Commands are selected from a menu rather than typed in a


command language.
graphics Multiple graphical elements are often mixed with text on the
same display.

Portable devices such as mobile phones, PDAs (personal digital


assistants) and tablets use interfaces very similar to a GUI with
similar icons and menus used for user inputs. But, because these
devices don’t have a mouse, touch-screen technology is used. This
allows the user to use their fingers to select icons and options that
are graphically shown on the device’s screen.

244
Application software

! Key point Extra characteristics for mobile de ces -

There are two types of system Characteristic Description


software: operating systems and pinching This is where you pinch your fingers together or move them
utility programs. apart on a touch screen to zoom in or out.

rotating This is where you use two fingers — one finger moves up while the
other finger moves down the touch screen to rotate an object.

swiping Swiping is where you swipe your finger across the touch screen
to scroll through a document or turn the page.

TV key poink § Application software


Application software consists of Application software is computer software that causes a computer to
programs that perform specific perform useful tasks beyond the running of the computer itself.
tasks, e.g. web browsers, word Such software is often called a software application, a program,
processing software. application or an app.
The word ‘application’ is used because each program has a specific
application for the user. For example, a word processor can help a
user write a document.
Application software programs are designed to run under
an operating system; they are a further abstraction from the
hardware. They range from word processors and web browsers to
video games and media players.
Examples of application software include:
® 3D computer graphics software
@ Animation software
® Audio editing
® Data manipulation (databases and spreadsheets)
® Digital audio editor
® Graphic art software
® Graphics editing
® Image editing software
® [mage organisers
e@ Media content creating/editing
@ Music sequencer
@ Presentation
@ Sound editing software
@ Text editors (word processors, desk top publishing)
@ Vector graphics editor
@ Video editing
® Video editing software
@ Web browser
] Key point
Never use brand names in At the time of writing this book the most popular application
software programs were: Microsoft Word, Google Chrome,
exams, use generic descriptions
such as ‘word processor’.
Windows Media Player, World of Warcraft, Adobe Photoshop,
iTunes, Skype and WordPerfect.
31 Software
ot
OTe

Mac programs were always called applications, but Windows


programs were referred to as executable files. This is why Mac
Lay

programs use the .APP file extension, while Windows programs


use the .EXE extension. But whatever the extension, both Mac and
Windows programs serve the same purpose so both should be
called software applications.

Translation software
We looked at translation software in earlier chapters but in
summary the purpose of translator software is to convert
program source code into machine code that can be executed on
the processor.
Translation software includes assemblers, compilers and interpreters.
Each performs a different task:
- An assembler (meaning one that assembles) is a computer
-_ program which translates assembly language to an object file or
~ machine language.

- Acompiler translates the whole program (source code) into


- object code that can be stored and reused. A compiler makes
faster, more secure code. It also produces object code that is
_ difficult to read, meaning competitors won't easily be able to
steal or users hack the code. We looked at compilers in more
~ detail in Chapter 15.

An interpreter allows code to run on multiple platforms. It allows


you to debug and test code without having to recompile the
entire source code.

2 Questions Modelling software


8 What is the purpose of a As computers become faster, computer models have become more
compiler? commonplace.
9 What is the purpose of an Real-world computer models tend to take one of two forms. They
interpreter? are either optimisation models, for example a model that helps a
10 Briefly describe the term parcel delivery driver to take the shortest route to deliver a load of
assembly language. packages, or simulation models.

Optimisation models are prescriptive — they tell the user what


needs to be done. Simulation models are used to clarify what
! Key point would happen in a given situation — they are not prescriptive.
In other words, simulation models are ‘what if’ tools. Simulation
For the examination you will need
models are used to model important aspects of the real world and
to understand how software can
show what could happen in certain scenarios.
be used to simulate and model
aspects of the real world. Flight simulators are one example of a simulation model. They
help to expose pilots and designers to routine and unexpected
flight circumstances. Computer games are also types of simulated
computer models, but the real benefits to society comes from
simulated models that can be used to explore real-world dangers
from flooding probabilities to health risks.

246
Application software

Areas of use of simulation models include transportation,


business, medical, and social science.
3 Use this book and other
sources such as the internet to | lmulated models are also used to explore the behaviour of
research how software can be systems under specified situations.
used to simulate and model
The rules behind models
aspects of the real world.
People use models every day. Our decisions and actions are based
not on the real world, but on our mental images of that world. We
mentally calculate the influence our actions have on our world.
Of course we make our decisions based upon a very flexible set
of thoughts and feelings — a computer model has to be far more
logical.
The logic, called the rules, has to be programmed into the system.
The rules are basically a set of variables controlled by the model’s
structure. The model's structure is based upon Boolean logic. The
very best computer models can also act on feedback. Any model
is only as good as its rules. In the case of simulation models, the
rules are based upon assumptions. These assumptions consist
of descriptions of the physical system being modelled and the
decision rules and structure the programmer creates.

Chapter review

In this chapter we have explored software.


Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand the legal impacts of using technology including intellectual
property, patents, licensing, open source and proprietary software and
cyber-security on society (More on this topic in Chapter 36).
® Know what an operating system is and how it manages files, processes,
hardware and the user interface
® Understand the purpose and functions of utility software
for managing, repairing and converting files; compression;
defragmentation; backing up; anti-virus, anti-spyware
® Understand how software can be used to simulate and model aspects
of the real world (More on this topic in Chapter 29).
oo Nae
ON
NN
\
Ds Specification references
You should:
5.1.1 understand why computers are connected in a network
5.1.2 understand the different types of networks (LAN, WAN) and
usage models (client-server, peer-to-peer)
5.1.3 understand wired and wireless connectivity
5.1.7 understand characteristics of network topologies (bus, ring, star,
mesh)
5.3.1 understand what is meant by the internet and how the internet is
structured (IP addressing, routers) 3
5.3.2 understand what is meant by the world wide web (WWW) and
components of the WWW (web server URLs, ISP, HTTP, HTTPS,
HTML)

§ Definition of a computer network


A computer network can be described as two or more computers
connected together to share resources. The purpose of connecting
| For the examination you will need computers together in a network is to exchange information and
to understand why computers are data; also networked computers can use resources of other
connected in a network. computers.

: You will often see the terms ‘client/server’ and ‘peer-to-peer’ when
e Question you read about networks, These terms describe a particular
1 What is a network? networking architecture. A peer-to-peer network has no central
server. Each workstation on the network shares its files equally
with the others. There’s no central storage or authentication of
users. We will return to these types of network later in the chapter;
1 Use this book and other for now we will focus on client/server networks.
sources such as the internet
There are a number of basic components of client/server computer
to research the different
networks and these are described below.
ways that computers can be
connected in a network.
32 Networks

ay

Server

Clients

Figure 32.1

Servers are powerful computers that provide services to the other


computers on the network.
A The server provides services to
a client.
Clients are computers that use the services that a server provides.
A The client requests services The communication media is the physical or wireless connection
from a server. between the devices on a network. This could be through cable in
an organisation’s local network, wireless signals, or the internet.
Network data speeds are measured in bits per second, either
using the units megabits per second (Mbps) or gigabits per second
(Gbps).
The network adapter or, as its often referred to, the network
interface card (NIC), is a circuit board that is equipped with the
components necessary for sending and receiving data. It is usually
either plugged into one of the available slots on a computer or is
built onto the motherboard. A transmission cable is attached to
the connector on the NIC.
The term resources refers to any peripheral device that is available
to a client on the network such as printers, fax devices and other
network devices; however, the term can also refer to data and
information.
A user is basically any person that uses a client to access resources
on the network.
The protocols of a network are formal, written rules used for the
network communications. They are essentially the languages that
computers use to communicate between each other on a network.
We will explore these in more detail in the next chapter.

250
Types of network ¢

Advantages of computer networks:


There are a number of advantages to using networks; these
include:
@ A network allows users to share software stored in a main
system.
@ Files can easily be shared between users over a network.
® Network users can communicate via email, instant messenger
and VoIP.
@ Within networks, it is much more straightforward to backup
data as it is all stored on the file server.
@ Networks allow data to be transmitted to remote areas that are
connected within local areas.
e@ Networking computers allows users to share common peripheral
resources such as printers, fax machines, etc. therefore saving
money.

Disadvantages of computer networks:


There are also a number of disadvantages to using networks,
which include:
@ Purchasing cabling to construct a network as well as the file
servers can be costly.
@ The management of a large network is complicated, which
requires training and a specialist network manager usually
needs to be employed.
® In the event of a file server breaking down the files contained
on the server become inaccessible, although email might still
work if it is stored on a separate email server. The computers
can still be used but are isolated.
® If a virus gets into the system through a network it can easily
spread to other computers.
@ There is a risk of hacking, particularly with wide area networks.
Stringent security measures are required to prevent such abuse,
such as a firewall.

Types of network
Local area network (LAN)
! Key point A LAN is characterised by high-speed transmission over a
For the examination you will
restricted geographical area. If the LAN is too large, signals need
need to understand the different
to be boosted. A networked school or home usually contains a
single LAN. Sometimes a single building will contain a few small
types of networks (LAN, WAN)
LANs. Occasionally a LAN will span a group of nearby buildings
and usage models (client-server,
but no more than this, which is why we say it has a restricted
peer-to-peer).
geographic area.
32 Networks
ye
an

Wide area network (WAN)


wi

? Questions
While LANs operate where distances are relatively small, wide
2 What is the geographical area
area networks (WANs) are used to link LANs that are separated by
of a LAN?
large distances that range from a few tens of metres to thousands
3 What is the geographical area of kilometres. The internet is the biggest example of aWAN. WANs
of a WAN? are often under collective or distributive ownership. The internet,
4 What is a web server? for example, is not owned by any one person or organisation, it is
distributed by all its service providers for the mutual benefit of all.

Personal area network (PAN)


2 Use this book and other A PAN is a computer network organised around an individual
sources such as the internet to person. Personal area networks often involve mobile computers,
research the different types of cell phones and/or handheld computing devices such as PDAs
networks. (personal digital assistants) and use Bluetooth.

§ Network topologies

Bus topology
Bus networks, which have absolutely nothing to do with the
system bus of a computer, use a common backbone to connect all
devices.
Client 1 Printer Server Scanner Client 2

Router

gu OP peer © pepper,
Client 3 Client 4 Client 5 Client 6 Client 7

Figure 32.2 A bus network

A single cable that functions as the backbone of the network acts


as a shared communication medium that devices connect to via
an interface connector. When a device wishes to communicate
with another network device, it transmits a broadcast message
onto the ‘backbone’ wire that all the devices see; however, only
the intended device actually accepts and processes the message.
Bus topologies are easy to install and require only a relatively
small amount of cabling when compared with alternative
topologies. Bus networks are the best choice of topology when the
network has only a limited number of devices. If the network

252
Network topologies
SSS

] Key point grows so there are more than a few dozen computers connected to
the backbone bus, then it is probable that performance issues will
For the examination you will need start occurring. Finally, if the backbone cable suffers a catastrophic
to understand the characteristics failure, then the entire network effectively becomes unusable.
of network topologies (bus, ring,
star, mesh). Advantages of bus topology:
@ It is easy and cheaper than other types of wired network to install
as a consequence of requiring only a small quantity of cable.
? Question ® The lack of dependency on a central device which is present in
5 Describe the term network. a star topology makes the system more flexible.
Disadvantages of bus topology:
@ It performs well only for a limited number of computers because
as more devices are connected, the performance of the network
3 Use this book and other becomes slower as a consequence of data collisions.
sources such as the internet @ The impact of a single cable failure makes this type of wired
to research how network data network more vulnerable than other wired networks.
speeds are measured.
Star topology
Nearly all wired home networks use the star topology. This
topology is, therefore, better understood than many other
A router is a device that directs networks. The star network has a central connection point referred
messages between computers in to as a ‘hub node’ that could be a device such as network hub,
a network. switch or router. The router on its own cannot be the hub node of a
network. It needs to be linked to a switch although in practice they
are usually in the same box. Devices usually connect to the hub
with network cable that is referred to as an ‘Unshielded Twisted
Pair’ (UTP) Ethernet.

Computer Printer

Figure 32.3

e Question | Star networks generally require more cable than bus topologies;
however, a failure in any star network cable will only restrict
6 Describe a star network.
access to the computer that is connected via that cable and not the
entire network. It should be realised, however, that if the hub
fails, the entire network also goes down.
& 32 Networks
4,

eo Advantages of star topology:


~ e Compared with bus topology, star topology is better in terms
of performance as signals don’t necessarily get transmitted to
all the workstations although performance does depend on the
capacity of the central hub.
@ The transmission medium is not shared so there can be multiple
simultaneous communications.
@ Failure of one node or link doesn’t affect the rest of the network
and it is easy to detect a failure and troubleshoot it as it allows
isolation of each device within the network.

Disadvantage of star topology:


@ The network operation ultimately relies on the correct
functioning of the central hub. So if the central hub crashes it
will lead to the failure of the whole network.
® The use of a hub, router or switch as the central device and the
additional cabling costs increase the overall cost of the network.
@ Performance and the number of nodes which can be added
depend on the capacity of the central device.

Ring topology
When every device has exactly two neighbours for communication
purposes, the network layout is referred to as a ring topology.
Client 4

Client 3

Client 1
gS
aa
Client 2

Figure 32.4 A ring network



Network topologies

In ring topology, all messages pass ‘around in the same direction.


This can be either clockwise or anticlockwise. As with the bus
topology, should a failure in any cable occur or, in this case, if
a device breaks down in the loop, it can take down the entire
network.
Ring topologies are found in some office buildings or school
campuses.
Advantage of ring topology:
@ Messages being sent between two workstations pass through
all the intermediate devices, so a central server is not required
for the management of this topology.
Disadvantages of ring topology:
@ The failure of any cable within the network can cause the entire
2 Questions
network to crash.
7 Describe a ring network. e Alterations, maintenance or changes being made to the network
8 What is the purpose of a nodes can impact the performance of the whole network.
server? @ Each device/computer needs additional software to handle
passing of messages.

Peer-to-peer network

Figure 32.5

Peer-to-peer networks, often referred to as P2P networks, are a


decentralised communications model in which each user on the
network has the same capabilities and any user can initiate a
communication session. Unlike the client/server model we looked
at before where the client makes a service request and the server
fulfils the request, the P2P network model allows each node to
function as both a client and a server.

Advantages of P2P networks:


@ It is very easy to install and configure computers on a P2P
network.
® P2P is reliable as there is no dependency on a server.
32 Networks

@ Failure of one peer doesn’t affect the functioning of other peers.


e Each user is the administrator of their own machine. Each user
9 Briefly describe the term controls their own shared resources.
firmware.
Disadvantages of P2P networks:
e As the whole system is decentralised it is difficult to administer.
No single person can determine accessibility for the whole
For the examination you will need network.
to understand what is meant by @ Security in a P2P network is very difficult so spyware, trojans,
the internet and how the internet etc. can easily be transmitted.
is structured (IP addressing, @ Data recovery or backup is very difficult. Each computer needs
routers). to have its own backup system.

Mesh network
The latest network arrangement is mesh networking. In a mesh
network each node connects to at least two other nodes and
can connect to every other network node. If this happens, the
arrangement is referred to as being ‘fully connected’.

2 Question
10 Briefly describe the term web
server.

Figure 32.6

The best example of a mesh network is the internet. Information


travels across the net by being forwarded automatically from one
router to the next. The internet is called ‘the cloud’ because a
signal can take literally billions of potential paths.

Advantage of mesh networks:


@ Mesh networks can heal themselves automatically when a break
occurs, so there’s no interruption of service to any node.
@ Mesh networks get bigger and faster as more nodes are added.
Disadvantages of mesh networks:
© This type of network involves more cabling or more wireless
devices, leading to higher cost.
@ Mesh networks are still in development and a full set of
recognised standards have not been adopted yet.

256
Network topologies

Wireless network

Router

Figure 32.7 A wireless network

A wireless network (Wi-Fi) uses radio waves to communicate. Radios,


! Key points
mobile phones and televisions use radio waves too.
A For the examination you will
This is what happens: a computer’s wireless adapter translates
need to understand wired and
data into a radio signal and transmits it using an antenna, and
wireless connectivity.
receives radio signals and converts them into data (remember
A Wi-Fi is a family of related
data has to be 1s and Os).
protocols rather than a single
protocol. Advantages and benefits of wireless networks:
A Wi-Fi is a trademark; the @ The primary benefit of a wireless network is the freedom
generic term for networks of from cables, as long as the device connecting to the network
this nature is WLAN. is within range of a wireless router. It is also very convenient.
The wireless nature of Wi-Fi networks allows users to access
A The Ethernet is a family of
network resources from almost any convenient location.
related protocols rather than a
@ With the growth of public wireless networks, users can
single protocol.
also access the internet outside their normal work or home
environment. For example, most public places such as chain
coffee shops, airports, libraries, etc. offer their customers a
wireless connection to the internet at little or no cost.
@ The initial setup of an infrastructure-based wireless network
is relatively low cost as it requires no expensive cabling and a
small network needs just a single access point.
@® Wireless networks are also easily expanded.
32 Networks
eee

Risks and disadvantages of wireless networks:


ee
e Any time data is sent wirelessly, there is a chance that it can
be intercepted. The security used to encrypt the information
determines how easy or hard it is to intercept the data. Some
of the more commonly used encryption methods have known
weaknesses. If a user sets up a wireless router without any
security, they are asking for trouble. People standing outside
their home with a laptop or mobile phone would be able to use
their internet connection and possibly even access the files on
other networked computers.
A Wi-Fi range, whilst sufficient for a typical home, can be
insufficient in a larger building. To obtain additional range,
repeaters or additional access points have to be purchased.
Wireless networking signals are subject to a wide variety of
interference and thick walls, metal used in building construction
and static interference all cause networking problems.
The speed on most wireless networks is much slower than on
the slowest common wired networks. In some situations where
speed is essential a user may need a wired network.

Chapter review

In this chapter we have explored different types of network.


Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand why computers are connected in a network
® Understand characteristics of network topologies including bus, ring,
star and mesh networks
™® Understand the different types of networks (LAN, WAN) and usage
models (client-server, peer-to-peer)
® Understand wired and wireless connectivity (More on this topic in
Chapter 33)
® Understand what is meant by the internet and how the internet is
structured (IP addressing, routers)
® Understand the components of the WWW including the terms web
server URLs, ISP, HTTP, HTTPS and HTML (More on this topic in
Chapter 33).
+f Specification references
You should:
4.2.6 understand the need for embedded systems and their functions
5.1.3. understand wired and wireless connectivity
5.1.4 understand that network data speeds are measured in bits per
second (Mbps, Gbps)
5.1.5 understand the role of and need for network protocols (Ethernet,
Wi-Fi, TCP/IP, HTTP. HTTPS, FTP, email (POP3, SMTP, IMAP))
5.1.6 understand that data can be transmitted in packets using layered
protocol stacks (TCP/IP)
5.3.2 understand what is meant by the world wide web (WWW) and
components of the WWW (web server URLs, ISP, HTTP, HTTPS,
HTML)

Any electronic communications process requires the following


components:
1 Use this book and other ® a source of the information
sources such as the internet @ a transmitter to convert the information into data signals
to research how data can be compatible with the communications channel
transmitted in packets using ® a communications channel
layered protocol stacks. @ a receiver to convert the data signals back into a form the
destination can understand
@ the destination of the information.
The transmitter encodes the information into a suitable form to be
transmitted over the communications channel.

! Key points The communications channel moves this signal as electromagnetic


A For the examination you will energy from the source to one or more destination receivers.
need to understand that The channel may convert this energy from one form to another.
network data speeds are This could be electrical or optical signals. It must maintain the
measured in bits per second integrity of the information so the recipient can understand the
(Mbps, Gbps). message sent by the transmitter.
A Data integrity is the state
The communication media can be either cable or wireless. The
of data being exactly as it
copper cable may be coaxial or twisted-pair. The alternative is
should be.
fibre optic cable.
P 33 Network data transfer

Electrical signal communication via the Electrical signal communication via the Optical signal communication via the
wires inner conductor of the wires glass fibres

Noise contamination high Medium noise contamination Very low noise contamination

Can be affected by external magnetic Less affected by external magnetic Not affected by magnetic interference
interference interference

Low bandwidth Medium bandwidth High bandwidth

Easy installation Reasonably easy installation Difficult installation

Lowest cost of the three communication Moderately expensive compared with Most expensive of the three
media coaxial cable communication media due to the cost
of the electronic components needed

§ Benefits and drawbacks


Copper cable is expensive, but most PCs come with copper NIC
cards. Replacing these with optical ones can be quite expensive.
Also traditional systems were built to run on copperinfrastructures.
Fibre can be used to replace these, however the electronics needed
to make it work are also relatively more expensive as is the cost of
replacing all the existing cables.
Fibre has a higher bandwidth than copper and the cable itself
is actually less expensive than copper, but the electronics that
it requires to transmit data are expensive. An optical cable also
weighs less than a comparable copper wire cable. Fibre can retain
a higher bandwidth over greater distances than comparable
copper cabling and eavesdropping on a LAN using copper cables
is easier, making fibre more secure.

Bandwidth
Bandwidth affects the amount of data that can be transferred in a
given amount of time. The quantity of information a channel can
convey over a given period is determined by its ability to handle
the rate of change of the signal called its frequency. An analogue
signal varies between a minimum and maximum frequency and
the difference between those frequencies is the bandwidth of that
signal. So the bandwidth of an analogue channel is the difference
between the highest and lowest frequencies that can be reliably
received over the channel.
Digital signals are made up of a large number of frequency
components. Only those within the bandwidth of the channel
can be received. It follows that the larger the bandwidth of the
channel, the higher the data transfer rate can be and the more
accurate the transmitted signal can be.

260
SY
Network protocols

Noise
All transmission media generate some noise. As the signals pass
through a communications channel the atomic particles and
molecules in the transmission medium vibrate and emit random
electromagnetic signals as noise. When the wanted signal is
not significantly higher than the background noise, the receiver
cannot separate the data from the noise and communication errors
occur. Fibre optic cable has the least noise of all the transmission
media used in networks.

! Key points § Network protocols


4 A protocol is a set of rules A protocol is, in one sense, nothing more than a set of rules that
or standards that control govern data transmission so that a particular type of data will be
communication between formatted in a particular mannet.
devices.
The importance of these protocols is that they provide a standard
A For the examination you will
way to interact among networked computers.
need to understand:
A the role of and need for Addressing
network protocols (Ethernet, In many ways, internet addressing is similar to the postal
Wi-Fi, TCP/IP, HTTP. HTTPS, addressing system.
FTP, email (POP3, SMTP,
IMAP)) The address system on the internet is called Internet Protocol (IP)
A that data can be addressing. In IPv4 an IP address assigned to a host is 32 bits long
transmitted in packets using and is unique.
layered protocol stacks An IP address has two parts: one part that is similar to the postal
(IGP/IP). code and the other part that is similar to the house address. They
are known as the net id (netid) and the host id (hostid).
The host is the end point of communication on the internet and
where a communication starts. It could be a web server, an email
server, or a desktop, laptop or any computer we use for accessing
the internet.
The netid identifies a contiguous block of addresses and is used to
identify which particular network the host is located on.
Another popular form of address is the Media Access Control
(MAC) address. MAC addresses are six bytes (48 bits). The
computer’s own hardware configuration determines its MAC
2 Questions address. The configuration of the network it is connected to
determines its IP address.
1 What is meant by a network
protocol? The first half of a MAC address contains the ID number of the
2 What is an IP address?
adapter manufacturer. The second half of a MAC address
represents the serial number assigned to the adapter by the
3 What is an address?
manufacturer.
33 Network data transfer
ac
a al

TCRAR
When we meet someone in business we shake their hand as a
ereeting. This is true in computing too, when two computers
connect in a network they first use a handshake.
For computers to work together they have to use what are called
protocols. A protocol is a set of rules which governs the transfer
2 Use this book and other of data between computers.
sources such as the internet
to research validation and
Protocols are essential for any communication between computers
and networks. They determine the speed of transmission, size of
authentication techniques.
bytes, error checking methods, and even whether communication
3 Use this book and other
will be asynchronous or synchronous.
sources such as the internet
to research how to protect Like any delivery system, in networks we need a delivery model.
software systems from When you use the postal system you can ask for guaranteed
cyberattacks. delivery. On the internet TCP/IP (Transmission Control Protocol/
Internet Protocol) is the most common delivery model. TCP is in
charge of the reliable delivery of information, while IP is in charge
of routing, using the IP addressing mechanism.
TCP/IP (Transmission Control
Protocol/Internet Protocol) is a
All networked computers communicate through what are called
transmission control protocol /
protocol suites. The most widely used and most widely available
internet protocol consisting of
protocol suite is the TCP/IP protocol suite. TCP/IP is a 4-layer
a set of standards that control
system as shown in the diagram.
how data is sent across networks
The application layer
including the internet.
The application layer provides applications for file transfer, remote
control and internet activities.
Webserver

Your laptop
Some of the most common application layer protocols are:
e FTP (File Transfer Protocol): a standard network protocol that
is used to transfer computer files from one host to another host
over a TCP-based network, including the internet.
e HTTP (Hypertext Transfer Protocol): HTTP is the underlying
protocol used by the world wide web. HTTP controls how
messages are formatted and transmitted. It also controls the
actions of web servers and browsers in response to received
commands. When you enter a URL in your browser it sends an
HTTP command to the web server instructing it to fetch and
transmit the requested web page.
e IMAP (Internet Message Access Protocol): IMAP is an internet
standard protocol used by email clients to retrieve email
messages from a mail server over a TCP/IP connection.
@ HTTPS (HTTP Secure): a protocol for secure communication
Over a computer network. It is widely used on the internet.
HTTPS consists of communication over Hypertext Transfer
Protocol (HTTP) within a connection encrypted by Transport
Layer Security.
Figure 33.1 Layers of TCP/IP

ce
eee
Ne twiorkeproionos
® Simple Mail Transfer Protocol (SMTP) is a TCP/IP protocol used
in sending and receiving email. SMTP can only send data to a
computer that is turned on, so it is only used to send data from
the sender to mail servers and then on to other mail servers.
POP3 and IMAP are used to fetch email/manage a mailbox
when the receiving computer is turned on.
@ SNMP (Simple Network Management Protocol): created as a
way of gathering information from different networked systems
in a consistent way. It can be used in connection to a wide array
of systems. The method of querying information and the paths
to the relevant information are standardised. There are many
versions of the SNMP protocol, and most networked hardware
devices implement some form of SNMP access.
@ IMAP, POP and SMTP protocols are discussed more fully later
in this chapter.

The transport layer


Below the application layer is the transport layer. It is the main
interface for all network applications.
The most commonly used transport layer protocols are:
e TCP
e UDP.

Differences between TCP and UDP

Data is read as a byte stream, UDP is a connectionless protocol.


no distinguishing indications are When data or messages are sent,
transmitted to signal message there is no guarantee they will arrive.
(segment) boundaries. TCP is a There may also be corruption while
connection-oriented protocol. If a transferring a message. Packets are
connection is lost, the server requests sent individually and are guaranteed
the lost part. to be whole if they arrive. There is one
packet per read call.

TCP is suited for applications that UDP is suitable for applications that
require high reliability, and transmission need fast, efficient transmission, such
time is relatively less critical. as games. UDP is also useful for
servers that answer small queries from
huge numbers of clients.

If two messages are sent along a Messages are not ordered. When
connection, one after the other, the two messages are sent out they may
first message arrives first. Data cannot arrive in a different order, If ordering is
arrive in the wrong order. required, it has to be managed by the
application layer.

Data is read as a ‘stream’, with nothing There is no tracking of connections,


distinguishing where one packet ends etc. UDP is faster because there is
and the next begins. There may be no error-checking for packets and the
multiple packets per read call. network card/OS has less work to do
to translate the data back from the
sent packets.
33 Network data transfer

The network layer


! Key point The network layer is considered the backbone of the OSI Model.
The network layer is sometimes It selects and manages the best logical path for data transfer
referred to as the internet layer. between nodes. The network layer addresses and packages data
for transmission and it then routes the packets across the network,
the name given to packets at this level is IP datagrams. The
network layer is also sometimes referred to as the internet layer,
this is because the source and destination IP addresses are added
to the packets. The network layer is responsible for taking routing
decisions, based on the destination IP address to determine which
route to send the datagram along. This layer contains hardware
devices such as routers, bridges, firewalls and switches, but it
actually creates a logical image of the most efficient communication
route and implements it with a physical medium. Network layer
protocols exist in every host or router. The router examines the
header fields of all the IP packets that pass through it.
The main protocol used at this layer is IP.
4 Use this book and other This layer also formats the data for transmission and provides
sources such as the internet error control for data delivered on the physical network.
to research the security issues
associated with the ‘cloud’ The data link layer
and other contemporary The data link layer is sometimes known as the network interface
storage. layer. The data link layer is where most LAN (local area network)
Use this book and other and wireless LAN technologies are defined.
sources such as the internet This layer normally consists of device drivers in the OS and the
to research different forms of network interface card attached to the system. The data link layer
cyberattack. is the protocol layer in a program that handles the moving of data
in and out across a physical link in a network.
The device drivers and the network interface card control all
communications with the media being used and transfer the data
over the network.
The most commonly used data link layer protocols are:
@ ARP (Address Resolution Protocol)
@ PPP (Point to Point Protocol).
The data link layer is responsible for encoding bits into packets
prior to transmission and then decoding the packets back into bits
at the destination. The data link layer is also responsible for logical
link control, media access control, hardware addressing, error
2 Question detection and handling and defining physical layer standards.
4 What is a connectivity device? The data link layer processes data faster than the network layer
because less analysis of the packet is required.

264
Data packets ( ~

§ Data packets |
Anything sent between computers has to be divided up into what
are called packets. Packets are small data units. Even a PowerPoint
or spreadsheet has to be divided into packets to be transmitted.
Imagine an object built of Lego where each brick is a packet.
Of course once transmitted the packets have to be put back together
in the correct order. These protocols wrap each data packet with
a set of instructions. The name for this is encapsulation. Once all
the packets have been received, the client needs to know they have
all arrived so the very last packet is a special one called a frame.

Packet 1 Packet 2
Message
tobe | mmmt Menace. Xl mam |Receved
sent ge
Packet 3 Packet 1

Divide outgoing Packets transmitted Packets are


message into by the internet reassembled to
packets give the messaae
Figure 33.2 Sending data packets

° Question Checksum
A simple error-detection scheme is used in which each transmitted
5 What is a packet?
package is accompanied by a numerical value based on the number
of set bits in the message. The receiving station then applies
the same formula to the message and checks to make sure the
accompanying numerical value (the checksum) is the same. If not,
the receiver can assume that the message has been corrupted.

Network routing and data rate


We know that packets are routed from a source to a destination.
These packets may need to travel via lots of cross-points, similar
to traffic intersections in a road transportation network. Cross-
points in the internet are known as routers. A router’s function
is to read the destination address marked in an incoming IP
packet, then to identify an outgoing link where the packet is to be
forwarded, and to forward the packet.

Similar to the number of lanes and the speed limit on a road, a


network link that connects two routers is limited by how much
data it can transfer in any given amount of time. This is called the
bandwidth or capacity and it is represented by a data rate, such as
e Questions
1.54 megabits per second (Mbps).
6 What is the purpose of a
router on a network? Suppose that traffic suddenly increases, for example because
of many users trying to download from the same website. The
packets generated can be queued at routers or even dropped.
| ae
33 Network data transfer

, Routers only have a limited amount of space, known as a buffer,


} to store backlogged packets. It is possible to reach the buffer limit.
Since the basic principle of TCP/IP allows the possibility of an IP
packet not being delivered, the limit of the buffer is not a problem.
On the other hand, from an efficient delivery point of view, it is
desirable not to have any packet loss (or at least, to minimise it).

§ Email protocols
When you send and receive an email there are three different
2 Questions
protocols that can be used to handle the email:
7 What is a mail server? e IMAP
e POP
e SMTP.

IMAP and POP are used to receive mail. SMTP is used to send mail.

Client Mail Server

SMTP

Client

Mail Server
Figure 33.3 Sending email using SMTP

Receiving emails
The software you use on your local machine is called a client and
this is connected to what is called a mail server. When someone
sends you an email it travels from the sender’s email client
to
their mail server using SMTP. Their mail server then uses
the
recipient’s email address to determine where the mail should
be
sent and then delivers it to the receiver’s mail server.
266
SS
Email protocols

The receiver’s mail server stores the received email in a mailbox


until the receiver’s mail client asks for it. IMAP and POP are the
two main protocols used for retrieving email from a mail server.
Both protocols are supported by almost all popular mail client
programs including Outlook and Apple Mail.
When a mail client reads an email it can do one of two things.
The IMAP protocol, by default, allows the user to keep all messages
on the server. It constantly synchronises the email client program
with the server and displays messages currently there. Actions
performed on the messages are carried out directly on the server.
The POP protocol is set by default to download all the messages
from the email server onto the client device. This means that all
the actions performed on the messages (reading, moving,
deleting, ...) are performed on the client machine. Because
6 Use this book and other everything is kept on the client machine, the user cannot reopen
sources such as the internet to messages from any machine other than where the messages have
research what is meant by the been downloaded. There is, however, an option to set up the POP
world wide web (WWW). protocol to save a copy of the message on the server after
downloading it on the client device.

Sending emails
When you send an email you will always use some form of SMTP,
which is a totally different protocol to IMAP and POP.
When you click on Send for an email message, your mail client
7 Use this book and other
contacts the SMTP server. The server authenticates who the user
sources such as the internet
is using the login id and password, then it receives the email
to research the importance of
message from your client. The mail client then places the sent
email in either the local sent mail folder or in the case of IMAP,
network security.
the sent email folder on the server.

Chapter review

In this chapter we have explored different ways that ® Understand wired and wireless connectivity (More
data is transferred over networks. ,_on this topic in Chapter 32)
Remember, before tackling any computer science task Understand that network data speeds are
or examination question on this topic you must: measured in bits per second (Mbps, Gbps)

® Understand that data can be transmitted in ® Understand the role of and need for network protocols
packets using layered protocol stacks (TCP/IP) including the terms Ethernet, Wi-Fi, TCP/IP, HTTP.
® Understand what is meant by the world wide web HTTPS, FTP and email (POP3, SMTP, IMAP)
(WWW) ® Understand the need for embedded systems and

® Understand the components of the WWW including their functions (More on this topic in Chapter 37).
the terms web server, URLs, ISP, HTTP, HTTPS, HTML
(More on this topic in Chapter 32)
ee
Spree
an
oa
=

Specification references
:You should:
5.2.1 understand the importance of network security 5.2.4 understand methods of identifying
and be able to use appropriate validation and vulnerabilities including penetration testing,
authentication techniques (access control, ethical hacking, commercial analysis tools and
physical security and firewalls) review of network and user policies
5.2.2 understand security issues associated with the 5.2.5 understand how to protect software systems

‘cloud’ and other contemporary storage from cyberattacks, including considerations


5.2.3 understand different forms of cyberattack at the design stage, audit trails, securing
(based on technical weaknesses and operating systems, code reviews to remove
behaviour) including social engineering code vulnerabilities in programming languages
(phishing, shoulder surfing), unpatched and bad programming practices, modular
software, USB devices, digital devices and testing and effective network security provision
eavesdropping

In the next two chapters we will explore why it is essential to use


strong passwords, keep anti-virus software and security patches
up to date, log out of services when we finish using them, and
be careful of the information we send in emails, texts or instant
messages.

fi Issues
With all the benefits technology brings it also brings issues. The
J Key points
only time anything is private in computing is when the computer is
A HTTPS provides an encrypted off-line. We all put private information onto the internet but only
version of HTTP for more in the hands of people who we believe will look after it. Never put
secure web transactions. any private information on the open public internet. When using
4 For the examination you will online banking, make sure the connection is encrypted (HTPPS).
need to understand what is
meant by the world wide web File sharing
(WWW) and components of The illegal sharing of music and films over the internet has
the WWW (web server, URLs, become more and more of a problem as internet speed has grown
ISP, HTTP, HTTPS, HTML). faster. Famous court cases based upon intellectual property rights
and privileges have hit the news.
34 Personal vulnerabilities

Software piracy |
! Key points
With the growth of the use of the internet more and more people
are using illegal, sometimes borrowed, software. Billions of pounds
A Copyright and patent laws
benefit society because
are lost as a result of pirated software.
they encourage creators of
new products to make them Hacking
available to the public.
Hacking refers to accessing data without authority. It has become
A Quarantine is a separate area more and more of a problem over the years. Sometimes the hackers
of a hard disk that holds an unlawfully access the data, but do not damage it. But often hackers
infected file until a virus can hack into data to either steal from it, or to cause malicious damage.
be removed. Either way it is illegal to hack into someone else’s data.
A Content filtering is a process
of restricting access to certain Web content
material on the web. The world wide web has revolutionised communication. It is a
4 Spam is unwanted, unsolicited wonderful place for information exchange and self-expression.
email. Anyone can gain an extensive global audience. But in recent
A A domain name is a human
years this has led to a rapid growth in negative materials such as
readable name for a resource
pornographic material, instructions for making bombs, and hate
location on a network. It is propaganda.
changed to a numerical IP
Invasion of privacy
address by a DNS server.
The chances are that the more you use the internet the easier it is
A Authentication is any process
for someone to invade your privacy. Your data is probably stored
by which someone Is verified
around the world in a wide range of databases. Your activity and life
to actually be who they claim
are probably being monitored. And it is not just the internet where
to be.
this is a problem; when someone goes shopping with a loyalty card
A Authorisation is any process
their details and what they have purchased are recorded. Critics of
in which someone is allowed,
the system argue that these loyalty cards compromise the privacy
after being authenticated, to
of customers, but the retailers who use them argue that they are
access data or information
a way of generating special offers targeted to the right customers.
that they requested.
Computer viruses
It is estimated a single computer virus can result in over £10
million of damage. With so many viruses about, even in email
attachments or in downloadable files and screen-savers, the issue
of computer viruses is a growing problem.

4 A virus is a computer program that is spread 4 Malware is malicious software designed by


when its payload is executed, interfering with the attackers to compromise security and do damage.
hardware, software or OS. 4 A companion is a virus that creates a program with
4 A virus is malware that needs a host application to the same name as an existing, legitimate program
transport itself, and tricks the OS into running it to deliver the
4 A logic bomb is malware designed to execute and virus.
do damage after a certain condition is met.

270
Issues

? Question Encryption
1 Explain the term encryption. When we are using networks for example ordering something on-
line or completing forms, we often have to send others our personal
information. A typical transaction might include our names, e-mail
! Key points | addresses, physical address, phone numbers, passwords and even
A Bugs are errors in software, our credit card details. Information security is provided on computers
Causing the program to and over the networks by a variety of methods but many of these
function in a manner that was methods use encryption. Encryption is a way to enhance the security
not intended. of a message or file by scrambling the contents so that only someone
A A hyperlink is an item ona with a secret code or key can read it.
web page that directs the There are two main types of encryption but the most common is
user to another location when what is called symmetric encryption. Basically this just means that
clicked. the same key, or secret, is used to encrypt and decrypt the data. The
A The term backdoor refers to an second main type of encryption is called asymmetric encryption
undocumented, unauthorised and this uses two interdependent keys, one to encrypt the data, and
method of gaining remote the other to decrypt it.
access to a system, bypassing
normal authentication methods. Deep linking
A For the examination you will Users of the world wide web often move from page to page following
need to understand different hyperlinks, which can appear as images or text. Originally hyperlinks
forms of cyberattack (based were at the heart of the world wide web. Sometimes the hyperlink takes
on technical weaknesses a user away from the website they are on to another website. Deep
and behaviour) including linking occurs when one web page includes a hyperlink to a web page
social engineering (phishing, that is buried deep within another site. This can give the appearance that
shoulder surfing), unpatched the hyperlinked pages are part of the original website. A growing number
software, USB devices, digital of companies are concerned that their data will be stolen.
devices and eavesdropping.
Cookies
A A worm is self-contained
Internet cookies are very small text files. They are downloaded
malware.
from the web server to a web browser. They record the activities
Bluetooth is a wireless on the browser, and then send them back to the server. Whilst
protocol/service allowing cookies can be very useful for autocompleting forms, they can
enabled devices to also be used by advertisers and less scrupulous users.
communicate and transfer
data. Unlicensed computer professionals
A Discovery mode is a Bluetooth Plumbers and electricians, doctors and most other people who
scheme that allows devices provide aservice to the public are usually licensed by law. Computer
to automatically detect and professionals are not. This makes it almost impossible to know
connect with other devices. whether the person who is fixing your computer is competent and
A Bluesnarfing is unauthorised reliable. Remember that people accessing your computer have full
access of information using a access to all of the data on it.
Bluetooth device in discovery
mode. Downloadable components
There are a growing number of cases in the news of children
starting to play a free game, and downloading extra characters
and plug-ins, not realising that these are all charged for. The use
of a free game followed by purchasable components is a growing
34 Personal vulnerabilities

issue, particularly with mobile phone use. Free games also provide
a good method of delivering worms and malware.
For the examination you will need
to understand: Bluetooth vulnerability
A methods of identifying The use of Bluetooth on mobile phones and tablets has led to
vulnerabilities including security vulnerabilities.
penetration testing, ethical
Like many other secure channel protocols, Bluetooth works using
hacking, commercial analysis
an initial phase called pairing where two devices establish short
tools and review of network
shared keys of a few numbers, followed by a data protection phase
and user policies
where data is encrypted and authenticated with those shared keys.
4 how to protect software
systems from cyberattacks, The problem is that eavesdroppers were sometimes able to obtain
including considerations enough information to guess the pairing PIN, which opened up
at the design stage, audit the data on the paired device. New protocols have now been put
trails, securing operating in place to attempt to stop this.
systems, code reviews to
remove code vulnerabilities in
programming languages and
HTTPS
bad programming practices, Hypertext Transfer Protocol Secure (HTTPS) is a secure version of
modular testing and effective HTTP. It is HTTP that is used for the data that is sent between
network security provision. your browser and the website that you are connected to. The ’S’ at
A Security is the process of the end of HTTPS stands for secure. It means all communications
protecting systems and data between your browser and the website will be encrypted.
from unauthorised access.
HTTPS uses one of two secure protocols to encrypt the data
A An open network is one whose communications: SSL (Secure Sockets Layer) or TLS (Transport
specifications and protocols Layer Security). Both the TLS and SSL protocols use an
are public, allowing different ‘asymmetric’ Public Key Infrastructure (PKI) system.
companies to produce
compatible products. When your browser requests a HTTPS connection, the website
will first send its SSL certificate to your browser. This certificate
contains the public key needed to begin a secure data session.
Based on this initial exchange, your browser and the website then
initiate the ‘SSL handshake’.

i Security measures

MAC address filtering


Media Access Control (MAC) addresses identify every device on
your network. A MAC address is an alphanumeric string separated
by colons, like this: 00:03:D1:1A:2D:14. Networked devices use
this address as identification when they send and receive data
over the network.
In MAC address filtering you find the MAC address of every device
you want to allow on your network, and then you fill out a table
in the router’s user interface so that any device on the list can join
your network, but any device not on the list will be blocked from
joining the network even if it knows your password.

272
Security measures

The problem is that a hacker using a wireless network analyser will


be able to see the MAC addresses of every computer you've allowed
on your network, and can change his or her computer’s MAC address
to match one that’s in that table, but this is not an easy thing to do.

Firewalls
A firewall is a network security system that monitors and controls all
incoming and outgoing network data transfer based on a set of what
are called security rules. Firewalls can be either hardware or software.
® Hardware firewalls can be purchased as stand-alone hardware
but they are also often found in broadband routers.
@ Software firewalls are software based and must be installed
on the computer. The software can then be customised.
There are a number of techniques firewalls use to prevent harmful
information from getting through the security wall and these include:
® Packet filtering: looking at the data in each packet entering or
leaving the network
© Application gateway filtering: applying security mechanisms
or blocking services such as FTP
@ Proxy server filtering: intercepting all messages entering and
leaving the network and hiding the true network address.
In addition to limiting access to computers and the networks they
are connected to, firewalls can be used to allow remote access to
a private network using secure authentication.

Chapter review

In this chapter we have explored system security. (phishing, shoulder surfing), unpatched software,
Remember, before tackling any computer science task USB devices, digital devices and eavesdropping
or examination question on this topic you must: (More on this topic in Chapter 35)
® Understand and be able to explain the importance = ™ Understand and be able to explain methods of
of network security (More on this topic in Chapter identifying vulnerabilities including penetration
35) testing, ethical hacking, commercial analysis tools
and review of network and user policies (More on
® Be able to use appropriate validation and
this topic in Chapter 35)
authentication techniques (access control, physical
security and firewalls) (More on this topic in ® Understand how to protect software systems
Chapter 35) from cyberattacks, including considerations
at the design stage, audit trails, securing
® Understand and be able to explain security issues
operating systems, code reviews to remove code
associated with the ‘cloud’ and other contemporary
vulnerabilities in programming languages and
storage (More on this topic in Chapters 35 and 36)
bad programming practices, modular testing and
® Understand and be able to explain different forms effective network security provision (More on this
of cyberattack (based on technical weaknesses topic in Chapter 35).
and behaviour) including social engineering
35 Social engineering and cyber-security

Specification references
You should:
5.2.1 understand the importance of network security 5.2.4 understand methods of identifying
and be able to use appropriate validation and vulnerabilities including penetration testing,
authentication techniques (access control, ethical hacking, commercial analysis tools and
physical security and firewalls) review of network and user policies
5.2.2 understand security issues associated 5.2.5 understand how to protect software systems
with the ‘cloud’ and other contemporary from cyberattacks, including considerations
storage at the design stage, audit trails, securing
< 5.2.3 understand different forms of cyberattack operating systems, code reviewstoremove
: (based on technical weaknesses and behaviour) code vulnerabilities in programming languages
including social engineering (phishing, shoulder and bad programming practices, modular
surfing), unpatched software, USB devices, testing and effective network security
digital devices and eavesdropping provision

Social engineering is a non-technical method used by hackers to


gain access to data and to systems. It relies heavily on human
interaction and often involves tricking people into breaking
normal security procedures.
@ Virus writers use social engineering to persuade people to run
malware-laden email attachments.
® Phishers use social engineering to convince people to give them
sensitive personal information.
® Scareware hackers use social engineering to frighten people into
running software that is useless at best and dangerous at worst.

] Key points
4 A stealth virus is a virus that attempts to avoid 4 A file infector virus is malware that copies itself into
detection by masking itself from applications, other program files (exe, dil, bin, sys, com, drv); when
redirecting commands around itself. the infected file is executed it loads into memory
4 A retrovirus is malware that attacks or bypasses the and tries to infect other files. It is also known as a
anti-virus software and can potentially destroy the parasitic virus.
virus definition file. 4 A polymorphic virus is malware that changes its
form each time it is executed, avoiding detection/
removal by anti-virus software.

274
Examples of social engineering

Someone could use social engineering to break into a computer


network by trying to gain the confidence of an authorised user.
People often do not realise how valuable the data they have on
their systems is to a hacker. Social engineers often rely on the
natural helpfulness of people as well as on their weaknesses.
Other typical social engineering techniques include appealing
to a user’s vanity, authority or greed alongside old-fashioned
eavesdropping.
It’s easy to identify the people around us that could gain physical
access to our files, legitimately or not; they include our family,
friends and others. But identifying those who can gain access to
our information and computers remotely is impossible. This is
why using technology safely and securely is so important.
If you use secure computing practice, you can at least limit the
risk of an unauthorised individual accessing your files and
A programmer at any computer confidential information or installing malicious software on your
or other device connected to computer.
a network could easily modify
the software at that host. This
Two of the easiest things you can do to keep others from accessing
is why sensitive data should be
your account or information are to use a strong password and to
encrypted.
lock your computer when you walk away from your desk.

! Key points
? Question A Anti-virus software is an application designed to detect and remove
malware and viruses.
1 Describe three main security
measures required when A A trojan horse is non-replicating, malicious code contained inside
using an internet-connected what appears to be a useful program.
computer. A Spyware is a non-replicating program used to track user activities,
monitor their machines and relay personal information to attackers.

Social engineering is gaining


§ Examples of social engineering
unauthorised access or obtaining Remember that in a social engineering attack, an attacker uses
confidential information by taking human interaction to obtain or compromise information about an
advantage of the trusting human organisation or its computer systems. Social engineering tactics
nature of some victims and the involve acts such as deception, manipulation and intimidation.
naivety of others. The table on the next page shows some of these threats.
35 Social eg
eel engineering and cyber-security
ne erg ae ee

Phishing is used to attempt to persuade someone to enter


confidential data, for example usernames, passwords and
Phishing is a technique for credit card details, by pretending to be from a trustworthy
source. It is usually carried out by email spoofing or instant
obtaining sensitive information
messaging. Users are often asked to enter details at a fake
by asking users for their a -___ website which looks just like the proper version.
passwords, credit card numbers,
3 Shouldering oS Shouldering, also sometimes called ‘shoulder surfing’, is
etc. This is achieved by sending Re ia oe using direct observation techniques, such as looking over
an email while masquerading as ~~~ someone's shoulder, to get passwords, PINs, security codes
a legitimate entity such as the and similar personal data.

user’s bank.
Blogging = Blagging is the act of creating and using an invented scenario
_ to engage a targeted victim in a manner that increases the
~ chance the victim will divulge information or perform actions
that would be unlikely in ordinary circumstances.

§ Cyber-security
Cyber-security relates to the protection of information systems from
theft or damage. There are many cyber-security threats including
those listed in the table below.

Cyber-security is a term used to describe the protection of information


systems from theft or damage to the hardware, the software and to the
information on them.

Also known as advertising-supported software. This is any software package that automatically
shows adverts, such as a pop-up. It may also occur in the user interface of a software package or on
an installation screen. The main object of adware is to generate revenue for its author. Adware, by
itself, is harmless. However, some adware may include spyware such as keyloggers.

| Computer viruses A computer virus is a self-replicating program that attaches itself to existing programs and can
then easily spread from one computer to another. Viruses can increase their chancesof spreading
to other computers by linking to files on a network system. A virus attempts to make a computer
system or data files unreliable.

| Denial of service This is an attempt to make a computer or network system unavailable to its users. A DoS attack
(DoS) attacks is usually focused on preventing an internet site or service from functioning efficiently or at all,
temporarily or indefinitely. The attacks usually target sites or services hosted on high-profile web
_ servers such as banks and payment websites (for example, PayPal).

Hacking means finding weaknesses in an established system and exploiting them. A computer
hacker is a person who finds weaknesses in a computer system to gain unauthorised access. A
hacker may be motivated by a multitude of reasons, such as profit, protest or challenge.

| Keyloggers A type of spyware that logs the keys used when someone types on a keyboard and can collect data
from an infected computer, including personal information such as websites visited, user logins and
financial information.

Physical threats (for Computer systems consist of physical items such as keyboards, monitors, memory sticks/remov
able
example, loss/ theft storage devices, base units and servers. These can be lost or stolen very easily — especially memory
of devices) — sticks and portable storage devices.

Pharming is a scamming practice in which malicious code is installed on a personal computer


or server or DNS records are modified, misdirecting users to fraudulent websites without their
knowledge or consent.

276
Cyber-security

Spyware A type of malware (malicious software) installed on a computer system that collects information
_ about users without their knowledge. Spyware is usually hidden from a user and can be difficult to
detect. Spyware is often secretly installed on a user’s personal computer without their knowledge.
Spyware can also install additional software or redirect web browsers to different websites. Some
Spyware can change computer settings, which could lead to slow internet connection speeds or
changes in web browser settings.

Trojans A Trojan often appears to be something which is wanted or needed by the user of a PC but is actually
a stand-alone malicious program designed to give full control of the infected PC to another PC.
They can be hidden in valid programs and software. Trojans can make copies of themselves, steal
information, or harm their host computer systems.

A worm is a stand-alone computer program that replicates itself so it can spread to other computers.
Like a computer virus a worm can use a computer network to spread. But unlike a computer virus, it
does not need to attach itself to an existing program. Worms almost always cause some harm to a
network, even if only by consuming bandwidth.

2? Question
2 Briefly describe the terms 4 Adware is software that displays advertising and gathers information
on browsing habits.
a) adware.
A A worm is a Self-replicating program that does not alter files, but
b) phishing
resides in active memory and propagates itself by means of computer
c) pharming networks to consume resources.
d) spyware. A Pharming is a cyberattack intended to redirect a website’s traffic to
another, fake site.

Malicious code
Viruses, keyloggers, worms and many of the items in the table
above are different types of malicious code. They are often
confused and thought of as being the same thing. Both are pieces
of code that is able to replicate itself. However, they are distinctly
] : different with respect to the techniques they use and their host
e Key points system requirements. This distinction is due to the way they
4 A bot is a type of program that | attack the host systems.
runs automatically, performing Malicious code is also known as malware and includes: computer
tasks directed by an attacker. viruses, trojans, adware and spyware.
A A virus hoax is an email
message that warns users of Protecting your personal data from cyberattacks
a non-existent virus or other The following preventative measures can help a user protect
malware. themselves from cyberattacks.
A A secure site is a website that ® Do not give out personal information over the phone, on the web
uses encryption techniques to or in an email unless completely sure of the recipient. Always
secure its data. verify the authenticity of requests for any personal information.
A A personal identification
@ Encrypt your data.
number (PIN) is a numeric
® Keep your operating system, browser, anti-virus and other
password, either assigned by
critical software up to date.
@ Never click on links in emails. Even if you do think the email is
a company or selected by a
legitimate, go to the website and log on directly. Be suspicious of any
user.
unknown links or requests sent through email or text messages.

277
35 Social
ee
nn eee engineering and oem
cyber-security

@ Never open email attachments unless you know it is from


a reliable source and turn off the option to automatically
download attachments in emails.
@ Set secure passwords and don’t share them with anyone, also
avoid using common words, phrases, or personal information
in your passwords.

Protecting a network and computers from


cyberattacks
® Encrypt the data.
@ Ensure that any HTTP open sessions time out after a reasonable
time.
® Ensure that TCP times out after a reasonable time.
® Install a firewall.
® Install anti-malware and anti-virus protection.
® Lock the network. Many cyber attack victims are compromised
! Key points
via Wi-Fi networks. The best defence against this is to have
A A time-out Is where the server no wireless network at all. Wired networks, while less versatile
is set to think that there has due to the need for cables, are more secure. If you use Wi-Fi,
been too long an interval update it regularly to the latest encryption standard.
of time between (1) the @ Secure the hardware. Physically locking computers to a desk
establishment of a connection, using the small metal loop found on most laptop and desktop
and (2) the receipt of any devices prevents theft and access to network login data.
data, so the server drops the @ Use tracking software on all networked mobile devices.
connection.
A Cyber-security consists of Automatic updates and patches
the processes, practices If your device seems to be working fine, you may wonder why
and technologies designed you should apply a patch or software update. By not applying a
to protect networks, patch you might be leaving the door open for malware to come
computers, programs and in. Malware exploits flaws in a system in order to do its work, and
data from attack, damage or updates and patches are designed to close these doors as they are
unauthorised access. found. The time frame between an exploit and when a patch is
A A power-on password is released is continually getting shorter.
a password entered on
computer startup, before the
Passwords
OS loads. Passwords are inconvenient, but necessary to protect your
personal data. If you use the same password on all your devices
and accounts, your data and your devices will be vulnerable
to malware and hackers. So how do you ensure the security of
devices and data with effective passwords?
First you must change any default passwords that have been
provided by external sources and not set by you. Passwords
should not be easy to crack, which they will be if the password
is simply personal data, such as your post code, a phone number,
or a significant date such as a birthday or if you use the same
password on all your accounts and devices.

278
ene
Cyber-security
enrvercenenneee

The characteristics of a good password include having a password


that:
® cannot be found in a dictionary
® contains special characters and numbers
® contains a mix of upper and lower case letters
2 Question ® cannot be guessed easily based on user information (birth date,
3 Briefly describe the term
post code etc.)
authorisation. ® is changed whenever you feel there has been a breach of
security.
Make passwords complex but memorable.
@ You can replace letters with numbers or special characters, e.g.
Johns becomes JOhn5.
@ You can also use a phrase and turn it into a password, for
example pick a sentence, and reduce it to first letters of each
word only, e.g. ‘I Like Computer Science’ becomes ILCS.
@ Take a word and reverse spell it, e.g. Today becomes yadoT.
Choosing a strong password lowers the chance of a security breach
] Key points with your accounts and devices. The advice on passwords used to be
4 Make passwords complex but to change them regularly, but this has become more difficult as the
memorable. number of devices and different access sites used is ever growing.
A Unauthorised access is If a user changes all their passwords every few months, they often
the use of a computer or end up using weaker passwords and reusing them across multiple
network without permission; networks and devices. It is now considered to be more important to
unauthorised use is the use use strong, unique passwords everywhere than to change passwords
of a computer or its data for regularly, although doing both obviously leads to greater security.
unapproved or possibly illegal
Biometric devices
activities.
A biometric device authenticates a person’s identity by translating
A Computer crime is any illegal
a personal characteristic, such as a fingerprint, into a digital code
act involving a computer.
that then is compared with a digital code stored in a computer,
verifying a physical or behavioural characteristic. These are used
on a wide range of mobile devices.

Security misconfiguration vulnerabilities


When any of the components that make up a web application
are configured badly there is a target for attackers. Security
misconfiguration vulnerabilities can happen at platform level,
web server level, application server level and through custom
code. In order to provide a secure system, all of the component
parts of a network application need to be configured correctly.
There are many ways that misconfiguration can lead to security
vulnerabilities. To protect from these vulnerabilities there are a
? Question number of steps that need to be taken including:
4 Briefly describe the terms ® Keep software up-to-date by installing the latest updates and
a) authentication security patches.
@ Remove unused features including all the sample applications
b) biometrics.
that come with content delivery systems.

279
ah
i 35 Social engineering and cyber-security
a7

® Disable default accounts and change passwords. You should also


change usernames, passwords and ports for default accounts.
@ Develop a strong application architecture that effectively isolates
components and encrypts data. Ensure security settings are set
to secure values.
@ Runtoolssuchas automated scanners to check for vulnerabilities.
@ Secure all layers individually and don’t rely on one layer in a web
application providing security for layers lower down in the stack.
® Make sure folder permissions are set correctly on the server.

Unpatched and/or outdated software vulnerabilities


Software vulnerabilities, like malware, have serious security

! Key points implications. The companies that sell software are aware of these
security vulnerabilities and regularly release security updates to
A Shouldering is observing a address these flaws. Microsoft, Adobe, Oracle, Firefox and Apple
person’s private information are just some software companies that regularly release security
over their shoulder, e.g. updates. Some software companies like Google Chrome and
cashpoint machine PINs. Flash even release updates automatically and many of these are
A Malware is an umbrella term invisible to users.
used to refer to a variety of
Outdated and unpatched devices present a major security risk as
forms of hostile or intrusive
the unpatched software remains weak, leaving the user open to
software.
cyber-crime attacks. Updating systems with the latest security
patches protects against attacks that exploit vulnerabilities.
Applying security updates often also addresses technical issues
with the software and improves the software’s performance.

MAC address filtering


! Key point
Without MAC address filtering, any wireless client can join and
MAC address filtering allows authenticate with a Wi-Fi network providing they know the
devices to access, or be blocked network name and encryption keys. When MAC address filtering
from accessing, a network is enabled, the access point or router performs an additional check
based on their physical address using a different parameter.
embedded within the device’s
network adapter.
Each device you own also comes with a unique media access
control address called a MAC address. This is what identifies it on
the network. Normally, a router allows any device to connect to the
network providing it knows the appropriate passphrase. With MAC
address filtering a router first compares a device’s MAC address
against an approved list of MAC addresses and only allows the device
onto the Wi-Fi network if its MAC address has been specifically
verified and approved by the administrator of the network.
This provides an additional level of security but it is still not
totally secure as MAC addresses can be spoofed in many operating
systems. This is where a device pretends to have one of the
allowed addresses. This is because MAC addresses are relatively
easy to obtain, as they are sent over the Wi-Fi with each packet
going to and from the device. This is necessary as the MAC
address is used to ensure that each packet gets to the right device.

280
Ce Te—*e Cyber-security

2 Question All an attacker has to do is monitor the Wi-Fi traffic, examine a


packet to find the MAC address of an allowed device and then
5 Briefly describe the term change their device’s MAC address to the allowed MAC address,
content filtering. although this not an easy thing to do whereas changing an IP
address is much simpler.

CAPTCHA
CAPTCHA stands for Completely Automated Public Turing test
to tell Computers and Humans Apart. It is a program used by
some websites to provide further protection for a user’s password
by verifying that user input is not computer generated. There are
now a wide number of different CAPTCHA systems using images,
numbers and even simple calculations but basically the idea is
to create something humans can read but that current computer
programs can’t. In this example distorted text is used.

Email verification
Figure 35.1 Human readable
Email confirmation and CAPTCHA are used to solve different
(distorted) security text
problems. Email verification is used to check that users are
using their real email address in the registration process. Email
verification is where an email is sent to the user’s email address and
they have to click on a link in it to confirm that the email address
is theirs. Email confirmation also protects from identity theft. For
example, the user cannot register a government email address and
pretend they are someone from the Cabinet Office as they will not
be able to click on the confirmation link sent by the email because
they are not the owner of this address. Email confirmation allows
the system to link each user to a specific email address.
Email verification proves that the email address is valid but
automating a response to an email is fairly easy, and thus cannot
be taken as assurance that the client is not a bot, so email
confirmation doesn’t protect from the bots (as CAPTCHA does).
e Question
Mobile phone verification
6 Briefly describe the term
With mobile phone verification you ask the user to enter their mobile
social engineering.
phone number, then you send them an SMS (text message) with a
code and they must enter this code into a web form on the website.

Removable media
For many years one of the most common ways to deliver a virus
has been through removable media. This has become even more
of an issue now with AutoRun. This is similar to AutoPlay, which
automatically starts playing video when you put a DVD or Blue-
ray disk into a media player. But unlike AutoPlay, which only plays
the media file, AutoRun starts automatically and can then start
installers of malware. The best option is not to use any removable
media unless it has been verified as being clean, but it is also
possible to disable AutoRun.
35 Social engineering and cyber-security

Penetration tests
A penetration test is an authorised attempt to evaluate the security
Penetration testing is the
of an IT infrastructure by safely attempting to hack into the system
process of attempting to gain
to find vulnerabilities. These tests also explore how effective
access to resources without
defence systems are. Penetration tests can be performed using
knowledge of usernames,
manual or automated technologies.
passwords and other normal
means of access. They are carried out on a wide range of systems including servers,
web applications, wireless networks, network devices, mobile
devices and other potential points of risk. Once vulnerabilities
2 Question
have been successfully exploited on a particular system, testers
7 Briefly describe the term unit may attempt to achieve the same unauthorised access.
testing.
There are two types of penetration test called black-box testing
and white-box testing.

White-box penetration tests vs black-box


penetration tests
White-box testing is a penetration test that uses the detailed insider
J Key points knowledge of the target system to improve the tests. In application
A The aim of a white-box penetration tests the source code of the application is usually
penetration test is to simulate provided along with design information. The test can also make
a malicious insider who has use of interviews with developers. In infrastructure penetration
knowledge of and possibly tests all the network details are provided. The goal of a white-box
basic credentials for the target penetration test is to provide the tester with as much information
system. as possible.
A The aim of a black-box White-box penetration testing has some clear benefits:
penetration test is to simulate @ It leads to deep and more detailed testing.
an external hacking or cyber ® It requires less testing time.
warfare attack.
@ It also tests things like quality of code, application design, etc.
But there are also some disadvantages as it is not a realistic attack,
as the penetration tester is not in the same position as a non-
Computers are not secured informed attacker.
against phishing; each user must Unlike white-box penetration tests, a black-box penetration test
rely on good judgement when requires no previous information and takes the approach of an
revealing sensitive data to others uninformed attacker.
without proper verification of who
they are. A benefit of this type of attack is that it simulates a realistic
scenario.
A disadvantage of a black-box penetration test is that tests can
? Question
take much longer to perform.
8 Briefly describe the term
Some areas of the infrastructure and software may remain
black-box testing.
completely untested.
Cyber-security

Chapter review

In this chapter we have explored social engineering.


Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand the importance of network security
® Be able to use appropriate validation and authentication techniques
(access control, physical security and firewalls) (More on this topic in
Chapter 34)
® Understand security issues associated with the ‘cloud’ and other
contemporary storage (More on this topic in Chapters 34, 36)
® Understand different forms of cyberattack (based on technical
weaknesses and behaviour) including social engineering (phishing,
shoulder surfing), unpatched software, USB devices, digital devices
and eavesdropping (More on this topic in Chapter 34)
® Understand methods of identifying vulnerabilities including
penetration testing, ethical hacking, commercial analysis tools and
review of network and user policies (More on this topic in Chapter 34)
® Understand how to protect software systems from cyberattacks,
including considerations at the design stage, audit trails, securing
operating systems, code reviews to remove code vulnerabilities in
programming languages and bad programming practices, modular
testing and effective network security provision (More on this topic in
Chapter 34).
36 Ethics and the law

‘ Specification references understand the ethical impact of using


You should: technology (privacy, inclusion, professionalism)
5.2.2 understand security issues associated with the on society
‘cloud’ and other contemporary storage 6.1.3 understand the legal impact of using
™ 6.1.1 understand the environmental impact of technology (intellectual property, patents,
technology (health, energy use, resources) licensing, open source and proprietary
on society software, cyber-security) on society

Much is written about using computers safely, but alongside safety


are the law and ethics of using computers, particularly when you
start to learn to code.

i Ethical use
If you access, view or collect confidential material and/or personal
information, it is your responsibility to maintain confidentiality.
Do not share this information with unauthorised individuals. But
ethics and the law are not the same thing.

BS Sere es : See eee ee re ee ee

Ethics are a guideline to computer Consists of rules to control computer


users and are not legally users that are legally enforceable.
enforceable.

Computer users are free to follow or Computers users must follow the
ignore a code of ethics. regulations and law for the country they
live in.

Ethical rules are universal and can Whether or not a crime has been
be applied anywhere, all over the committed depends on the country or
world. state where it took place. There are many
examples of laws in one country allowing
things that are illegal in others. This is a
4 Computer ethics are moral big issue with the internet.
guidelines that govern the use
Ethics aim to create ethical Laws aim to prevent misuse of computers.
of computers and information computers users.
systems.
oe a. If you don’t follow ethical rules you Not obeying laws is referred to as a crime.
4 Computer addiction is a are deemed to be immoral.
growing health problem that
occurs when the Con Ethics relates to the rules and standards governing the conduct of
consumes someones entire an individual with others. As technology and computers became
social life. more and more a part of our everyday lives, the definition of

284
Ethical use

computer ethics evolved. Computer ethics is concerned with


standards of conduct as they relate to computers.
For the examination you will need
to understand the legal impact Basically, computer ethics are about knowing the difference
of using technology (intellectual between what is ethical and what is unethical. It may be possible
property, patents, licensing, open to access someone’s personal information on a computer system
source and proprietary software, but this action is unethical. It is also easy to copy and duplicate
cyber-security) on society. digital content but ethics suggest that it is wrong to do so without
gaining the author’s approval.

The digital divide


Over the past few years society’s dependence on computer technology
has increased. The ability to communicate via email and access
the internet has become an essential part of everyday life. But
there are many people in the world who do not have access to
the internet and this has led to a disparity called the digital
divide.
This gap is of growing concern. Rural communities, low-income
1 How would your life be families, people with disabilities, and those in some areas of the
different today, if there were wider world do not have the same advantages as more privileged
no computer systems? households and communities. To many people this is a major
ethical issue.
Online gambling is becoming a real problem in the Western world
and leads to many ethical questions. The internet opens up
opportunities to lose large amounts of money while staying in the
comfort of one’s own home.
Cyber-squatting is when someone purchases a domain knowing
that it will be useful to a well-known company. They then sit on
the name until they can sell it at a higher price. Often companies
with well-known trademarks are unable to purchase their domain
name as it is owned by a cyber-squatter. Common names are also
subject to cyber-squatting, raising questions about how ethical
this practice is.
People often write important messages in their emails. But
increasingly email security has been compromised. Email was
once only available to the computer literate. Today email has
become a standard means of communication for millions of
people. Many of these people wrongly assume that only they and
the recipient of their email will be able to read the communication.
This illusion of privacy is a big problem of concern. Email travels
from server to server and can be read more easily by a hacker with
some technical knowledge. Recently email privacy has become a
big ethical debate about individual rights, corporate rights and
the use of technology.
Facial recognition is no longer in the realm of science fiction; it is
now used in train stations, airports and number of public places.
» 36 Ethics and the law

, It has even found its way into social media sites. But many people
are concerned about the lack of privacy, and how captured data
could be used by unscrupulous companies and individuals.

] Key points Legal use


A The IP address is a number You will know all the warnings against illegally downloading
that identifies a device ona copyrighted material like movies, music, software and video games.
TCP/IP network. What you may not realise is that when you begin downloading
A Copyright is exclusive rights illegal copies of this type of material, you can also inadvertently
given to authors and artists begin sharing these files with others. The IP address of the
to duplicate, publish, and sell computer used to download materials is easy to track, making it
their materials. possible to identify who the responsible individuals may be.
Getting caught is not the only reason to stay away from illegal
downloading; there is also a chance these files contain viruses,
trojans or other malicious files that can infect your computer.
Cyber-ethics Is a system of moral standards or moral values that can
be used as guidelines for all computer users to follow.

Code of ethics Guidelines that help determine whether a specific


computer user’s actions are ethical or unethical.

pyc see rss Anything created by inventors, authors and artists that
is their own ideas or work becomes known as their
intellectual property.

Copyright Protects the form of expressions of ideas, not the ideas


themselves.

Refers to the right of all individuals and companies to


deny or restrict the collection and use of information
Software theft occurs when about them.
someone: Cyber-crime Is any illegal act involving computers or computational
1 steals software media devices, usually over the internet or other networks.

2 i j ; ;
intentionally erases programs Is any law relating to protecting the internet and other
3 illegally copies a program, or online communication technologies.
4 illegally registers and/or
activates a program. The law relates to copyright and intellectual property.
@ Intellectual property is about creations of the intellect (hence,
the name): inventions, artistic works, names, images and
designs. Intellectual property also relates to industrial property
such as inventions, trademarks etc. The word property means a
Product activation is a process
possession, something in which the owner has legal rights.
that attempts to prevent software
Unlike intellectual property, copyright law only protects the
piracy by requiring users to
form of expressions of ideas, not the ideas themselves. So the
provide a software product’s
copyright laws do not protect ideas or systems only how they
25-character identification
are expressed. This means that nothing in the copyright laws
number in order to receive an
prevents others from developing another work based on the
installation identification number.
same idea.

286
Ieee
Legal use

Software cracking
Software cracking is the modification of software to remove or
disable features which are considered undesirable by the person
cracking the software. Crackers often illegally remove or overwrite
copy protection features, nag screens and adware.
The term crack refers to the means of achieving software cracking.
This can include stolen serial numbers or a tool that performs the
act of cracking such as keygens used to generate an activation
key, patching which involves adding a patch so that the software
cannot conduct security checks and loaders to load the software
without these security checks.
A cracker (also known as a black hat hacker) is an individual who
breaches or bypasses security to gain access to software without
paying royalties. A cracker is anyone who reverse engineers
software or illegally modifies software.

2 Questions
Software and source code theft
Computer source code is the most important asset of a software
1 Briefly explain the term
company. The term source code refers to the programming
copyright.
instructions that are compiled into the executable files that are
2 Briefly describe the term sold by the software development company.
intellectual property.
Most people would agree that a company or individual should
be allowed to receive a return from the investment needed to
develop quality software. To allow for this, software developers
need a level of protection for the ownership over the software
they produce. In legal terms this is in the form of intellectual
property law which comes under the principles of copyright and
patent law.
The rights of the developer are formally agreed in legal terms ina
software licence. When you buy a piece of software you normally
have to agree to a software licence by clicking ‘I Agree’ or by
2 Use this book and other opening the package. This is a legal agreement between the
sources such as the internet owner and user of the software. It gives the user permission to
to research the legal impact use the software but does not transfer ownership rights to the
of using technology. intellectual property. Any act of reverse engineering or cracking
the software is illegal.

Open source and proprietary software


Open source software is software where you can see and access
Source code is the original
the source code. You can also modify and improve it. The internet
program code of software; it is
is built on source technologies including the Linux operating
the part most computer users
system and the Apache web server application. Applications
don’t ever see.
software such as Star Office is also open source software.
36 Ethics and
eee a the law

Whilst open source software is free, free software is not a good


term for it, as the purpose of the open source movement 1s to
allow for open development and improvement. Apple provides
free upgrades for its operating system but it is not open source.
Proprietary software is software that is owned by a company or
the programmer. Usually the owner is the company that developed
the code for the software. Proprietary software is restricted
in its use, and its source code is almost always kept secret. The
restrictions include aspects such as that the software cannot be
copied or distributed without complying with stated licensing
agreements.

Plagiarism
Whilst plagiarism is not a recent problem, the internet has led to an
explosion in the copying of other people’s work. In schools, colleges
and universities tutors now have a real problem in identifying and
discriminating between original work and plagiarised work.
Plagiarism is also theft.

Personal versus private


Laws on data use relate to some types of personal information. A
distinction is made between personal data and sensitive personal
data. A leak of sensitive personal data is treated more seriously in
the law. Sensitive personal data includes data relating to a person’s
race, sexuality, health, criminal record or affiliations (such as
Information privacy is the right political persuasion or trade union membership). A person’s name
of individuals and companies to
or phone number may be personal, but in the law it is not covered
deny or restrict the collection as sensitive personal data. Compiling all the names and phone
and use of information about numbers of residents of a town in a single book is not viewed as
them. particularly threatening by most people, whereas compiling lists
of socialists would be viewed as personal.
Under EU law, personal data can be collected only under strict
conditions and for a legitimate purpose. The main law in the
EU protecting users is the Data Protection Directive. In the
US, the attitude towards data protection is governed mainly by
market forces, so there is no all-encompassing law regulating
the collection and processing of personal data. Instead, data
protection is regulated separately by each state.

Security versus privacy


It is now common practice for companies such as Google to collect
For the examination you wiil need personal information such as data about the services you use and
to understand the ethical impact how you use them. They keep server logs of location information,
of using technology (privacy, data about the software you use, storage data and other identifying
inclusion, professionalism) on information. Giving them access to this data enables us to better
society.
exploit the internet. Doctors can track public health risks using
Mf
f #
The environmental impacts of technology
ta A

this type of data. As soon as we voluntarily share it with others,


the information stops being truly private.
There is a big debate about what is private and what is not, anda
3 Use this book and other similar debate about how secure individuals should be allowed to
sources such as the internet make their data. If it is possible to make data totally secure these
to research the ethical impact secure systems would also be used by people such as terrorists, so
of using technology. governments want to limit security levels below what they can
break into!

§ The environmental impacts of technology


When you use computer electronics, you are participating in
one phase of that product’s life. Before the product makes it to
you, raw materials (resources) are taken (extracted) from the
environment; these are then processed and manufactured into a
product. The product is then packaged and transported, again
using valuable resources. The next stage is the use of the product
and the energy needed for this use; the final stage is how you
dispose of the device when you have finished with it.
_ Resources
For the examination you will need
to understand the environmental
impact of technology (health, End of life
energy use, resources) on society.

Material
processing

Use

Product
manufacturing
Distribution
Figure 36.1 The technology cycle

Resources and material processing


A computing device’s life starts in an open pit mine is South
America. It will then cross nearly all continents from the time its
raw materials are collected to the time the product is manufactured.
4 Use this book and other
Technology devices often use copper from Chile processed in
sources such as the internet
Sweden, Tantalum from Australia or Congo, beryllium from
to research the environmental
Kazakhstan, platinum from South Africa and a semiconductor
impacts of technology.
chip built by workers in Ireland or Israel.
ra
36 Ethics and the law

Energy use
You may think that your computer-based technology uses the
most energy when you use it and that by switching devices off
and using power-saving modes you can reduce this. Whilst this
is true, eighty-one percent of the energy a computer device uses
is used when it is made. It actually takes more energy to create
the device than it takes to run it for its entire working lifetime.
This makes computer devices different to all other household
appliances, most of which use the most energy during their
lifetime. So however good the user is at turning off their systems,
they will be involved in using large amounts of energy just owning
the device, especially if they are updating the hardware regularly.

End-of-life management
Consumer electronics are increasingly treated as disposable
items. We throw away televisions, computers and mobile phones.
Globally we throw away over 40 million metric tons of electronic
waste (e-waste) every year, and only about 13% of that weight
is recycled. Most e-waste recycling occurs in China, India,
Pakistan, Vietnam and the Philippines where they shred, burn,
and dismantle the products in ‘backyards’. Emissions from these
activities damage human health and the environment due to
exposure to toxic metals, such as lead.
Manufacturers are starting to take responsibility for changing
the way they manufacture electronics. This involves using fewer
natural resources during production. Users can make a difference
too by buying products that use recycled materials or that can
be updated rather than thrown away. Donating or recycling used
electronics helps too.
The disposal of computer devices is unlike most other consumer
goods because they are usually disposed of before they become
useless. The main reason for purchasing new computer devices is
not to replace a non-functioning system, but to keep up to date
with rapidly changing technologies. One way of reducing the
amount of computer waste would be to reuse systems that may be
out of date, but remain fully functional.

Waste disposal
Disposal of IT equipment comes under the WEEE directive (Waste
Electrical and Electronic Equipment).
The main objectives of the WEEE directive are:
® to increase reuse, recycling and other forms of recovery, leading
to a reduction in the amount of waste going to landfill or
Incineration
© to improve the environmental performance of all operators
involved in the life cycle of electrical and electronic equipment
The environmental impacts of technology ie
‘s
Ae

® to set criteria for the collection, treatment, recycling and ¥


recovery of WEEE ‘
® to make producers responsible for financing most of these
activities — private householders are to be able to return WEEE
without charge.

What can individuals do?


Everyone has a responsibility to reduce their individual carbon
footprint. Completely powering off computing devices when not
in use is good for your devices and for the planet.
@ Change power settings so that devices power down after 15 minutes
of inactivity.
® Consider end-of-life management for all computing devices.
® Dim the screens on computing devices as this saves power.
®@ Disable screensavers as they don’t actually conserve energy,
some even use more energy than normal use.
® Do not have too many programs running at the same time and
change the system settings to reduce programs that auto-start.
® Shut down all devices at the end of the working day.
e@ Turn off printers, scanners and other peripherals when not
using them.
@ Unplug all computing devices when they are fully charged,
e Question otherwise they will be using power to keep topping themselves
3 Compare the terms legal and
up throughout the day.
ethical use related to computer
® Unplugging chargers when they’re not in use saves on power
software.
consumption.
@ Use hibernate mode instead of sleep mode.

Chapter review

In this chapter we have explored ethical and legal issues relating to


computer use.
Remember, before tackling any computer science task or examination
question on this topic you must:
® Understand the environmental impact of technology (health, energy
use, resources) on society
® Understand the ethical impact of using technology (privacy, inclusion,
professionalism) on society
® Understand the legal impact of using technology (intellectual property,
patents, licensing, open source and proprietary software, cyber-
security) on society (More on this topic in Chapter 31)
® Understand security issues associated with the ‘cloud’ and other
contemporary storage (More on this topic in Chapter 34).
+f Specification reference

You should:
4.2.6 understand the need for embedded systems and their functions

An embedded system is a system that has computer hardware with


software embedded in it as one of its components. As embedded
: J Key points systems are small, low cost and simple they have become very
popular and are now indispensable in our lives. They are found
4 Embedded systems: everywhere, from kitchens to hospitals.
A frequently have more limited
Consider this list of embedded systems that are being used every
resources than PCs
day.
©) 4 usually focus on dedicated ® Aircraft (flight landing gear systems and many other systems
tasks, but PCs are general-
from sensors to controllers)
purpose computers
@ All gym equipment from treadmills to cycling equipment
A uSually require minimal human e ATMs
intervention. ® Car systems (pressure monitoring systems, airbags, power windows
|. Vi

and GPS systems)
@ Microwave ovens
@ Military applications
® Mobile phones
® Pacemakers
®@ Robots
For the examination you will ® Toys
need to understand the need @ T'V remotes (and all other remote control devices)
for embedded systems and their ® Vending machines
functions. @ Washing machines (including dishwashers and drying machines)
® and many more...
Every embedded system consists of custom-built hardware built
around a micro-controller. The hardware contains memory chips
onto which the software is loaded. The software residing on the
memory chip is called ‘firmware’. Not all embedded systems have an
operating system. Small appliances such as toys and remote control
units do not need an operating system, only the software specific
to that application.

292
Programming micro-controllers =

§ Programming micro-controllers
Often you can program a micro-controller using the BASIC
programming language.
Standard basic instructions include:
@ for...next (the normal iteration (looping) statement)
® go sub (go to a subroutine)
® goto (goto a label in the program)
@ if...then (the normal if/then decision statement)
@ end (end the program and sleep)
BASIC logic statements include:
@® =

@ <>
@<
@ <=
®>
® >=
@ AND
@ OR

Figure 37.1 A Raspberry Pi computer

The Raspberry Pi can be used as a micro-controller and you may


have come across one. It is only the size of a credit card but it is in
fact a fully functioning single-board computer. It was developed
in the UK by the Raspberry Pi Foundation to help promote the
teaching of computer science in schools.
, 37 Embedded systems


Mechanical System
e system model e dynamic response

Input signal
conditioning
and interfacing

e discrete circuits
e amplifiers
e filters
e A/D, D/D

Graphical Output signal — Digital control—


displays conditioning — architectures oes
and interfacing Ws logic circuits Ris ce
e LEDs
¢ digital displays ° D/A. D/D ° microcontroller &.,
e LCD e amplifiers ia
° CRT e PWM ee \
® power transistors
¢ power op amps —¢ logic
and arithmetic.
Paonia
-e a
“+communica (ohh,

Figure 37.2 Architecture of actuators and sensors

2 Questions § Actuators and sensors


1 What is a programming Micro-processors use actuators and sensors to function.
language?
An actuator is used to move or control the output. It is a type
2 Briefly describe the term of motor for moving or controlling a mechanism or system. To
embedded system. operate it needs a source of energy, usually in the form of an
electric current, hydraulic fluid pressure or pneumatic pressure.
The actuator converts that energy into motion.
! Key points
A sensor is a converter that measures a physical quantity and
A The term analogue refers to converts it into a signal. The micro-controller uses this signal to
continuously changing values make a decision. For example, a mercury thermometer converts
without steps. the measured temperature into expansion and contraction of a
A A sensor is a device that can liquid which indicates the temperature via markings on the tube.
detect physical conditions Of course this is not digital but analogue and micro-controllers
such as temperature, weight, need digital signals. For micro-controllers a thermocouple
light, sound, etc. converts temperature to an output voltage which can be read.

294
nnn Wearable technology
NORSIRUROINS

A sensor is therefore a device which responds to an input quantity


by generating either an analogue output, which would need
converting into a digital signal by an ADC, or a digital output in
the form of an electrical or optical signal.
Modern mobile phones have a wide variety of sensors that
automate many of our daily tasks or make them easier.
@ An accelerometer measures linear acceleration of movement.
An accelerometer sensor will measure the directional movement
of a device but cannot measure lateral orientation or tilt; for this
a gyroscope sensor is needed.
@ Gyroscope (gyro) sensors are used in mobile phones to
detect the orientation of the phone. A gyro sensor measures
the angular rotational velocity. As such the sensor tracks the
phone’s rotation or twist.
e The digital compass sensor in a mobile is usually based on
a sensor called a magnetometer. This sensor provides mobile
phones with a simple orientation in relation to the Earth’s
magnetic field. This way the phone always knows which way is
North and can auto rotate digital maps.
e A barometer sensor measures altitude. The barometer is there
to help the phone’s GPS chip lock onto GPS signals by delivering
altitude data.

§|Wearable technology
Wearable technology, also called wearables, is clothing and
accessories incorporating computer and advanced electronic
embedded technologies. Wearables contain sensors, and also
often make use of web connection and Bluetooth networking to
connect wirelessly to other devices and the connected world. They
also use their inbuilt sensors to connect to your body and can be
used to help to achieve goals such as fitness, weight loss or better
organisation.
Most of the early generation wearables were wrist worn, but now
an increasing number are clipped to the body, hung around the
neck, built into things such as glasses or implanted into the body.
The main types of wearable include:
@ Smartwatches are wrist-worn devices that connect to things
like mobile phones and PDAs to tell the user about:
— call notifications, email and social media messages
— fitness tracking by keeping track of the activity the user
does such as the number of steps taken or distance travelled,
calorie count etc.
— GPS to track where the user is, sometimes with maps or
breadcrumb trails to help them find their way.
’ 37 Embedded systems
a

f © Head-mounted displays are objects such as glasses that are


designed to either block out the rest of the world or add to it via
virtual reality in order to:
— present a computer-generated virtual reality
— act as an overlay of information.
@ Smart clothing where the sensors are built into things you can
weal.
e Smart jewellery with sensors built into things like rings.
® Implantables which are wearables that you have no choice
but to carry with you wherever you go as they are surgically
attached under your skin. They provide things such as:
— insulin pumps
— heart stimulation
— contraceptive devices
— validation for door entry and device control.

Chapter review

In this chapter we have explored embedded systems.


We also explored micro-controllers.
Remember, before tackling any computer science task or examination
question on this topic you must:
™® Understand and be able to explain and apply the need for embedded
systems and their functions (More on this topic in Chapter 33).
38 Project

You will be set a task in the form of a single project which can be
undertaken in a period totalling 20 hours. When completing the
task, you will be expected to work independently and produce a
unique piece of work that you have designed, implemented, tested
and evaluated.
at Ne at aaa

Your project must use one programming language from the following:
@ Python
® Java
@ Pascal/ObjectPascal
® VisualBasic. NET
@ C-derived (C, C++, C#).

If a data file is required to complete the project, this will be provided


by your teacher.

§ Analysis
You will be expected to clearly identify the requirements of the
problem including what the proposed solution will need to do
to meet these requirements. You should write an introduction to
the problem, in prose. Remember that this must demonstrate a
comprehensive understanding of abstraction and decomposition.
Decomposition requires you to make choices by breaking the
given problem down into sub-problems which you will later
design and implement.
You will need to:
@ decompose the problem into manageable sub-problems, with
an explanation of each.
Your analysis must include an explanation for any judgements
you make on the selection of the most appropriate sub-problems.
Remember that the examiner needs to see comprehensive links to
evidence so that your lines of reasoning are well supported, clear
and concise. eth
ati
I
fe
et

For your decomposition you can use:


@® prose or
@ bulleted lists.

298
y} a
“.
>
bins e
«ata-
TOON Design

But remember to add an explanation of the reasons why the


decomposition submitted is the most appropriate to meet
requirements.

For the Analysis stage, your report will include:


@ a short introduction to the problem set
@ a list of the requirements of the problem that you intend to
program
@ full decomposition of the problem into sub-problems, including
— ashort description of what each of your sub-problems will do
— ashort explanation of the reasoning behind the decomposition
you have submitted.

§ Design
You will be expected to design a solution to a problem including:
@ full decomposition into subprograms with clear abstraction
@ planning data structures
@ designing the algorithms
@ designing an appropriate modular structure for the solution
@ showing that all the requirements of the problem are fully
addressed
@ designing the user interface.
Your algorithms must be well designed and there should be no
errors in logic and in the use of programming constructs. The aim
is to produce an overall solution which is fully functional.
You will need to articulate your designs in a manner appropriate
to the task and with sufficient clarity for a third-party examiner
to understand. You must show how all the key aspects of your
solution are structured. You should also show that you have
clearly identified the requirements of the problem and made valid
judgements for decomposing the problem into sub-problems that
show good application of key concepts to the problem.
You can communicate your design using many of the tools covered
in this book including:
@ prose and diagrams
@ flowcharts
@ pseudo-code
@ descriptions of algorithms
@ data structures
@ text file/database structures.
You may also want to provide screen shots of actual screens.
4 You will need to show:
@ a comprehensive design which could be used as the basis of an
effective implementation of a complete solution
@ a good understanding of variables, data types and structures
@ all of the main blocks of the proposed solution including data
validation
@ that your design choices are justified with reference to user
requirements.
Your algorithm(s) should:
e show detailed decomposition into sub-problems and how they
link together (if appropriate)
e demonstrate clear abstraction (for example by including
parameterisation, links between components)
@ include inputs, processes and outputs
® use all the basic programming constructs in this textbook:
sequence, selection and iteration.
Remember to include:
@ the algorithm(s)
® comments on any refinements to the design identified during
implementation, with reasons.

At this stage you must also devise a comprehensive test strategy.


Your test strategy for the solution should be based on meeting
the requirements of the problem. The test strategy should explain
the approaches that you will use when testing your program.
Remember, it must cover the requirements of the problem and
contain a very clear explanation about the approaches for planning
the testing of the program.
The test plan and test strategy must include:
@ a clear description of the purpose of each test
® coverage of normal, erroneous and boundary data
@ expected outcomes with appropriate reference to your test data.
An example of a table that could be used for the initial test plan
is shown below.

Test no Purpose of | Test data Expected Action


the test result needed/
comments

For the Test stage, your report will include:


@ the test strategy
® the initial test plan, using the headings shown, with all first
four columns completed. It must be labelled ‘Initial Test Plan’.

300
bad

Implementation $

§ Implementation | |
At this stage you will implement your designs. But remember that
you will be testing and refining your code as you go, and this
must also be clearly shown. Your program must fully address the
requirements of the problem and should include:
@ a full solution with no logic errors, showing your effective use
of debugging skills
® subprograms, programming constructs, data validation, showing
that your choice of data types and structures lead to an overall
program which is fully functional
® evidence that your program has been fully decomposed into
subprograms and computing techniques are used to make the
program unequivocally clear and easy to understand.
You will be expected to implement your designs including:
® implementing the data structures and code so that a computer
can understand your solution.
Again drawing upon the contents of the book you will need to
use your coding skills to work at an appropriate level. You will
be expected to provide program listing(s) that demonstrate your
technical skill. You will also be expected to use appropriate
annotation. Your code should be well structured. It may be that
you need to amend your original design solution during this stage
and these refinements should be implemented and documented
as additions to the design and in the program code by using
comments as descriptors.

A refinement can include many things, for example a more efficient


way of programming a sub-problem or an additional component
that has not been previously considered but which will add to the
functionality of the solution.
Again remember that you are presenting your work to a third
party.

You should:
@ include explanations of particularly difficult-to-understand
sections of your code
@ provide a carefully divided presentation of the code
® provide appropriately labelled sections, to make navigation as
easy as possible for a third party reading the code listings
@ show how your program fully addresses all the requirements of
the problem set
@ show that your program runs with few or no logic errors
@ show how your program has been fully decomposed into
subprograms.
38 Project
] e es

Your code needs to:


© be applied sufficiently to demonstrate proficiency
@ be annotated to show a successful solution to the problem that
utilises exception handling, data use validation and subroutine
interfaces as appropriate
@ use meaningfully named variables (local and/or global)
® be appropriately structured for ease of maintenance.
When developing your code you should:
use subroutines complete with appropriate interfaces
use effective and cohesive subroutines
demonstrate good exception handling
incorporate self-documenting code
demonstrate modularisation of your code
show that you can use appropriate local variables
demonstrate minimal use of global variables
show the appropriate use of data validation
show the appropriate use of constants
demonstrate a consistent style throughout
always use meaningful identifier names
ensure that you use appropriate indentation
add annotation wherever it is required to help someone understand
your solution.
For the Implementation stage, your report will include:
® a copy of the program code; any refinements should be noted as
comments in the final program
® screenshots demonstrating effective use of debugging skills to
COLLeECr errors
® if errors have been identified, show how the program was
corrected to overcome them or provide a clear description of
how to fix the errors in the test table
@a description of how refinements were identified and
implemented in the design or implementation stage and how
they improve the functionality and robustness of the solution
@ an evaluation that critically reviews how successfully the program
meets the specified requirements.

§ Testing, refining and evaluation


You will be expected to test your designs against the original task
requirements including:
® testing your solution for errors
® choosing and using test data covering normal (typical),
boundary (extreme) and erroneous data.
You will need to develop a comprehensive test plan that includes:
© test data that enables the testing of all requirements
® coverage of normal, erroneous and boundary data types
@ the expected outcomes for each test.

302
eS. Evaluation/refining

Your test plan should show how each component will be tested to
meet the requirements of the problem.
Testing is asking the questions ‘Does the solution work?’ and
‘How well does it work?’ You will need to plan a series of tests to
show that the different sections and elements within your solution
work as intended, so that your solution is successful. It is best to
test your designs both after the system has been fully coded and
during the coding process. When carrying out tests it is important
that normal (typical), boundary (extreme) and erroneous data as
covered in this book should be used as appropriate.
You will need to present your results in a structured way, for
example in tabular form, showing clear evidence of testing. You
should also explain the reasons for the tests carried out alongside
the evidence for them.
This could take the form of:
® the test performed
® its purpose if not self-evident
® the test data
@ the expected test outcome
® the actual outcome with a sample of the evidence.
You will need to demonstrate that:
® a thorough representative range of tests has been planned and
carried out
® all the requirements of the problem have been achieved
® your test data includes normal (typical), boundary (extreme)
and erroneous data and shows that you have included:
— a comprehensive application of programming concepts and
principles, including debugging
— constructs, data validation and a choice of data types and
structures and how these have led to an overall solution
which is fully functional
® you have produced a test plan which is clear and unambiguous.

Evaluation/refining
You should evaluate your solution to the task through the design
process including:
® refining your solution as a result of testing
® assessing how well the solution meets the requirements of the
problem
® suggesting how your solution could be improved if the problem
were to be revisited.
Evaluation is considered to be answering the question ‘How well
does the solution work, and how could it be better?’
»PSO 38 Project
Te EEE eee

r You will need to consider and assess how well your solution meets
the requirements of the problem and how your solution could be
improved.
Remember to show:
@ how well the solution meets all the requirements of the problem
® if errors have been identified, how your program has been
corrected to overcome these errors
@ that all components and the requirements have been fully
tested using normal, boundary and erroneous data
@ that your testing includes how any subprograms interact
@ how you have considered the efficiency of execution and
robustness have been carried out
@ how you have made improvements to the solution
@ how any refinements have been identified, implemented and
tested.
Tests for any refinements completed in the design and/or
implementation stages should be added to the end of the test
plan and carried out. The evaluation must include a thorough and
critical evaluation of your program stating how successfully the
program meets each of the original requirements and the reason
for adding refinements to the final solution.
For the testing stage, your report will include:
@ the updated and complete test plan (labelled ‘Final Test Plan’)
@ the evaluation.

§ Overall quality of your report


You must show real quality in your completed report. This report
must include all the sections mentioned above.
You should try to use a range of technical terms but make sure they
are used accurately. There should also be a consistent approach to
the structure and layout of your report which enables easy cross-
referencing between sections and between different parts of the
solution.

304
Key points index

| Term Key point = ‘This key point is referred


toonpages
Abstraction Abstraction is the process of removing unnecessary detail from a problem. 2, 3-5, 93, 125-6,
4127-9
Actuator An actuator is used to move or contro! the output. It is a type of motor for 229, 294-5
moving or controlling a mechanism or system. To operate it needs a source
of energy, usually in the form of an electric current, hydraulic fluid pressure or
pneumatic pressure. The actuator converts that energy into motion.
Adware Adware is software that displays advertising and gathers information on PAS}, PUT
browsing habits.
Algorithm An algorithm is simply a set of steps that defines how a task is performed. It Shy hy AAA OS) Gi
iS a sequence of steps and decisions to solve a problem. 35-7, 108-9, 117, 198,
202-11, 299-300
Alphanumeric Alphanumeric refers to alphabetic and numeric characters, and is used to 49,149, 272
describe the collection of Latin letters and Arabic numeric digits.
ALU The arithmetic logic unit (ALU) performs all the arithmetic and logical 2205221; 23,239
operations within the CPU.
Analogue The term analogue refers to continuously changing values without steps. 149, 153-6,
180, 260,
294-5
AND A logic gate that outputs 1 if both inputs are 1. 32-9,
34, 7D, (1,220,
293
Anti-virus software Anti-virus software is an application designed to detect and remove malware 269, 274-5, 277-8
and viruses.
Application Application software consists of programs that perform specific tasks, e.g. 239, 245-7
software web browsers, word processing software.
Architecture The internal logical structure and organisation of the computer hardware is 426, 216-22
called the computer architecture.
Argument An argument is any piece of data that is passed into a function when the 91°92, 93,100; 196
function is called.
Array An array is a data structure made up of a series of variables all of the same 45-6, 164, 165, 166-71,
type, grouped under one identifier. Elements are accessed using an index. An 207
array is a collection of data items that are given a single name and saved on
a computer system in a sequential form. The data items are distinguished by
numbers.
ASCII ASCII (American Standard Code for Information Interchange) is a 7-bit system 145, 146-7, 149, 184,
to code the character set on a computer.
Assembler An assembler is a program that translates assembly language programs 130-1, 246,
into machine code. Assembler directives are instructions to the translating
programs.
Assembly language Assembly language is a low-level programming language. It uses mnemonic 126-7, 13-1, 132, 246,
codes and labels to represent machine-level code. Each instruction
corresponds to just one machine operation.
Authentication Authentication is any process by which someone is verified to actually be who 201,.242; 270; 271
they claim to be.

».
Key points
Rey
Ck OTS index
——_—

) Authorisation Authorisation is any process in which someone is allowed, after being


authenticated, to access data or information that they requested.
270

Backdoor The term backdoor refers to an undocumented, unauthorised method 271


of gaining remote access to a system, bypassing normal authentication
methods.
Binary Binary is a system of numbers using only two digits O and 1 (also called the 16; Ws ei oS.
base-2 system), unlike the decimal (or denary) system in everyday use that 134-42, 143, 148, 150,
uses O to 9 (base-10). All data that is stored in a computer is converted to AUSSS).
binary. The word binary comes from ‘bi-’ meaning two. We see ‘bi-’ in words
such as ‘bicycle’ (two wheels) or ‘binocular’ (two eyes).
When we say a binary number, we pronounce each digit (e.g. the binary
number ‘101’ is said as ‘one zero one’). This is so that we don’t get
confused with the decimal number.
Binary shifts Binary shifts can be used to perform simple multiplication/division by powers 141-42
of 2.
Bit A single binary digit (like ‘O’ or ‘1’) is called a ‘bit’. For example 11010 is five 136
bits long. The word bit is made up from the words ‘binary digit’.
Bitmap A bitmap is a graphical image represented as an array of brightness values. 150-3, 182, 183
For example, in a black and white image O represents white and 1 represents
black.
Black-box testing The aim of a black-box penetration test is to simulate an external hacking or 282
cyber warfare attack.
Bluesnarfing Bluesnarfing is unauthorised access of information using a Bluetooth device ATCA.
in discovery mode.
Bluetooth Bluetooth is a wireless protocol/service allowing enabled devices to 2 Ak, BY
communicate and transfer data.
Boolean A Boolean expression is an expression that when evaluated is either true or 17, 33, 34, 49, 75-81,
false. alal(Sy, al(Syal
Bot A bot is a type of program that runs automatically, performing tasks directed PY
by an attacker.
Boundary data Boundary data is data that is at the extreme limit of valid data and is often 106
used to check boundary conditions. Invalid or erroneous data is data used to
test a program that should always be rejected because it is ‘out of range’.
Bug Bugs are errors in software, causing the program to function in a manner that UNS) Shey, Zita
was not intended.
A bus is part of the computer architecture that transfers data and signals 218, 2247236
between al! the components of the computer.
A bus is a collection of wires through which data is transmitted from one
component to another.
Byte A byte is a group of binary (‘O’ or ‘1’) digits, 8 bits. 151, 159, 161, 180-1
Caesar cipher Caesar ciphers are symmetric. There is one shared key. In a Caesar cipher, 1O9-202
an algorithm replaces each letter in a message with a letter further along in
the alphabet using a number key.
Call The term ‘Call’ to Subroutine means the code inside the subroutine should 12
be executed.
Char Char is an alphabet or symbol in the written form of a natural language. ALSyS), aleyl
Python does not support characters.
Character A character is a data type that stores a single alphabetic or numeric 163
character.
Clients Clients are computers that use the services that a server provides. The client 250
requests services from a server.
Clock chip The clock chip is the electronic device in a computer that controls the timing 220
of signals.
SSS Ss Shs ss hs eet ess
Key points index
svesssssnennsiensseneenenes

Cloud computing Any software, hardware or storage running on servers connected to the
internet can be called cloud computing. In cloud computing you use the
software on machines ‘in the cloud’ instead of using your own personal
computer to carry out or store your work.
Cloud storage Cloud storage uses magnetic and increasingly solid-state storage at a remote
location.
Colour depth Colour depth (or bit depth) refers to the number of bits used for each pixel or
dot. The more bits the more colours that can be represented.
Companion virus A companion is a virus that creates a program with the same name as an
existing, legitimate program and tricks the OS into running it to deliver the
virus.
Compare To compare is to examine (two or more strings, etc.) in order to note 52
similarities and differences.
Compiler A compiler is a computer program that translates C, C++, BASIC, Pascal and 125-6, 130-1, 246
similar high-level programming languages into machine language.
To compile refers to converting (a program) into machine code or a lower-level
form in which the program can be executed.
Compression Data compression is a reduction in file size to reduce download times and ASML dey 1/
storage requirements.
Computational Computational thinking involves learning how to use a set of problem-solving 2-13
thinking skills and techniques that are used by computer programmers to write
programs.
Computer crime Computer crime is any illegal act involving a computer.
Condition- A condition-controlled loop causes a statement or set of statements to repeat 67
controlled loop as long as a condition is false. In Python you use the WHILE statement to
write a condition-controlled loop.
Content filtering Content filtering is a process of restricting access to certain material 270
on the web.
Control unit (CU) The control unit (CU) is inside the CPU and is used to control the flow of data 220-1237
within the system.
Copyright Copyright is exclusive rights given to authors and artists to duplicate, publish 270, 286
and sell their materials.
Copyright and patent laws benefit society because they encourage creators of
new products to make them available to the public.
Count-controlled A count-controlled loop iterates a specific number of times. In Python you use SLLTi
loop the FOR statement to write a count-controlled loop.
Counter A counter is a timer or variable that can be used to count. 65
CPU The central processing unit (CPU) is the electronic circuitry within a computer AWAXG}, ae ¥3), Bala’, Dalie-
that carries out the instructions of a computer program. It is a type of 218-21, 236
processor that runs a computer system. The name processor is a more
generic term but is often used to mean the same thing. The only problem
with using the term processor when referring to the CPU is that there will be
other processors in a system but only one CPU.
Cyber-security Cyber-security is a term used to describe the protection of information 276-82
systems from theft or damage to the hardware, the software and to the
information on them. It consists of the processes, practices and technologies
designed to protect networks, computers, programs and data from attack,
damage or unauthorised access.
Data Data is information, such as text, symbols and numerical values. Data are the 125
facts or details from which information is derived. Data itself has no meaning,
but becomes information when it is interpreted.

Data integrity Data integrity is the state of data being exactly as it should be. 259

Data standards Data standards are the rules by which data are described and recorded. 149

Debugger A debugger is a piece of software that helps a programmer track down faults 104-5
in a program.
Key points index

Debugging The process of testing a program for errors during its execution is a cyclic 104-5
activity cailed debugging.
Decomposition Decomposition means breaking a problem into a number of sub-problems, so 2, S610-12
that each sub-problem accomplishes an identifiable task, which might itself
be further subdivided.
Definite loop In definite loops, the number of iterations is known before we start the 65
execution of the body of the loop.
Discovery mode Discovery mode is a Bluetooth scheme that allows devices to automatically mp oh
detect and connect with other devices.
Domain name A domain name is a human readable name for a resource location on a 270
network. It is changed to a numerical IP address by a DNS server.
Element An element in computer science is an essential or characteristic part of 38, 120, 166
something; other terms often used outside computer science are part,
section, portion, piece and segment. An element can be an item of an array.
Each element can be accessed by referencing its location in the array.
Embedded Embedded systems frequently have more limited resources than PCs. 292-6
systems Embedded systems usually focus on dedicated tasks, but PCs are general-
purpose computers. Embedded systems usually require minimal human
intervention.
Encrypted A programmer at any computer or other device connected to a network could 198, 275
easily modify the software at that host. This is why sensitive data should be
encrypted.
Equality The assignment operator (=) and the equality operator (==) appear to be 50, 206
similar, but are used for completely different reasons. The assignment
operator (=) is used to assign a value to a variable, element or array; the
equality operator (==) is used to compare two values or expressions. The
result is TRUE if the expressions are equal or FALSE if they are not.
Equivalency Equivalency is to look for equality (are they the same?) in value, etc. 62
Erroneous data Erroneous data is data that would not normally be expected such as the 106
wrong data type.
Error There are different types of errors, or bugs, that prevent computer programs 49, 96-101
from working correctly. Three of the key error types are runtime, syntax and
semantic.
Ethics Computer ethics are moral guidelines that govern the use of computers and
information systems.
Execute In computer science to execute is the process of carrying out an instruction
by a computer.
Extreme data Extreme data is data that is at the extreme limit of valid or invalid to check
boundary conditions.
File infector virus A file infector virus is malware that copies itself into other program files (exe, 274
dil, bin, sys, com, drv); when the infected file is executed it loads into memory
and tries to infect other files. It is also known as a parasitic virus.
Firmware Firmware is programming that’s written to the read-only memory (ROM) of a 224
computing device. It is added at the time of manufacturing and is used to run
user programs on the device.
Flowchart A flowchart is a diagram of the sequence of movements or actions of the 14-21, 71
program involved in a complex system or activity.
Function A function is a group of statements that exist within a program for the 84, 91,122
purpose of performing a specific task. In computer programming a function
is a subprogram; given a particular set of argument values, the function
returns a unique result. A function returns a value. Functions return a value
and procedures do not. Functions should be explainable in one sentence that
doesn’t contain AND or OR. You can use functions in your programs again and
again. A function has three parts: a name, parameters and a body.
Key points index

Generalisation Generalisation means adapting a solution that solved one problerh to solve oe
another. It also means replacing many things with one. First decide what
similar problems have in common, then write code that by using variables and
functions can be called when you need these similar features.
Hexadecimal The word hexadecimal means ‘based on 16’ (from Greek hexa: ‘six’ and Latin 142-4
decima: ‘a tenth part’).
High-level language A high-level language is a computer programming language designed to allow 126
people to write programs without having to understand the inner workings of
the computer.
HTTPS HTTPS provides an encrypted version of HTTP for more secure web XCD, ASS), 2UE
transactions.
Hyperlink A hyperlink is an item on a web page that directs the user to another location aifAl
when clicked.
Image size If you want to calculate the size of an image you need to use colour depth ARSE: soyaS
and the number of pixels in your calculations.
Size in bits = WxHxD
Size in bytes = (W x H x D)/8
where W = image width, H = image height and D = colour depth in bits.
immutable An immutable object is an object whose state cannot be modified after it is XG), ALTAAL
created.
Import To import is to bring in from an outside source. 89
Indefinite iteration Indefinite iteration is the name given to the repetition of a sequence where 65
the number of repetitions is unknown.
Indefinite loop In indefinite loops, the number of iterations is not known before we start 65
to execute the body of the loop, it all depends on when a certain condition
becomes true. For example WHILE.
Information Information is a collection of facts or data that is communicated. ALS
Information privacy Information privacy is the right of individuals and companies to deny or 288
restrict the collection and use of information about them.
Input validation Input validation is the process of inspecting data that has been input to a ALTE)
program, to make sure it is valid before it is used in a computation.
Input validation is commonly done with a loop that iterates as long as an
input variable references bad data.
Integer An integer is a whole number value, positive or negative, with no decimal or 34, 159, 165
fractional part.
Interactive device Every interactive device has to have an input, process and output. 214, 215
Interpreter An interpreter is a program that executes a source program by reading it one 126, 130-1, 246
line at a time and doing the specified operations immediately.
IP address The IP address is a number that identifies a device on a TCP/IP network. 261, 286
Iteration Iteration (looping) is repeating parts of a sequence over and over again, like 65
eating breakfast every day at 7.30 a.m.
Iteration is the name given to the repetition of a sequence of computer
instructions a specified number of times or until a condition is met.
Library A library is a collection of files, computer programs, or subroutines. 89
Local variable A local variable is created inside a subroutine and cannot be accessed by 85
statements that are outside the function — it has local scope.
Logic bomb A logic bomb is malware designed to execute and do damage after a certain 270
condition is met.
Logic error A logic error is something that stops the program doing what it is meant to 96, 99-101
do, or causes a runtime error (such as ‘division by zero’).
Logic errors are when a program works but does not give the answer you
require. For example using ‘<’ instead of ‘>’, adding when you should be
subtracting, etc.
1 ee
4 Key points index

7
4
Logical reasoning Logic programming is a method of writing computer programs based on the 2,3
mathematical study of logical reasoning.
Loop A loop is part of a program that repeats itself (to prevent the need for the 42-5
same piece of code to be typed out multiple times). It is a way of repeating a
statement a number of times until the condition is met. The condition allows
the user to escape from the loop.
Lossless Lossless and lossy compression are terms that describe if, in the 183
compression of a file, all original data can be recovered when the file is
uncompressed. With lossless compression, every bit of data that was
originally in the file remains, nothing is lost, when the file is uncompressed.
MAC address MAC address filtering allows devices to access, or be blocked from PPD, PXSSO—AL
filtering accessing, a network based on their physical address embedded within the
device's network adapter.
Machine code Machine code is a set of instructions that a computer can execute directly. 125-6, 127, 130-1, 133,
Machine code is written in a binary code, and each statement corresponds to 134-5, 237
one machine action. A computer's CPU can only understand instructions that
are written in machine code. Machine code and assembly code are low level.
Everything else is high level.
Magnetic storage In magnetic storage, magnetic material is given a polarity. That polarity is 221-8
then read and depending on the direction is output as a binary 1 ora O.
Main memory Main memory (also called primary storage) is any form of memory that is 217, 219, 223-4, 236-7
directly accessible by the CPU, except for caches and registers. It is directly
connected to the motherboard and is the place where programs and data are
kept while being used.
Malware Malware is malicious software designed by attackers to compromise security 210; 2022
and do damage. It is an umbrella term used to refer to a variety of forms of
hostile or intrusive software.
Memory Just like humans, computers rely a lot on memory. They need to process and 223-6
store data, just like we do. However, computers store data in digital format in
a reserved location and of a set location size.
Memory location The computer memory is made up of partitions (much like a set of drawers). 54, 60, 166, 226
The memory location is the address which uniquely identifies every location in
the memory.
Menu A menu is an element displayed on the screen that gives the user a choice of 120, 244
options.
Motherboard The motherboard is the central printed circuit board (PCB) that holds all the 217-18
crucial components of the system.
Nested loop Nested loops consist of an outer loop and one or more inner loops. Each 119-22
time the outer loop is repeated, the inner loops are re-entered and started
again as if new.
Network layer The network layer is sometimes referred to as the internet layer. 264
Non-volatile Non-volatile memory is typically used for the task of secondary storage, or 226; 229,230,
232
memory long-term persistent storage. Examples are ROM and hard drives.
NOT A logic gate that outputs the opposite value to the input. Ooo
Object In computer science, an object could be variable, a data structure or a Shit
function. Each object has a location in memory and a value. Objects are often
referenced by an identifier.
One-dimensional A one-dimensional array is a single variable that can store a list of values. To 166, 167-8
array create an array, you first must define an array variable of the desired type.
Open network An open network is one whose specifications and protocols are public, 272
allowing different companies to produce compatible products.
Operand The operand is the part of the instruction that tells the CPU what to apply the 236
operation to.

310
SSS
Key points index
SSS isc st isc Soe sss

Operating system An operating system is the software that controls all the hardware. It acts 239, 240, 241-2
as an interface between the user and the hardware and also between
applications and the hardware. The operating system is a program that allows
application software to communicate with the hardware.
Operator An operator is a symbol or instruction that performs a specific function: for 236
example, ‘NOT’ or ‘AND’. The operator is the part of the instruction that tells
the CPU what to do.
Optical storage In optical storage, pits and grooves in a flat surface provide a reflection for 227, 228-9
a laser; a light sensor then assigns a binary 1 or a O depending on the light
intensity.
CD, DVD and Blu-ray disks use optical storage.
CD, DVD and Blu-ray disks are all the same physical size and use the same
spiral track layout for placing data on the platter.
OR A logic gate that outputs 1 if either, or both, of the two inputs are 1. US), TAS; olll
Overflow When a number becomes too large to fit into the number of bits allocated it is 140-1
said to ‘overflow’ and some bits are ‘lost’ leaving an incorrect value.
Parameter A parameter is a variable that receives an argument that is passed into a SORLoO
function. A parameter is information explicitly passed to a subroutine as part
of the subroutine call. Parameters in functions are often called arguments.
Penetration testing Penetration testing is the process of attempting to gain access to resources 282
without knowledge of usernames, passwords and other normal means of
access.
Pharming Pharming is a cyberattack intended to redirect a website’s traffic to another, 216-211
fake site.
Phishing Phishing is a technique for obtaining sensitive information by asking users LUO 2
for their passwords, credit card numbers, etc. This is achieved by sending
an email while masquerading as a legitimate entity such as the user’s bank.
Computers are not secured against phishing; each user must rely on good
judgement when revealing sensitive data to others without proper verification
of who they are.
PIN A personal identification number (PIN) is a numeric password, either assigned 2
by a company or selected by a user.
Pixel Pixels are the dots that make the graphical image on screen. They are the 150
smallest element of an image.
Placeholder Variables are a storage placeholder for texts and numbers. A placeholder is 60
a term referring to something not yet finalised. It is a section of computer
storage reserved for information that will be provided later.
Polymorphic virus A polymorphic virus is malware that changes its form each time it is 274
executed, avoiding detection/removal by anti-virus software.
Power-on password A power-on password is a password entered on computer startup, before the 278
OS loads.
Procedure Procedures are collections of statements that define what happens to the 12, 88, 89-90
parameters. It is a Subroutine that does not return values.
Process A process is a series of actions or steps taken in order to achieve a
particular end.
Product activation Product activation is a process that attempts to prevent software piracy by
requiring users to provide a software product’s 25-character identification
number in order to receive an installation identification number.
Program A program is a sequence of instructions to perform a task.
Program counter The program counter is simply a register in the CPU that keeps the address
of the next instruction.
Protocol A protocol is a set of rules or standards that control communication between 250, 261-4
devices.
Quarantine Quarantine is a separate area of a hard disk that holds an infected file until a 270
virus can be removed.
Key oe
ey points eee
index ———E——————

RAM means Random-Access Memory. It is volatile, meaning that when power


217, 223, 224
is interrupted, even for an instant, the contents are erased. RAM is not
suitable for long-term storage of software and data. The contents of RAM can
be manipulated while the computer is running. It is similar to ROM except
that it does not have its contents permanently embedded and thus they will
be erased when the power is turned off.
Randomisation Randomisation is a function in a computer program that allows random of
outcomes from a defined list or number sequence. It is useful when creating
games.
Real number A real number can have decimal or fractional parts. 459,161
Register The register is a special fast access part of the CPU that stores data in use 236, 231
by the CPU.
Repeat-until Repeat-until statements are efficient loops that will continue until the 69
condition is true.
Resolution The resolution is the number of pixels or dots, for example ppi (pixels per 150
inch), often referred to as dpi (dots per inch).
Retrovirus A retrovirus is malware that attacks or bypasses the anti-virus software and 274
can potentially destroy the virus definition file.
RGB RGB stands for red, green and blue. 151-2
ROM ROM (Read-Only Memory) retains data when the computer is turned off. ROM 223, 224
(Read-Only Memory) is another variety of memory. It is similar to RAM except
that it has its contents permanently embedded and thus will not be erased
when the power is turned off.
Router A router is a device that directs messages between computers in a network. DSS:
Sample A sample is a point in time where a measurement of an analogue signal is 155
taken (Sampled). The unit of sample rate is ‘samples per second’. This is
often expressed in kiloHertz (kHz).
Sample rate The sample rate is the number of times the sound is sampled per second. It MUDS=6
is measured in Hz (100 Hz is 100 samples per second).
Sample resolution The sample resolution is the space available for each sample, measured in MS
kilobits.
Secondary storage Secondary storage is the place where software and data are kept when not in 22/—-31)
use.
Secure site A secure site is a website that uses encryption techniques to secure its data. 277
Security Security is the process of protecting systems and data from unauthorised 278
access.
Selection Selection is when a path through the program is selected based on a 6977/0
condition. Sequences are often not a simple line, often the next action
depends upon the last decision; this is called selection.
Sensor A sensor is a device that can detect physical conditions such as temperature, 156
weight, light, sound, etc.
Sequence A sequence is where a set of instructions or actions is ordered, meaning that JES), (61S)
each action follows the previous action. Sequences are often not a simple
line, often the next action depends upon the last decision; this is called
selection.
Sequencing Sequencing is placing information, concepts and actions in order. Steps are 44
followed to lead to the same result, like following a recipe.
Server Servers are powerful computers that provide services to the other computers 250
on the network. The server provides services to a client.
Shouldering Shouldering is observing a person’s private information over their shoulder, 276
e.g. cashpoint machine PINs.
Social engineering Social engineering is gaining unauthorised access or obtaining confidential 274-5
information by taking advantage of the trusting human nature of some victims
and the naivety of others.

312
Key points index

Solid state Solid-state disks do not require physical motion so they have shorter 227, 229-30
response times. They also do not suffer from physical wear.
Solid-state storage uses electrical signals to change the state of a transistor.
This transistor can either be changed to binary 1 or to O.
Source code Source code is the original program code of software; it is the part most 287-8
computer users don’t ever see.
Spam Spam is unwanted, unsolicited email. 270
Spyware Spyware is a non-replicating program used to track user activities, monitor BS
their machines and relay personal information to attackers.
Stealth virus A stealth virus is a virus that attempts to avoid detection by masking itself 274
from applications, redirecting commands around itself.
String A string is a data type used to store a sequence of letters, symbols and/or 5-9
numeric characters.
Subroutine A subroutine is a sequence of instructions that is set up to perform a 82, 88-94
frequently performed task. It is a computer program contained within another
program. It operates semi-independently of the main program. A subroutine
is a named ‘out of line’ block of code that may be executed (called) by simply
writing its name in a program statement.
Switch In computers, transistors act as switches. The current flowing through can be 76, 133-4
turned on or off. A switch is also the name given to a device for connecting
multiple network devices.
Syntax error A syntax error is a mistake in the program’s words or symbols, such as 96-8
misspelled keywords, missing brackets or incorrect indents. These types of
errors are the easiest to find.
A syntax error is something that stops the interpreter from understanding the
code (it cannot run).
TCP/IP TCP/IP (Transmission Control Protocol / Internet Protocol) is a transmission 262
control protocol / internet protocol consisting of a set of standards that
control how data is sent across networks including the internet.
Time-out A time-out is where the server is set to think that there has been too long an 278
interval of time between (1) the establishment of a connection, and (2) the
receipt of any data, so the server drops the connection.
Top-down design Programmers commonly use a technique known as top-down design to break 30
down an algorithm into functions and modules.
Trace table A trace table is a method of using data to check that a flowchart or code 102-3
covers all possibilities correctly.
Translator A translator is a program to convert high-level or assembly-level commands 130, 246
into machine code.
Trojan horse A trojan horse is non-replicating, malicious code contained inside what 275
appears to be a useful program.
Truth table A truth table is a diagram used to show the value of a Boolean expression for Om S— 9
all possible variable combinations.
Truth value The truth value of a statement is always either true or false. 34,75
Unauthorised Unauthorised access is the use of a computer or network without permission, PAYS)
access unauthorised use is the use of a computer or its data for unapproved or
possibly illegal activities.
Unicode Unicode is a universal code used by computers to represent thousands of 145, 147
symbols and text characters. It is a system of up to 32 bits to code the
character set of a computer (usually 16-bit or 32-bit versions).
User interface A user interface is the access point and the boundary between the computer 214-15
and the user.
Valid data Valid data is data used in testing that represents normal data that could be 106
expected.
Key points index
ee

Validation Validation is the process of checking data as it is input to ensure that it is 106, 174-8
sensible; it does not check that it is correct.
Variable A variable is a name/location in memory/identifier, used to store a value 60-1, 166
which can change during execution of the code. The word variable in
programming describes a place to store information that can change, such as
numbers, text, lists of numbers and text. Another way of looking at a variable
is that it’s like a box with a label on to store something.
Virus A virus is a computer program that is spread when its payload is executed, 270, 274-5
interfering with the hardware, software or OS. It is malware that needs a host
application to transport itself.
Virus hoax A virus hoax is an email message that warns users of a non-existent virus or 277
other malware.
Visibility Visibility is another name for the scope of a variable in a program; it is the 85
lines of code in the program where the variable can be accessed.
Volatile memory Volatile memory is a type of storage whose contents are erased when the 266
system’s power is turned off or interrupted.
While While statements are efficient loops that will continue to loop until the 68, 74
condition is false.
A WHILE loop is a control flow statement that allows code to be executed
repeatedly based on a given Boolean condition. The WHILE loop canbe
thought of as a repeating IF statement. It will be executed continuously until
the condition is met.
White-box testing The aim of a white-box penetration test is to simulate a malicious insider who 282
has knowledge of and possibly basic credentials for the target system.
Whitespace Whitespace is any section that is unused, for example the space around an 61
item in the code. Whitespace is a useful tool in any coded solution as it helps
to separate the blocks of code.
Wi-Fi Wi-Fi is a family of related protocols rather than a single protocol. Wi-Fi is a PASYTl
trademark; the generic term for networks of this nature is WLAN.
Worm A worm is self-replicating malware that does not alter files, but resides in PATA, PAUPX, PTET
active memory and propagates itself by means of computer networks to
consume resources.

314 oe
INI
O
D
=
>
a
sweipyieg
O
Z
o
m
zs)
Ss 8AdVaal]
HIOSYON
O
m
on
20LS1220

CUSHING STEVE.
| Edexcel computer science for G
427035/00003 - 2 of 2
This student book has been endorsed for use with the Edexcel GCSE (9-1) specification for Computer Science.

Build student confidence and ensure successful progress through GCSE Computer Science. Our expert
author provides insight and guidance for students to meet the challenges of the new Edexcel
specification, with tasks and activities to test the computational skills and knowledge required for
success in the assessment, and advice for successful completion of the non-examined assessment.
Builds students’ knowledge and confidence through detailed topic coverage and explanation of
key terms.
Develops computational thinking skills with practice exercises and problem-solving tasks.
Instils a deeper understanding and awareness of computer science, and its applications and
implications in the wider world.
Helps monitor progression through GCSE with regular assessment questions, that can be further
developed with supporting Dynamic Learning digital resources.
Author:
Steve Cushing is a well-respected and widely published author for secondary Computing, with examining
experience.

Visit www. hoddereducation.co.uk/ComputerScience/GCSE/Edexcel for more information about all


our resources to support the GCSE Computer Science specification.

Edexcel GCSE Computer Science Dynamic Learning is an online


subscription solution that supports teachers and students with high —
quality content and unique tools. Dynamic Learning incorporates =
Teaching and Learning resources, Whiteboard and Student eTextbook elements
that all work together to give you the ultimate classroom and homework resource.

ISBN 978-1-4718-6622-7

| |
81471 866227

www.hoddereducation.co.uk .

You might also like