Cs Book2
Cs Book2
you need for your written exams as well as your practical Computer Computer
Science Science
assessments.
REVISION REVISION
Through an engaging introduction to the core principles, GUIDE WORKBOOK
you will develop skills in problem solving and computational
thinking. Fo
r the
r the
9–1 Fo
9–1
Other key features of the Student Book include:
n
iti o
exa ms
Inc
ed
de exa ms
lu
s fr e n e
e o n li
A LW AY S L E A R N I N G A LW AY S L E A R N I N G
• in-depth coverage of the core content you need for all ISBN: 9781292131207 ISBN: 9781292131191
components of your course
• a ‘Preparing for your exam’ section with tips and guidance
for achieving success in your written exams
• ‘Exam-style questions’ and ‘Exam tips’ throughout to give
you plenty of practice at answering the kind of questions
you might find in the exam
• ‘Checkpoint’ features to check your knowledge and
challenge your understanding
• ‘Worked example’ features to help you understand how to
carry out practical skills before you try an activity yourself.
Computer Science
Series Editor: Ann Weidmann
www.pearsonschools.co.uk T 0845 630 33 33 Authors: Chris Charles Alex Hadwen-Bennett David Waller Jason Welch Shaun Whorton
myorders@pearson.com
A LW AY S L E A R N I N G
Computer Science
Series Editor: Ann Weidmann
Authors: Chris Charles Alex Hadwen-Bennett David Waller Jason Welch Shaun Whorton
A LW AY S L E A R N I N G
www.pearsonschoolsandfecolleges.co.uk
Copies of official specifications for all Edexcel qualifications may be found on the website: www.edexcel.com
19 18 17 16
10 9 8 7 6 5 4 3
Copyright notice
All rights reserved. No part of this publication may be reproduced in any form or by any means (including
photocopying or storing it in any medium by electronic means and whether or not transiently or incidentally
to some other use of this publication) without the written permission of the copyright owner, except in
accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence
issued by the Copyright Licensing Agency, Saffron House, 6–10 Kirby Street, London EC1N 8TS (www.cla.co.uk).
Applications for the copyright owner’s written permission should be addressed to the publisher.
Acknowledgements
For acknowledgements please see page viii.
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 a related 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 a resource 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.
Introduction iv
Glossary 240
Index 247
iii
There are many benefits to taking the Edexcel GCSE Computer Science course:
• It has a real applied focus. You will be encouraged to put the theory you
are learning into context and apply what you have learned to your own
practical activities. This makes it much more fun.
• It reflects today’s world – the issues and topics you will learn about are
up to date and will help you to understand how technology can be used to
tackle current issues that impact on modern society.
• You will gain a well-rounded understanding of computer science.
Through an engaging introduction to the core principles, you will develop
skills in problem solving and computational thinking. You will learn how
to decompose and model aspects of real-world situations, and as a result
be able to design, build and test a fully programmed solution to
a problem.
• You will also have the opportunity to improve your transferable skills,
developing ‘underpinning’ concepts that are useful in many subjects and
careers, such as mathematics, science and engineering.
• If you do well in this course you will be in a good position to progress
to the next level of study – whether this is an A level or a vocational
qualification, such as a BTEC National. The content of this GCSE is ideal
grounding for other qualifications; it has been designed using a similar
approach to make the experience of moving on a smooth one.
iv
Each chapter gives you all the information you need to know and guides you through the
content of the course in a practical and engaging way, making it clear what you will cover
and giving you useful activities and questions to help you practise what you have learned.
In this student book there are lots of different features. They are there to help you
learn about the content in your course in different ways, understand it from multiple
perspectives and get the most from your learning.
• Key terms – there are certain terms that you will need to know and be
Key terms
able to explain. These boxes explain what the words mean. The words
themselves are also highlighted in the main text, and you will find all the Sequence: an ordered set of
key terms in the Glossary at the end of the book. instructions.
Algorithm: a precise method
• Activities – these are designed Activity 1 for solving a problem. It consists
to build your knowledge and of a sequence of step-by-step
understanding and develop your Produce a written description instructions.
computational and problem- of an algorithm for getting
solving skills. to school. It should start with
leaving home and end with
arriving at school. For example,
the algorithm could start with
'Walk to bus stop'.
vi
vii
The publisher would also like to thank the Python Software Foundation
(www.python.org) for their kind permission to reproduce screenshots.
viii
Problem solving
1.1 Algorithms
Understanding algorithms
Learning outcomes
By the end of this section you should be able to:
• describe what an algorithm is.
• explain what algorithms are used for.
• express algorithms as flowcharts, pseudo-code and written descriptions.
• use and describe the purpose of arithmetic operators.
An example of an algorithm
An interactive map is a useful way to find a route between two locations.
This image shows a route between two cities that was calculated by a
mapping program.
• It is unambiguous in telling the driver exactly what to do, like ‘turn left’,
‘turn right’ or ‘continue straight’.
• It is a sequence of steps.
• It can be used again and will always provide the same result.
The route on this interactive map has • It provides a solution to a problem, in this case how to get from London
been calculated using an algorithm
to Glasgow.
A solution to a problem with these characteristics is called an algorithm.
Most problems have more than one solution, so different algorithms can be
created for the same problem.
Flowcharts
Flowcharts can be used to represent an algorithm graphically. They provide Indicates an input
a more visual display. or output.
There are formal symbols that have to be used in a flowchart – you can’t Shows the logical
just make up your own because nobody else would be able to follow your flow of the
algorithm.
algorithm.
Figure 1.1 shows the flowchart symbols that should be used. Figure 1.1 Flowchart symbols
Activity 2
Fill kettle with water
Display the ‘journey to school’ algorithm, which you created
This is a process (an action
that has to be performed).
in the previous activity, as a flowchart.
Turn on kettle
Activity 3
A student has created a written algorithm for preparing a
Place coffee in cup bath. Display the following as a flowchart – you may need to
change the order or add actions.
This instruction is unambiguous –
the water must be boiling.
• Put in the plug.
Wait for kettle to boil • Fill bath to the correct level.
Stating ‘wait for the water to heat’
would be ambiguous. How hot? • Check temperature is OK.
Stir
Pseudo-code
In addition to flowcharts and written descriptions, algorithms can also be
expressed in pseudo-code. The pseudo-code can then be used to code the
End solution in an actual programming language.
It allows the developer to concentrate on the logic and efficiency of the
Figure 1.2 Flowchart of an algorithm to algorithm without having to bother about the rules of any particular
make a cup of coffee
programming language. It is relatively straightforward to translate an
Key term algorithm written in pseudo-code into any high-level programming language.
Pseudo-code
Start
Algorithm for adding two numbers
SEND ‘Please enter the first number.’ TO DISPLAY
Enter first RECEIVE firstNumber FROM KEYBOARD
number SEND ‘Please enter the second number.’ TO DISPLAY
RECEIVE secondNumber FROM KEYBOARD
SET total TO firstNumber + secondNumber
Enter second
number SEND total TO DISPLAY
Activity 5
Here is a written description of an algorithm.
Enter the first number.
Enter the second number.
The third number is equal to the first number multiplied by the
second number.
Display the third number.
Input
username Summary
• An algorithm is a precise method for solving a problem.
• Algorithms can be displayed as written descriptions, flowcharts and in
Does
NO
pseudo-code.
username
exist? • Pseudo-code is a structured, code-like language.
• Pseudo-code is translated into program code.
YES • Arithmetic operators are used in calculations.
Input • Variables and constants are ‘containers’ for storing data. The value
password stored in a variable can change, whereas the value of a constant never
changes.
• Selecting descriptive names for identifiers makes code easier to read.
Is
NO
password
correct?
Checkpoint
Strengthen
YES
S1 Produce a written description of an algorithm for borrowing a book
End from the library.
S2 Describe what each of the seven arithmetic operators does.
Figure 1.4 Flowchart of an algorithm S3 Describe what a variable is and explain why variables are useful.
Produce a written description of S4 Explain the difference between a variable and a constant.
this algorithm. Challenge
C1 Produce a flowchart describing an algorithm for making a cheese
sandwich.
C2 Write an algorithm expressed in pseudo-code that receives three
numbers from the keyboard, calculates and displays the average.
Is
temperature of NO Did you know?
water = We use iteration in our daily lives whenever we carry out an
100°C?
action over and over again. For example, at mealtimes we keep
YES on eating until our plate is empty or we have had enough to eat.
When we’re travelling by car and the traffic lights are red we have
Turn off kettle to keep waiting until they change to green.
An actor repeats their lines over and over again until they
The computer are word perfect.
would also have
Pour water into cup to be told how
much water to
pour into the cup!
IF…THEN statement
IF score >= 90 THEN
SEND ‘Excellent’ TO DISPLAY
END IF
Relational operators
Relational operators are used to compare two values. The operators that
you will be using are:
= equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
<> not equal to
Activity 7
10
Activity 8
A driving school uses this rule to estimate how many lessons a learner
will require.
• Every learner requires at least 20 lessons.
• Learners over the age of 18 require more lessons (two additional
lessons for each year over 18).
Create an algorithm expressed in pseudo-code that inputs a learner’s age
and calculates the number of driving lessons they will need.
Nested selection
The IF…THEN…ELSE statement allows a choice to be made between Key term
two possible alternatives. However, sometimes there are more than two
Nested IF statement: a nested
possibilities. This is where a nested IF statement comes in useful.
IF statement consists of one or
more IF statements placed inside
Worked example each other. A nested IF is used
where there are more than two
A learner handed in three homework assignments, which were each given
possible courses of action.
a mark out of 10. All the marks were different. Write an algorithm that
would print out the highest mark.
Figure 1.7 shows the algorithm expressed as a flowchart:
Start
Input mark1,
mark2, mark3
Is Is
NO NO
mark1 > mark2 >
mark2? mark3?
YES YES
Output Output
Is mark2
Output NO mark3
mark1 >
mark3 mark3?
11
The variables mark1 and mark2 are compared using a relational operator.
If mark1 is greater than (>) mark2 then it is compared with mark3. If it is
not greater than mark2, then mark2 must be greater than mark1 and it is
then compared with mark3.
This algorithm can also be expressed in pseudo-code:
RECEIVE mark1 FROM KEYBOARD
RECEIVE mark2 FROM KEYBOARD
RECEIVE mark3 FROM KEYBOARD
IF mark1 > mark2 THEN
IF mark1 > mark3 THEN #This is an IF statement
within another IF statement.
It is called a nested IF.
12
Activity 10
A school uses this algorithm to calculate the grade learners achieve in
end-of-topic tests.
What would be the output of this algorithm for these test scores: 91, 56
and 78?
Iteration
When writing programs it is often necessary to repeat the same set of
statements several times. Rather than simply making multiple copies of the
statements you can use iteration to repeat them. The algorithm for making
a cup of coffee includes an instruction to keep waiting until the water in the
kettle boils.
Key terms
Waiting for the kettle to boil Indefinite iteration: this is used
when the number of iterations
IF temperature = 100°C THEN
is not known before the loop is
Switch off kettle
started. The iterations stop when
ELSE
a specified condition is met.
Keep waiting
This sort of loop is said to be
END IF
condition controlled.
Selection would be useless without iteration. The question would be asked Definite iteration: this is used
once and then the program would move on or just stop. There has to be a when the number of iterations,
method for repeating the question until there is a desired outcome. In a or turns of the loop, is known
flowchart this is easy to implement – you just have to draw some arrows. in advance. It can be set to as
many turns as you want. This
In both pseudo-code and program code you have to construct a loop, sort of loop is said to be count
or iteration. There are two types of iteration: indefinite iteration and controlled.
definite iteration.
13
Therefore we have to use indefinite iteration. There are two ways of doing this
in pseudo-code: you can use a REPEAT…UNTIL loop or a WHILE…DO loop.
Using WHILE…DO
RECEIVE temp FROM SENSOR
WHILE temp < 100 DO
RECEIVE temp FROM SENSOR
END WHILE
Switch off kettle
14
Worked example
A learner is designing a program to help younger children with their times
tables. When a user enters a number the program will output the times
table up to 12.
RECEIVE number FROM KEYBOARD #The number entered is assigned
to the variable ‘number’.
FOR index FROM 1 TO 12 DO #The loop is set up using the
variable ‘index’ which will
change from 1 to 12 at each
turn of the loop.
SEND number * index TO DISPLAY #The value of ‘number’ is
multiplied by the value of ‘index’
at each turn of the loop.
END FOR #This command is used to close
the loop.
This loop will be repeated 12 times. At each turn of the loop the variable
‘index’ is incremented by one.
Top tip
Variables in a FOR loop can be used to indicate the start and end values of
the loop, for example
start = 25
finish = 50
FOR index FROM start TO finish DO
SEND ‘Congratulations’ TO DISPLAY
END FOR
Activity 12
Create an algorithm expressed in pseudo-code that asks a user to enter
a start number and an end number and then outputs the total of all the
numbers in the range. For example, if the start number was 1 and the end
number was 10, the total would be 55 (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10).
Tip:
You should initialise the variable total to zero before the start of the loop.
15
Worked example
SET correct TO ‘LetMeIn’ #The variable ‘correct’ is assigned
the value of the password stored in
the system.
FOR index FROM 1 TO 3 DO
SEND ‘Please enter your password.’ TO DISPLAY
RECEIVE password FROM KEYBOARD
IF password = correct THEN
SEND ‘You entered the correct password.’ TO DISPLAY
END IF
END FOR
The loop will ask for the password to be input three times, but what if the
user gets the password correct on the first attempt? They do not want to
have to enter it twice more.
Another solution would be to use indefinite iteration and keep count of the
number of iterations.
Worked example
SET count TO 0 #The variable ‘count’ is assigned
the value 0.
SET correct TO ‘LetMeIn’ #The variable ‘correct’ is assigned
the value of the password stored
in the system.
REPEAT
SET count to count + 1 #The variable ‘count’ is
incremented by 1 on each turn.
SEND ‘Please enter your password.’ TO DISPLAY
RECEIVE password FROM KEYBOARD
IF password = correct THEN
SEND ‘Correct password.’ TO DISPLAY
ELSE
SEND ‘Incorrect password.’ TO DISPLAY
END IF
UNTIL password = correct OR count = 3
The loop will end if either of the conditions is met – if the password is
correct or the number of attempts is equal to 3.
Key term
Logical operator: a Boolean We have used a compound comparison by joining two conditions together
operator using AND, OR and NOT. using an ‘OR’, which is a logical operator.
16
Worked example
FOR student = 1 TO 20
SET sum = 0
FOR mark = 1 TO 5
RECEIVE nextMark FROM KEYBOARD
SET sum TO sum + nextMark
END FOR
SET averageMark TO sum/5
SEND averageMark TO DISPLAY
END FOR
17
Worked example
A learner has a Saturday job selling cups of tea and coffee. The tea is
£1.20 per cup and the coffee is £1.90. He is supposed to keep a record of
the number of cups of each he sells.
Unfortunately he has been so busy that he has lost count but he knows
that he did not sell more than 100 of each.
He has collected £285.
Write a program that will calculate how many cups of tea and coffee he sold.
SET teaCost TO 1.2
SET coffeeCost TO 1.9
FOR numCoffees FROM 1 TO 100 DO
FOR numTeas FROM 1 TO 100 DO #This loop for the teas is
nested inside the loop for
coffees.
SET total TO (numCoffees * coffeeCost) +
(numTeas * teaCost)
IF total = 285 THEN
SET teas TO numTeas
#These ‘teas’ and ‘coffees’
variables are needed as
the loops will continue and
‘numCoffees’ and ‘numTeas’
will change.
SET coffees TO numCoffees
END IF
END FOR
END FOR
SEND ‘The number of teas is’ & teas & ‘and the number of
coffees is’ & coffees TO DISPLAY
In this SEND command, four items of information are displayed. First there
Key term
is some literal text enclosed in quotation marks – ‘The number of teas
Concatenation: the linking is’ – followed by the value of the variable teas, followed by some more
together of two or more items of literal text – ‘and the number of coffees is’ – followed by the value of the
information. variable coffees. The four are joined by an ‘&’ symbol – known as the append
operator. Joining items of information in this way is called concatenation.
If this algorithm was converted into program code and executed this
Did you know? sentence would be displayed on the screen:
Different symbols are used
in other pseudo-codes and ‘The number of teas is 95 and the number of coffees is 90’
programming languages, with
the ‘+’ symbol being the most Activity 14
popular. Create an algorithm that will print out the times tables (up to 12 times)
for the numbers 2 to 12. You should use concatenation so that the
printouts state “2 x 2 = 4”, “2 x 3 = 6”, “9 x 8 = 72”, etc.
18
Summary
• The constructs command sequence, selection and iteration are the basic building blocks of algorithms.
• Nested IF statements allow for more than two alternatives.
• There are two types of iteration – definite and indefinite.
• Another name for iteration is loop. Loops can be nested.
• Comments make algorithms easier to understand. The # symbol indicates a comment in pseudo-code.
• Concatenation is the linking together of two or more items of information.
Checkpoint
Strengthen
S1 Explain, using examples, how command sequence, selection and iteration are used in algorithms.
S2 Explain what each of the six relational operators does.
Challenge
C1 Design an algorithm in pseudo-code that asks the user to enter their height (in metres) and weight (in
kilograms) and displays their body mass index (BMI). The formula for calculating BMI is weight / height2.
C2 Design an algorithm expressed as a flowchart to control the heating in a house. A thermostat monitors the
temperature within the house. During the week the temperature should be 20°C between 6.00 and 8.30 in the
morning and between 17.30 and 22.00 at night. At weekends it should be 22°C between 8.00 and 23.00. If the
temperature in the house falls below 10°C at any time the boiler is switched on.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try the following activities again.
19
• The length of the rectangle is input and is stored in the variable ‘length’.
area =
length * width • The width of the rectangle is input and is stored in the variable ‘width’.
• The variable ‘length’ is multiplied by the variable ‘width’ to find the area
of the rectangle, which is stored in the variable ‘area’.
Output • The value of the variable ‘area’ is output.
area
Here is another algorithm, this time displayed in pseudo-code.
20
ELSE
IF goalsFOR < goalsAgainst THEN
SET loss TO loss + 1
ELSE
SET draw TO draw + 1
END IF
END IF
SEND ‘Press ‘Y’ to enter another result.’ TO DISPLAY
RECEIVE anotherEntry from KEYBOARD
END WHILE
SEND ‘Total wins: ‘ & win TO DISPLAY
SEND ‘Total losses: ‘ & loss TO DISPLAY
SEND ‘Total draws: ‘ & draw TO DISPLAY
21
Worked example
Create a trace table for the following algorithm.
SET number TO 3
FOR index FROM 1 TO 5 DO
SET number1 TO number * index
SET number2 TO number1 * 2
IF number2 > 20 THEN
SEND number2 TO DISPLAY
END IF
END FOR
This algorithm can be traced in the following table.
number index number1 number2 output
3
3 1 3 6
3 2 6 12
3 3 9 18
3 4 12 24 24
3 5 15 30 30
The value of the variable number remains at 3 throughout, but as the index
increases from 1 to 5, then so do the values of number1 and number2.
When the value of number2 is greater than 20, its value is output.
Activity 16
Complete a trace table for this algorithm.
SET number1 TO 2
SET number2 TO 3
FOR index FROM 1 TO 5 DO
SET number1 TO number1 * index
SET number2 TO number2 + number1
END FOR
Exam tips
Spend some time studying the algorithm to ensure that you fully
understand it.
In 1 you are asked to ‘explain’ how the algorithm works. A longer answer
is required that includes all of the stages of the algorithm. Use the correct
terms to explain the constructs.
In 2 and 3 short answers are sufficient.
In 4 calculate the charge for each person using the rules of the algorithm.
Then calculate the overall charge and check to see if the family qualifies
for a group discount.
22
Exam-style question
This flowchart displays an algorithm used by
Start
Holiday Theme Parks Limited.
charge = 0
total = 0
number = 0
Input
customer
charge = £10
number = number + 1
Input
age
Is age YES
A charge = £5
< 13? number = number – 1
NO
Is age YES
charge = £9
> = 60?
NO
B
End
Figure 1.9 Flowchart of an algorithm showing charges in a theme park
23
Identifying errors
There are three types of error in computer programs.
• Syntax errors occur when algorithms are being converted into program
code.
• Runtime errors occur when the program is executed.
• Logic errors are errors in the design of algorithms.
24
The best way to find logic errors is to use the technique in the section above
– use sample data and check if the actual output from the algorithm is as
expected. You’ll learn more about this in Chapter 2.
25
Activity 18
This is part of a larger algorithm
designed to ensure that input data Start
falls within a certain range. It is part
of a school management system and
checks that the ‘year group’ entry is Input
acceptable. It has learners aged 11 to yearGroup
18 years with year groups of 7 to 13.
The staff using the system
congratulated themselves on never Is
yearGroup >=
making an error when entering the 7 OR <= 13?
year group, but when learner lists
were printed out they immediately
YES
received complaints.
What is the logic error in the
Figure 1.10 Flowchart showing an
algorithm?
error in an algorithm
Worked example
Study this algorithm.
WHILE index < 10 DO
SET index TO 1
SEND index TO DISPLAY
SET index TO index + 1
END WHILE
The expected output is 1, 2, 3, 4, 5, 6, 7, 8, 9.
But there is a logic error. The variable ‘index’ is initialised in the wrong
place. It should be done before the start of the WHILE loop.
This algorithm would loop forever because at each turn in the loop the
variable index is set to 1. It will never reach 10. This is an example of an
‘infinite loop’. The algorithm should have been written as shown below.
Key term SET index TO 1
WHILE index < 10
Infinite loop: a loop that is
SEND index TO DISPLAY
never-ending since the condition
required to terminate the loop is SET index TO index + 1
never reached. END WHILE
26
Activity 19
Find and correct the errors in these algorithms.
• Example 1
SET index TO 1
WHILE index < 10
SEND index TO DISPLAY
END WHILE
• Example 2
SET index TO 1
WHILE index < 10
SEND index TO DISPLAY
SET index TO index - 1
END WHILE
• Example 3
SET index TO 1
WHILE index < 1
SEND index TO DISPLAY
SET index TO index + 1
END WHILE
Summary
• A logic error in an algorithm will produce an incorrect result.
• Tracing the value of variables in an algorithm helps to identify logic errors.
• Understanding the order of precedence of arithmetic operators and the significance of brackets will help you
to avoid making logic errors.
Checkpoint
Strengthen
S1 Describe the purpose of an algorithm and explain how it works.
S2 Explain, using examples, what a logic error is.
S3 Use a trace table to check the output of an algorithm and identify any logic errors. Explain what BIDMAS
means and demonstrate how this expression would be evaluated.
43 × 10 / 2 + (8 − 3)
27
Challenge
C1 State the purpose of this algorithm and explain the rules behind the calculation.
SEND ‘Enter the weight of your parcel in kilograms’
TO DISPLAY
RECEIVE parcelWeight FROM KEYBOARD
IF parcelWeight <= 2 THEN
postage = 8
ELSE
IF parcelWeight <= 10 THEN
postage = 8 + ((parcelWeight – 2) * 2.5)
ELSE
postage = 8 + (8 * 2.5) + ((parcelWeight – 10) *
3.5)
IF END
IF END
SEND ‘The cost of posting your parcel is ‘ & postage
TO DISPLAY
C2 People often want to know the human-equivalent age of their dog or cat. The rules for calculating this are:
A 1-year-old cat is equivalent in age to a 15-year-old human, a 2-year-old cat is equivalent in age to a 24-year-
old human. Add four years for every year after that.
A 1-year-old dog is equivalent in age to a 12-year-old human, a 2-year-old dog is equivalent in age to a
24-year-old human. Add four years for every year after that.
Design an algorithm expressed in pseudo-code that allows the user to input the age of their dog or cat and
outputs its equivalent human age. It should allow the user to have another go.
Use a trace table and test data to check the logic of your algorithm.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try the following activities again.
• For S1 reread pages 20–21 and have another go at all the activities.
• For S2 and S3 reread the section on trace tables on pages 21–22 and the
section on identifying errors on pages 24–25.
28
o Merge sort
o Binary search
• explain how the choice of algorithm is influenced by the data structures and data values that need to be
manipulated.
• evaluate the fitness for purpose of algorithms in meeting specified requirements efficiently using logical
reasoning and test data.
Two of the most common tasks in computer programs are sorting data into
a particular order and searching for particular items of information.
Arrays
All sorting and searching algorithms work on lists of data. As you already
know, a variable stores just a single value (e.g. SET age TO 15 or SET
firstName TO ‘David’).
But what if a programmer wants to store lots of related values, such as the
names of a group of friends? A tedious way of doing it might look something
like this.
SET friend1 TO ‘Alice’
Key term
SET friend2 TO ‘Barry’
Array: an organised collection of
SET friend3 TO ‘Catherine’
related values that share a single
It would be much easier if all the names could be stored in one list to which identifier.
the names of new friends can be added. That’s exactly what an array allows
you to do.
An array with the identifier ‘arrayFriends’ would store the data items
like this.
arrayFriends = [‘Alice’, ‘Barry’, ‘Catherine’]
29
This would insert the name ‘David’ at index position 3 and the name ‘Eva’ at
index position 4 – the fourth and fifth positions in the array.
Top tip
Pseudo-code has a built-in Worked example
LENGTH command, which you
can use to find the number This code will traverse an array named ‘arrayFriends’ and print out each
of elements in an array. For element of the array.
example, SET size TO FOR index FROM 0 TO LENGTH(arrayFriends) – 1 DO
LENGTH(arrayFriends) would #The loop has to run to the length of
set the variable ‘size’ to 5. the array minus 1 as indexing starts at
Remember: the elements have 0. For example, if there are 10 items
indexes 0 to 4. then they will be indexed as 0 to 9
(10 –1).
SEND arrayFriends[index] TO DISPLAY
Top tips END FOR
• Use the LENGTH function to
find out how many marks
there are altogether. Activity 20
• Use a variable maxMark – set An array named ‘arrayScores’ contains a set of marks that a learner has
initially to 0 – to find the obtained during her computer science course.
highest mark. Create an algorithm expressed in pseudo-code to find and display her
• Each mark in the array is highest mark and her average mark.
compared in turn with the
value stored in maxMark. Activity 19 would have been a whole lot easier if the elements of the array
• If it is higher then maxMark is had been sorted into order. We will next be looking at two ways this can be
updated. done.
Sorting algorithms
Key terms As sorting is such a widely used procedure, many algorithms have been
Ascending order: this is created to carry it out. As with all algorithms, some are more efficient
arranging items from smallest to than others.
largest (e.g. 1, 2, 3, 4, 5, 6 or a, b,
c, d, e, f). Bubble sort
Descending order: this is When data is sorted, different items must be compared with each other and
arranging items from largest to moved so that they are in either ascending order or descending order.
smallest (e.g. 6, 5, 4, 3, 2, 1 or f, e,
The bubble sort algorithm starts at one end of the list and compares pairs of
d, c, b, a).
data items. If they are in the wrong order, they are swapped. The comparison
30
of pairs continues to the end of the list, each complete traversal of the list
Key term
being called a pass. This process is repeated until there have been no swaps
during a pass, indicating that the items must all be in the correct order. Traversal: travel across or
through something. An array can
The algorithm can be described as follows. be traversed by moving from the
first to the last element in order
Bubble sort (ascending order) to examine the data stored at
1 Start at the beginning of the list. each index position.
2 Compare the values in position 1 and position 2 in the list – if they are
not in ascending order then swap them.
3 Compare the values in position 2 and position 3 in the list and swap if
necessary.
4 Continue to the end of the list.
5 If there have been any swaps, repeat steps 1 to 4.
Worked example
Here is an example of a bubble sort in action.
Pass 1 Pass 2
Items 1 and 2 must Items 1 and 2 are in
4 2 6 1 3 2 4 1 3 6
be swapped. correct order.
Items 1 and 2 are Items 2 and 3 must
2 4 6 1 3 2 4 1 3 6
swapped. be swapped.
Items 2 and 3 are Items 2 and 3 have
2 1 4 3 6
2 4 6 1 3 already in ascending been swapped.
order. Items 3 and 4 must
2 1 4 3 6
Items 3 and 4 must be swapped.
2 4 6 1 3
be swapped. Items 3 and 4 have
2 1 3 4 6
Items 3 and 4 have been swapped.
2 4 1 6 3
been swapped. Items 4 and 5 do not
2 1 3 4 6
Items 4 and 5 must need to be swapped.
2 4 1 6 3
now be swapped.
Items 4 and 5 have Pass 3
2 4 1 3 6
been swapped. Items 1 and 2 must
2 1 3 4 6
be swapped.
Figure 1.11 A bubble sort
Items 1 and 2 have
1 2 3 4 6
It would take a human three passes to carry out this been swapped.
bubble sort, but a computer would need four passes All items are now in
1 2 3 4 6
because it must continue until there have been no swaps. the correct order.
A computer cannot just look at all of the numbers at
once and see that they are all in order.
31
Activity 21
length = length of list
position = 1 Study the flowchart of the bubble sort algorithm.
switch = 0
Using the variables declared, explain the logic behind the
algorithm – explain how it functions to sort a list.
Is List Swap List item
item (position) > YES (position) and List The bubble sort algorithm can also be expressed in
List item (position item (position + 1)
+ 1)? switch = switch + 1 pseudo-code. It assumes that the items to be sorted are
stored in an array.
NO
Activity 22
A tutor has stored a set of class examination results in an array
named ‘exam1’. Write an algorithm expressed in pseudo-code
to sort the results into descending order and then output the
highest and lowest result.
32
Worked example
Here is an example of a merge sort which will sort the following list into ascending order.
8 4 2 6 1 3 5 7
The list is recursively split into half to produce a left list and a right list each time.
8 4 2 6 1 3 5 7
8 4 2 6 1 3 5 7
This continues until there is only one item in each list. Therefore each list is sorted into order.
8 4 2 6 1 3 5 7
The left and right lists are now recursively merged with the items in the correct order.
4 8 2 6 1 3 5 7
The leftmost items in each list are the lowest items of those lists and the algorithm compares them – in this case
4 with 2. The 2 is inserted in the new list and the 4 is then compared with the second number of the right list – 6.
The 4 is inserted and the 6 is compared with the second number of the left list.
2 4 6 8 1 3 5 7
The algorithm now merges these two lists in the same way to produce the final sorted list.
1 is compared with 2 and then 2 with 3, 3 with 4 etc.
1 2 3 4 5 6 7 8
Activity 23
Using a table like the one in the worked example, show how the following
list would be sorted into descending order using merge sort.
48, 20, 9, 17, 13, 21, 28, 60
33
Time (seconds)
all possibilities until the solution
to a problem is found. 3
Divide and conquer: an
algorithm design that works by 2
dividing a problem into smaller
1
and smaller sub-problems,
until they are easy to solve.
0
The solutions to these are then 0 100 1000 15000 250000 500000
combined to give a solution to Number of items
the complete problem. Figure 1.14 A graph comparing the performance of bubble and merge sort algorithms
34
Activity 24
This flowchart displays a linear search algorithm, but some of the symbols
have not been labelled. Draw and complete the flowchart using the
labels provided.
Start
NO
YES
Output array NO
[index]
YES
End
End
Activity 25
Labels
An array contains the names
of the one hundred most Output search item
Is index > length?
Does array [index] =
not found target?
downloaded performers on an
online music streaming site.
Produce an algorithm expressed index = index + 1 Input search item
in pseudo-code that enables
a user to see if their favourite
performer is in the top 100. Figure 1.15 A flowchart displaying a linear search algorithm with labels missing
35
To use this method the list must be sorted into ascending or descending
order. It will not work on an unsorted list.
Worked example
In this list, the search item is the number 13.
Select the
3 13 24 27 31 39 45 60 69 median
number.
As this is too high, the sub-list to the left of the median must be searched.
This is again too high and so the sub-list to the left must be searched.
36
Activity 26
Display the stages of a binary search, as in the worked example above, to
find the number 13 in this list.
3 9 13 15 21 24 27 30 36 39 42 54 69
Activity 27
Complete the missing sections of the pseudo-code.
37
Searching algorithms can be compared by looking at the ‘worst case’ and the
‘best case’ for each one.
Worked example
If you wanted to find a particular item in a list of 1000 items these are the best
and worst case scenarios for the linear search and binary search algorithms.
Linear search
A linear search starts at the first item and then works through sequentially.
The best case would be if the item is first in the list.
The worst case would be if it is the last in the list.
Therefore in this example the average would be 500 comparisons.
Binary search
The best case would be if the item is in the median position in the list. The
search would require only one comparison.
For the worst case it would have to choose the following medians until it
finally hit the target.
(This assumes that the target is always smaller than the median.)
Attempt Median
1 500
2 250
3 125
4 63
5 32
6 16
7 8
8 4
9 2
10 1
Therefore the worst case for the binary search is ten comparisons.
The binary search is therefore far more efficient than the linear search.
If the list is to be searched just once then a linear search would be better, but
if there is a large list that will be searched many times then sorting the list and
using binary search would be better. Once the list has been sorted new items
can be inserted into the correct places.
38
Exam-style question
A tutor has stored learner surnames in an array as shown below.
Marek Jackson Bachchan Wilson Abraham French Smith
1 Show the stages of a bubble sort when applied to this data.
The tutor has a sorted list of names from another class as shown below.
Azikiwe Bloom Byrne Davidson Gateri Hinton Jackson Linton Smith Wall
2 Show the stages of a binary search to find the name ‘Jackson’ when applied to this list.
Exam tips
These questions are testing knowledge of the sort and search algorithms.
1 The answer should be set out to show how the data is progressively
sorted using the bubble sort and the result of each pass should be
shown.
2 This question is to check that you know that this is a recursive method
where the median is repeatedly selected.
Summary
• An array stores multiple items of data.
• There are many algorithms for sorting and searching data.
• The choice of algorithm depends on the data that is to be processed.
Checkpoint
Strengthen
S1 Describe the differences between the ‘bubble sort’ and ‘merge sort’ algorithms.
S2 Describe how a binary search algorithm finds the search item.
Challenge
C1 Explain when a linear search might be preferable to a binary search even though the binary search algorithm
is more efficient.
C2 Discuss the advantages of using arrays in algorithms.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try the following activities again.
39
Worked example
A learner has been set the task of creating a computer version of the
‘noughts and crosses’ game where a user plays against the computer.
40
Abstraction
We use abstraction all the time in our daily lives. We abstract the essential
features of something so that we can understand what people are trying
to communicate.
Somebody might say, ‘I was walking down the street
when I saw a cat.’ You immediately understand what
they mean by ‘street’ – probably a road with a pavement
and houses or shops along the side of it. Similarly you
can picture the cat – a smallish animal with fur, four legs
and a tail. An animal that is basically ‘cattish’. You have
extracted the basic properties of animals called cats so
that you can recognise one when you see one or imagine
one when somebody talks about a cat.
What you picture is very unlikely to be exactly like the
actual street and cat that the person experienced. But
because of our ability to abstract, the person did not have
to go into unnecessary painstaking detail about exactly
where they were and what they saw. They wouldn’t get
very far with the story if they did. Is this the street and cat you imagined?
When we create algorithms we abstract the basic details of the problem and
represent them in a way that a computer is able to process.
Worked example
Yasmin is designing a computer version of a game in which users have to
throw a die to determine their number of moves.
In the computer game the users can’t have an actual die, so she will have
to design a ‘pretend’ or virtual die that behaves in exactly the same way
as a real-life die.
Yasmin will have to use her powers of abstraction to work out the
essential features of a die and then represent them in computer code.
To represent the die she will have to create a routine that will select a
random number from 1 to 6 because that’s what a die does.
Yasmin has used abstraction to model a real-life event.
Levels of abstraction
There are different levels or types of abstraction. The higher the level of
abstraction, the less is the detail that is required. We use abstraction all the
time in accomplishing everyday tasks.
When programmers write the ‘print’ command they do not have to bother
about all of the details of how this will be accomplished. They are removed
from them. They are at a certain level of abstraction.
A driver turning the ignition key to start a car does not have to understand
how the engine works or how the spark to ignite the petrol is generated. It
just happens and they can simply drive the car. That is abstraction.
41
The solution is still at a high level of abstraction and more details will need
to be added.
For example, the programmer will need to decide how the game will record
which player has selected each square; how the computer will decide which
square to select; how the game will decide if the computer or the user has won.
The programmer will have to go into more and more detail or move to lower
levels of abstraction.
Eventually the programmer will be able to design an algorithm for the game
and code it using a high-level programming language such as Python or
Java. Even before they start to implement the game, they will need to plan
how they will test the finished program to make sure that it works correctly;
what test data they will use; what outcomes it should produce.
42
Coding an algorithm
High-level programming languages make it easier for a programmer to write
code. Unfortunately, the processor, which has to execute the program, cannot
understand the language it is written in, so it needs a translator to translate
the code into the only language it does understand – a stream of 1s and 0s.
The processing can be split into parts. For example, in the example of the
noughts and crosses game there could be separate algorithms for:
• deciding where the computer should make its next selection – it could be
called ‘computer entry’;
• checking if the computer or the player has won – it could be called ‘check
if won’;
• checking if there are any empty squares left – it could be called ‘check draw’.
These separate algorithms could be used when they are needed. It is
efficient because it means that the same code doesn’t have to be rewritten
whenever it is needed.
In Chapter 2 we’ll look in detail at how subprograms are used to reduce the Key term
complexity of programs and to make them easier to understand. Subprogram: a self-contained
module of code that performs a
In the die example above, the designer could write a subprogram called
specific task. It can be ‘called’ by
‘die’ that generates a random number from 1 to 6. In the main program the
the main program when it
designer could just call the ‘die’ subprogram without having to think about
is needed.
how to implement it each time.
43
Summary
• Computational thinking is an approach to solving problems that includes techniques such as decomposition
and abstraction.
• Problems are easier to solve if they are decomposed into smaller
sub-problems.
• Abstraction is used to remove unnecessary detail to make a problem easier to understand and solve.
• When designing a solution to a problem the inputs, outputs and processing requirements should be identified
at the outset.
Checkpoint
Strengthen
S1 Explain what is meant by ‘decomposition’ and the benefits it provides for programmers.
S2 Explain what is meant by ‘abstraction’.
Challenge
C1 Describe examples of the use of ‘decomposition’ and ‘abstraction’ when solving a problem.
C2 In your own words explain what is meant by ‘computational thinking’.
44
Programming
46
Pseudo-code Python
SET totalFor TO 0 totalFor = 0
SET totalAgainst TO 0 totalAgainst = 0
SET win TO 0 win = 0
SET loss TO 0 loss = 0
SET draw TO 0 draw = 0
SET anotherEntry TO ‘Y’ anotherEntry = ‘Y’
WHILE anotherEntry = ‘Y’ DO while anotherEntry == ‘Y’:
SEND ‘Enter goals for:’ TO DISPLAY
RECEIVE goalsFor FROM KEYBOARD goalsFor = int(input(‘Enter goals for:’))
SEND ‘Enter goals against:’ TO DISPLAY
RECEIVE goalsAgainst FROM KEYBOARD goalsAgainst = int(input(‘Enter goals against:’)
SET totalFor TO totalFor + goalsFOR totalFor = totalFor + goalsFor
SET totalAgainst TO totalAgainst + goalsAgainst totalAgainst = totalAgainst + goalsAgainst
IF goalsFor > goalsAgainst THEN if goalsFor > goalsAgainst:
SET win TO win + 1 win = win + 1
ELSE
IF goalsFOR < goalsAgainst THEN elif goalsFor < goalsAgainst:
SET loss TO loss + 1 loss = loss + 1
ELSE
else:
SET draw TO draw + 1 draw = draw + 1
END IF
END IF
SEND ‘Press ‘Y’ to enter another result.’ TO DISPLAY
RECEIVE anotherEntry from KEYBOARD anotherEntry = input(“Press ‘Y’ to enter another result”)
END WHILE
SEND ‘Total wins: ‘ & win TO DISPLAY print(‘Total wins:’,win)
SEND ‘Total losses: ‘ & loss TO DISPLAY print(‘Total losses:’,loss)
SEND ‘Total draws: ‘ & draw TO DISPLAY print(‘Total draws:’,draw)
As you can see, the algorithm and the program are similar, but not the same.
Some obvious differences are:
47
You can get away with missing out a bracket, getting the indentation
wrong or misspelling a command word in pseudo-code because the target
audience is a human and humans are able to make allowances for any
shortcomings in the code. The same can’t be said for computers. If the code
Key term isn’t exactly right, the computer won’t be able to execute it and will flag up
Syntax error: an error that a syntax error. Needless to say, you are bound to encounter syntax errors
occurs when a rule of the frequently while learning to program.
programming language is broken.
Top tip
Not all programming languages use the same symbols to represent
Activity 1
arithmetic operators. Here are the symbols Python and Java use to
Start compiling a reference represent modulo (MOD), integer division (DIV) and exponent (^):
guide that shows the high-level
program code equivalents to Python Java
the pseudo-code commands Modulo % %
you use. Begin by revisiting Integer Division // Math.floor(a / b)
the algorithms you wrote while Exponent ** Math.pow(a, b)
studying Chapter 1. You need to check which symbols the language you are learning uses for
arithmetic operators.
Data types
As you learnt in Chapter 1, algorithms use variables (named memory
locations) to store values. Variables have a variety of uses, for example
controlling the number of times a loop is executed, determining which branch
of an IF statement is taken, keeping running totals and holding user input.
48
integer 8 * 5 = 40
real 8.0 * 5 = 40.0
character ‘8’ * 5 = ‘88888’
Top tip
Although you don’t have to declare the data types of variables in pseudo-
code, it is a good idea to do so. You can simply list the variables and their
data types at the start of an algorithm, for example:
INTEGER age
REAL weight
BOOLEAN correct
CHARACTER gender
You can also specify the data type of a variable in the RECEIVE statement,
for example:
RECEIVE age FROM (INTEGER) KEYBOARD
RECEIVE price FROM (REAL) KEYBOARD
49
Activity 4
A theme park uses a program to monitor the number of people entering
and exiting the park. The maximum number of visitors at any one time
must not exceed 10,000. When the number of people in the park reaches
the maximum a ‘Park Full’ message is displayed at the entrance gate.
Children can visit the park free of charge, but each accompanying adult
must pay £2.50 admission. The program keeps track of the amount of
money collected at the gate.
1 Identify the variables needed in the program.
2 Select an appropriate data type for each variable and constant.
50
Command sequence
A command sequence is a set of instructions that the computer executes
one after another in order. Command sequences are usually combined
with loops and selection statements.
Activity 5
RECEIVE number1 FROM (INTEGER) KEYBOARD
RECEIVE number2 FROM (INTEGER) KEYBOARD
SET result1 TO number1 / number2
SEND result1 TO DISPLAY
SET result2 TO number1 MOD number2
SEND result2 TO DISPLAY
SET result3 TO number1 DIV number2
SEND result3 TO DISPLAY
Selection
The selection construct is used to create a branch in a program. The
computer selects which branch to follow based on the outcome of a
condition, for example:
51
52
Summary
• A program is an algorithm that has been converted into program code.
• Pseudo-code is far more forgiving than program code.
• The four basic data types are integer, float/real, Boolean and character.
• The data type of a variable determines the operations that can be performed on it.
• Data types don’t have to be declared in pseudo-code but it’s a good idea to do so.
• Variable and type declarations, command sequences, selection and iteration are four of the structural
components of a program.
Checkpoint
Strengthen
S1 Explain why variables are needed.
S2 Give examples of the four data types.
S3 Describe how selection and iteration are implemented in the high-level language you are studying.
Challenge
C1 Describe these structural components of a program: variable and type declarations, command sequences,
selection and iteration constructs.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try redoing the activities in this section.
53
Code readability
You should always try to ensure that any code you write is easy to read and
understand. This benefits you and anyone else who needs to understand
how your programs work.
It’s surprising how quickly you forget. Try revisiting the programs you have
already written in this chapter and make sure it is still clear to you what they
do and how they work. Imagine how much more difficult it would be to make
sense of a complex program with lots of variables, subprograms, nested
loops and multiple selection statements.
Contrary to what you might think, programming is not a solitary activity.
Programmers usually work in teams, with each programmer developing a
different part of the program. This only works if they all adopt a consistent
approach to writing readable code.
54
The programmer who produced this program did not follow good practice.
There are a number of ways that the readability of this code could be improved.
Programmers often work in teams; it is vital that any coding they share is clear and
error free
This table lists the techniques you should use to make your programs easy
to read and understand.
Technique Description
Comments Comments should be used to explain what each part
of the program does.
Descriptive names Using descriptive identifiers for variables, constants
and subprograms helps to make their purpose clear.
Indentation Indentation makes it easier to see where each block
of code starts and finishes. Getting the indentation
wrong can result in the program not running or not Activity 9
producing the expected outcomes. Investigate how to write
White space Adding blank lines between different blocks of code comments in the high-level
makes them stand out. language you are studying.
55
Activity 10
1 Rewrite this algorithm using appropriate techniques to make it easier
to read.
SET x TO 10
WHILE x >= 0 DO
IF x > 0
SEND x TO DISPLAY
ELSE
SEND ‘Blast Off’ TO DISPLAY
Calculators have a number of useful
END IF
functions
SET x TO x -1
END WHILE
Exam-style question
XtraSave is a chain of supermarkets that has recently installed self-
checkouts in all its stores.
Customers are issued with one 10p voucher for each £10 they spend and
one 5p voucher if they spend £5.
The self-checkout system uses the following algorithm to print vouchers.
56
1 Identify the line number(s) that show one example of each of these Exam tip
structural components: (3 marks) This question is testing your
a Selection ability to identify two of the main
b Iteration programming constructs. Make
c Variable initialisation sure you have identified the line
numbers in which the selection
2 Identify two techniques the programmer has used to improve the
and iteration constructs are found.
readability of the code. (2 marks)
3 Give two reasons for writing code that is easy to read. (2 marks)
Summary
• Using comments, descriptive names, indentation and white space makes code easier to read.
• Producing readable code makes it easier to understand what a program does and how it does it.
Checkpoint
Strengthen
S1 Explain why it is important to make your code easy to read.
S2 Describe four techniques that a programmer should use to make code easy to read.
Challenge
C1 Revisit the programs you have already written. Do you still understand what they do and how they work? If
not, try to improve their readability.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, look again at the table on page 55.
57
2.3 Strings
Learning outcomes
By the end of this section you should be able to:
• describe what a string is and explain what strings are used for.
• use iteration to traverse a string.
• concatenate and split strings.
Earlier in the chapter you learnt that a character is one of the four basic data
Key terms types. A character can be a single letter, a symbol, a number or even a space.
String: a sequence of characters. A sequence of characters is called a string. Although strings can contain
They can be letters, numbers, different sorts of characters, including numbers, they are all treated as if
symbols, punctuation marks or they were text.
spaces.
When a computer executes a program it needs a way of telling the
Function: a function is a difference between a string and an instruction. In most programming
subprogram that performs a languages this is achieved by enclosing strings in quotation marks (e.g.
specific task and can be used at ‘johnsmith@mail.com’, ‘10/04/15’ or ‘123’). Both single ‘ ’ and double “ ”
any point in the program. High- quotation marks are acceptable, as long as they are used consistently. In
level programming languages the example “It’s her book”, the apostrophe is treated as part of the string
have a number of useful built-in because double quotes have been used to enclose the string.
functions. You can also create
your own or use functions Strings are very useful when communicating with users. For example, asking
available in online libraries. them to enter some information into a program or displaying the output of
a program in a format that humans can read and understand.
String indexing
Each character in a string has an index number, with the first character at
position 0. You can use the index to reference individual characters in a string.
For example, if the string myText holds the value ‘Computer Science’, then
myText[1] references the letter ‘o’, myText[8] references the space and
myText[11] the letter ‘i’.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
String C o m p u t e r S c i e n c e
Table 2.3 Example string index
Length
The Edexcel pseudo-code has a built-in LENGTH function, which you can
use to find the number of characters in a string (e.g. in the case of the
string ‘Computer Science’, SET numChars TO LENGTH(myText) assigns
the value 16 to the variable numChars).
The programming language you are studying will have a function for
finding the length of a string. You should investigate what it is called and
how it works.
58
Activity 11
Design and write a program to check the length of a password. If the
password entered is less than six characters, the program should output
‘The password you have entered is not long enough’; otherwise it should
output ‘Length of password OK’.
String traversal
You can use a FOR loop to cycle through each of the characters in a string. Key term
This is known as string traversal. String traversal: using a loop to
The following algorithm prints out the word ‘monkey’ letter by letter, cycle through each character in a
displaying each letter on a separate line. string.
Type conversion
Type conversion is performed when a string and a non-string (for example
an integer) are joined together. The computer converts the non-string into a
string before joining the two strings together.
In the next example the string value stored in the variable learnerName
and the integer value stored in the variable testScore are concatenated to
form a new string, which is stored in the variable message.
59
Activity 13
1 Investigate how to concatenate and slice a string in the high-level
programming language you are studying.
2 A company wants a program to generate usernames for new
employees. Each username consists of the first four letters of the
employee’s last name and the first letter of their first name joined
together. If the employee’s last name is less than four characters in
length a letter ‘X’ is used to fill in for each of the missing characters.
Develop a program that asks the user to input their first and last
names and outputs their username.
3 Use string slicing to split this line of data into a set of seven strings.
‘23456,West Kirby,04/11/15,23.0,11.5,30,D1’
The comma indicates where one string ends and the next begins and
should be omitted when the string is sliced.
String formatting
Programming languages provide various methods of formatting strings to
improve the way they are displayed on screen. They are particularly useful
for formatting numbers, for example, by setting the number of decimal
places to display.
In the first line of this code extract the value ‘Robert Bennett’ is stored
in a memory location to which the identifier newLearner points. In the
second line, the value ‘Sally Hadwen’ is stored in a different location in
memory, and the newLearner identifier is then updated to point to this
new memory location.
60
Summary
• A string is a sequence of characters.
• Each character in a string has a unique index value denoting its position in the string. The first character in a
string has the index value 0.
• High-level programming languages have a built-in length function that finds the length of a string.
• A loop is used to traverse a string, character by character.
• Concatenation is the process of joining two or more strings together.
• Slicing is the process of extracting part of a string.
• String formatting is used to control the way text is displayed on screen.
Checkpoint
Strengthen
S1 Describe how individual characters in a string are referenced.
S2 Design an algorithm that uses a loop to traverse a string.
S3 Describe how a string and a non-string are concatenated.
Challenge
C1 Develop a program that asks the user to input a sentence and then splits it up wherever a space occurs. Each
word should then be displayed on a separate line.
61
SET winners TO []
numItems = LENGTH(firstNames)
62
Activity 15
The following code uses a linear search algorithm. Describe how it works.
Activity 16
In Chapter 1, Activity 22 you developed an algorithm that output the
highest and lowest results.
Implement this algorithm in the high-level programming language you
are studying.
High-level programming languages have a number of methods for working Did you know?
with arrays, including append (to add an item to an array), pop (to remove
an item from an array) and reverse (to reverse all the elements in an array). Arrays can be static or dynamic.
They might also have a built-in sort method. A static array has a fixed length
that must be declared when it is
created (e.g. myFriends[5]). A
Activity 17 dynamic array can grow as new
Investigate the array handling methods available in the high-level items are added. It would be
programming language you are studying. declared as myFriends[].
63
0 1 2 3
0 80 59 34 89
1 31 11 47 64
2 29 56 13 91
Top tip
Here is how to initiate a two-dimensional array in Python and Java.
Python
Scores = [[80, 59, 34, 89], [31, 11, 47, 64], [29, 56,
13, 91]]
Java
Int[][] Scores = {{80, 59, 34, 89}, {31, 11, 47, 64},
{29, 56, 13, 91}};
64
Records
We have already said that the elements of an array must all be the same Key terms
data type. In contrast, the record data structure stores a set of related Record: a data structure that
values of different data types. stores a set of related values of
different data types.
Each element in a record is known as a field and is referenced using a field name.
Field: an individual element in a
Table 2.5 illustrates how the record data structure works. Each row of the table record.
holds a set of information about a particular learner. Each column stores one
item of information about the learner – their learner number, their age, their
form etc. All the values in a column have the same data type – learnerNum
and age are integers; firstName, lastName and form are strings.
learnerNum firstName lastName age form
1 Susan Smith 15 10H
2 Ben Roberts 14 10B
3 Anita Khan 15 10A
4 Ian Wright 15 10G
Table 2.5 Example record data structure
65
Activity 19
1 A record data structure is to be used to store the details of music
albums. Give the appropriate data type for these fields:
a the title of the album;
b the name of the artist;
c the year of release;
d the genre.
2 Develop a program that uses a record structure for storing the details
of music albums. It must:
It is essential to have a way to sort
a have fields for title, artist, year of release and genre;
through the vast amounts of recorded
music available b allow the user to input the details of new albums;
c allow the user to search for an album by name and display its
details.
Exam-style question
The XtraSave chain of supermarkets collects data about product sales.
The record for each product includes:
• its unique ID (e.g. X14 or X98);
• the total number sold in each month of the year.
1 Identify a suitable data structure to store this data. (1 mark)
2 Explain why the data type you chose is most suitable to hold this
data. (3 marks)
3 Draw a diagram of the data structure that shows the amount sold in the
first three months of the year. Include data for at least two products.
(3 marks)
Exam tip
This question is testing your ability to select and design data
structures. Make sure you have:
• identified a suitable data structure to store the data and justified your
choice;
• provided the sample data requested (i.e. sales figures for two products
for the first three months of the year).
Row and column headings can be included as long as they are clearly
distinguishable from the data.
66
Summary
• A data structure is an organised collection of related elements. Arrays and records are common data structures.
• A one-dimensional array is a list of elements, each of which has a unique index value representing its position
in the list.
• A two-dimensional array is a matrix of rows and columns. Each element in the array has a unique pair of indices,
one to identify the row and one the column in which it is located.
• All the elements in an array have the same data type.
• A record consists of a collection of fields. The values stored in a record can be of different data types.
Checkpoint
Strengthen
S1 What is the index of the first element in a one-dimensional array?
S2 How does a linear search algorithm find an element in a one-dimensional array?
S3 How is an element stored in a two-dimensional array referenced?
S4 How is a nested IF used to traverse a two-dimensional array?
Challenge
C1 Develop a program for a simple address book that uses a one-dimensional array to store a set of names and
email addresses, and allows the user to search for a person by name and returns their email address.
C2 Develop a program that uses a two-dimensional array to represent a treasure map consisting of a grid of 4
rows and 4 columns.
A random number function should be used to establish the location of the treasure.
The user must hunt for the treasure by repeatedly entering the coordinates of squares. The program should
tell them when they have found the treasure and help them in their search by indicating how close they are.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try redoing the activities on pages 63, 65
and 66.
67
2.5 Input/output
Learning outcomes
By the end of this section you should be able to:
• explain the need for validation.
• write code that validates user input.
• write code that accepts and responds appropriately to user input.
• write code that reads to and writes from a text file.
User input
Most programs require some form of input either from a user or from a file.
You already know how to receive user input from a keyboard.
Validation
It is important to ensure that data entered by the user is valid, as invalid
data can cause a program to behave unexpectedly or even stop altogether.
Not surprisingly, if the data entered into a program is incorrect, the output
it produces will also be wrong. This is sometimes called the Garbage In,
Garbage Out (GIGO) principle.
Key term Any program that requires data entry should have appropriate forms of
Validation: to check that the data validation built in. But be warned, validation can’t guarantee that the data
entered by a user or from a file entered is correct. It can only make sure that it is reasonable.
meets specified requirements.
There are a number of different types of validation.
Range check
A range check is used to ensure that the data entered is within a specified
range. Study this algorithm, which checks that the number entered is
between 1 and 10.
BOOLEAN valid
SET validNum TO False
WHILE validNum = False DO
SEND ‘Please enter a number between 1 and 10:‘ TO
DISPLAY
RECEIVE number FROM (INTEGER) KEYBOARD
IF number >= 1 AND number <= 10 THEN
SET validNum TO True
END IF
END WHILE
SEND ‘You have entered: ‘& number TO DISPLAY
68
BOOLEAN validChoice
SET validChoice TO False
WHILE validChoice = False DO
SEND “Please enter either ‘Yes’ or ‘No’:” TO DISPLAY
RECEIVE userChoice FROM (STRING) KEYBOARD
Activity 21
IF userChoice=’Yes’ OR userChoice=’No’ THEN
SET validChoice TO True Design and implement a program
END IF for a user menu with these four
END WHILE options:
SEND You have selected: ‘& userChoice’ TO DISPLAY 1 display average temperature
2 display temperature range
This algorithm requires the user to enter either ‘Yes’ or ‘No’ and uses an
3 display wind speed
IF statement to validate their input. The WHILE loop is repeated until an
appropriate choice is made. 4 quit the program
The program should ask the user
A problem with this algorithm is that the user must enter their choice to select an option and validate
exactly, in order for it to be accepted. For example, entering ‘yes’ instead of their entry. Entries in upper and
‘Yes’ would not be accepted. This issue can be addressed by altering the IF in lower case should be accepted.
statement to accept more valid options. When a valid option is selected
it should be displayed on screen
IF userChoice = ‘Yes’ OR userChoice = ‘yes’ OR with an appropriate message.
userChoice = ’No’ OR userChoice = ‘no’ THEN …
Top tip
Many programming languages have functions that can be used to convert
a string to upper or lower case. This can simplify the validation of strings,
for example:
Python
if userChoice.lower() == ‘yes’ or userChoice.lower() ==
‘no’:
validChoice = True
Java
if(userChoice.toLowerCase() == ‘yes’ ||
userChoice.toLowerCase() == ‘no’) {
validChoice = true
}
69
Presence check
Another type of validation is a presence check. This simply ensures that a
value has been entered, preventing the user from leaving an input blank.
This algorithm asks the user to input their name and uses a presence check
to ensure they have entered a value. Any value will cause the loop to finish.
It will keep asking the user to input their name until they input a value.
Look-up check
A look-up check is used to test that a value is one of a predefined set of
acceptable values. The list of acceptable values can be stored in a one-
dimensional array.
This algorithm stores a list of valid form names in an array. It compares the
form name entered by the user with the values in the array.
70
Menus
Many programs feature menus that allow the user to choose which part of
the program they wish to use. The simplest way to implement a menu is to
give the user a list of numbered options to choose from and ask them to
input the number that corresponds to the option they want to select. The
user input can be validated using a range check to ensure that the option
selected is permissible. A menu could be set out like the example below.
71
This algorithm reads the data from the external text file marks.txt and
calculates the average score for the first test. It uses three predefined
functions: APPEND, SPLIT and LENGTH.
72
Stage 1
Each record in the file is read in and appended to the one-dimensional array
rawData, using a predefined function APPEND. At the end of this stage the
rawData array has four elements, each of which is a string.
Index 0 1 2 3
Elements ‘Lois Bennett, 56,45,78,32’ ‘Eloise Roberts,23,89,67,98’ ‘James Hadwen,65,43,83,45’ ‘Patrick Dua-Brown,21,43,38,29’
Stage 2
A loop is used to traverse the array rawData, element by element. A
predefined function SPLIT is used to split each element in turn into a set
of strings, using the commas as separators. Each set of strings is assigned
to the temporary array nextLearner and is then appended as a new row
to the two-dimensional array examResults. At the end of this stage the
examResults array looks like this.
0 1 2 3 4 Top tip
0 ‘Lois Bennett’ ‘56’ ‘45’ ‘78’ ‘32’ Here is an example of how to
1 ‘Eloise Roberts’ ‘23’ ‘89’ ‘67’ ‘98’ read data from a text file in
2 ‘James Hadwen’ ‘65’ ‘43’ ‘83’ ‘45’ Python and Java.
3 ‘Patrick Dua-Brown’ ‘21’ ‘43’ ‘38’ ‘29’ Python
Stage 3 file = open(‘scores.txt’,
Finally the algorithm iterates through each row of the array examResults, ‘r’)
converting the first set of marks for each learner to integers and totalling Java
them. It then calculates the average mark achieved in the first examination FileReader reader = new
and displays it on the screen. FileReader(‘scores.txt’);
Activity 26
1 Implement the algorithm shown above in the high-level programming
language you are studying. (You will need to create a text file in order
to test the program. It should consist of at least ten records.)
2 Extend your program to allow the user to choose which of the four
examinations they want the average mark for. The program should
accept valid user input only.
73
The algorithm shown above writes data to an external text file called
marks.txt. It asks the user how many learners they want to enter marks
for. A FOR loop is then used to enter a name and a set of four marks for each
learner and to write this data as a new record in the file marks.txt.
Top tip
Here is an example of how to write data to a text file in Python and Java.
Python
file = open(‘scores.txt’, ‘w’)
file.write(name, mark1, mark2, mark3, mark4)
file.close()
Java
FileWriter writer = new FileWriter(‘scores.txt’, true);
writer.write(name, mark1, mark2, mark3, mark4);
writer.close();
Data imported from a file needs to be validated in the same way as data
entered by the user is validated. Records that do not meet the validation
rules should be rejected and stored in another file (known as an error log) so
the user knows which records haven’t been imported.
74
Activity 27 Activity 28
When saving records to a file you
Amend the program you developed in Activity 26 to include a validation
can choose to write or append.
rule that ensures that only valid marks between 1 and 100 are read in
Investigate the difference
from the file. Any invalid records should be rejected and written to an
between the two and find out
error log.
how they are implemented in
Make sure your text file includes some erroneous data so that you can
the high-level programming
test that the validation rule is working.
language you are studying.
Exam-style question
The XtraSave chain of supermarkets collects data about the products it
sells. It records the department, name and price for each product. The
data is stored in a text file with one record per line. The file contains over
8,000 records. Part of the file is shown below.
Supermarkets collect a great deal of information about the products they sell
75
Summary
• Validation techniques should be used to ensure that data entered by a user or from a file is valid. They can’t
guarantee that the data is correct, only that it is reasonable.
• A range check is used to ensure that data is within a specified range.
• A length check is used to ensure that data has a length within a specified range.
• A presence check is used to ensure the user has entered some data.
• A look-up check is used to ensure the data matches one of the values in a predefined list.
• When users are required to choose from a list of options, their input should be validated to ensure that their
choice is valid.
• Large sets of data are normally stored in text files. The advantage of writing data to a file is that the data is not
lost when the program is terminated. It can be read in from the file whenever it is needed.
Checkpoint
Strengthen
S1 Describe what might happen if a program doesn’t include validation on user input.
S2 Identify a data structure suitable for storing a list of values used in a look-up check.
S3 Show how your name, date of birth and favourite colour would be stored in a text file.
S4 Explain why it is advantageous to write data to a text file.
Challenge
C1 Develop a program that:
• writes a set of employee records consisting of employee number, name and department to a text file;
• reads in the stored records from the text file;
• allows the user to search for an employee’s details;
• uses appropriate validation.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try redoing the activities in this section.
76
2.6 Subprograms
Learning outcomes
By the end of this section you should be able to:
• describe what a subprogram is.
• explain the benefits of using subprograms.
• explain the difference between global and local variables.
• explain the concept of passing data into and out of subprograms.
• create subprograms that use parameters.
• write code that uses user-defined and pre-existing subprograms.
Activity 29
1 Investigate how subprograms are implemented in the high-level
programming language you are studying.
2 Write the die function and the averageScore procedure in the high-
level programming language you are studying.
Local and global variables have different scope. A local variable is only
available within a subprogram, whereas a global variable can be accessed
anywhere within the program.
Activity 31
FUNCTION discountPrice(total)
BEGIN FUNCTION
IF total >= 50 THEN
SET saving TO total * discount
SET newTotal TO total - saving
RETURN newTotal
ELSE
RETURN total
END IF
END FUNCTION
A recursive subprogram always has at least two pathways – one that leads
to the subprogram being called again and one that leads to a terminating
condition being met. In Chapter 1 you learnt how recursion is used in a
merge sort algorithm to repeatedly divide a list in two. The terminating
condition is reached when the size of each list is one.
79
Built-in functions
Key term In addition to user-written subprograms, most high-level programming
Built-in functions: functions languages have a set of built-in functions for common tasks. These are
that are provided in most high- designed to save the programmer time, such as functions that round
level programming languages to numbers, count the number of characters in a string and generate random
perform common tasks. numbers. You have come across some of these already.
80
01 REAL area
02
03 FUNCTION areaCalc(width, height)
04 BEGIN FUNCTION
05 REAL area
06 SET area TO width * height
07 RETURN area
08 END FUNCTION
09
10 SET area TO 5
11 SET rectangleArea TO areaCalc(5,10)
12 SEND rectangleArea TO DISPLAY
81
Summary
• A subprogram is a section of code within a larger piece of code that performs a specific task. It can be used at
any point in the program.
• A function is a subprogram that returns a value to the main program.
• A procedure is a subprogram that does not return a value to the main program.
• Parameters are values that are passed to a subprogram when it is called.
• Local variables can only be accessed from within the subprogram in which they are created.
• Global variables can be accessed anywhere in the program, including inside subprograms.
• Built-in functions are functions provided in a high-level programming language to perform common tasks.
Checkpoint
Strengthen
S1 Explain the benefits of using subprograms.
S2 Explain with examples what is meant by the scope of a variable.
S3 Describe what happens when a global variable and a local variable share the same name.
S4 Give an example of a common built-in function.
Challenge
C1 Design and implement a calculator program that:
• allows the user to enter a set of numbers;
• uses separate functions to calculate the mean, mode and median;
• allows the user to select which function they want;
• uses appropriate validation.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try redoing the activities in this section.
82
Trace tables
You saw in Chapter 1 (on page 22) how a trace table can be used to identify
logic errors at the design stage.
SET table TO 5
FOR index FROM 1 TO 5 DO
SET answer TO index * table
SEND answer TO DISPLAY
END FOR
83
Activity 32
SET gender TO [‘M’, ’M’, ’F’, ‘M’, ‘F’, ‘F’, ‘M’, ‘F’,
‘M’, ‘F’]
SET length TO LENGTH(gender)
SET count TO 0
SET index TO 0
REPEAT
IF gender[index] = ‘F’ THEN
SET count TO count + 1
How many female students are there in END IF
this class? SET index TO index + 1
UNTIL index = length
This algorithm is intended to count the number of female learners in a class.
Create and complete a trace table for this algorithm.
Syntax errors
Syntax errors, as we saw earlier (page 48), occur when the grammar rules of
a programming language are not adhered to.
84
The Python program in Figure 2.4 is designed to ask the learner to enter their
favourite subject. If they enter Computer Science the program should display
‘That’s my favourite subject too’, otherwise it should output ‘Interesting. I prefer
Computer Science’. Unfortunately, a syntax error prevents the program from
executing. The rules of the language state that a colon must be placed at the
end of a line before an indented block and this is missing from the second line.
Syntax errors are normally identified when the algorithm is being implemented
in a high-level programming language because they prevent the code from
being translated.
Error summary
This table summarises the three types of error you are likely to encounter.
Type of error Description
Logic The program seems to run normally; however, there is an error in the logic of the program, which
means it does not produce the result you expect.
Syntax Syntax refers to the rules of the programming language. A syntax error means that part of the
code breaks the rules of the language, which stops it running.
Runtime An error that occurs when the computer tries to run code that it cannot execute.
85
Activity 33
RECEIVE width FROM (INTEGER) KEYBOARD
RECEIVE height FROM (INTEGER) KEYBOARD
SET area TO width / height
SEND ‘The area is: ‘ & area TO DISPLAY
1 Give a value for the variable height that would cause a runtime error.
2 Implement this algorithm in the high-level programming language
you are studying, correcting the error you identified in Question 1 and
using validation to prevent a runtime error occurring.
Activity 34
Top tip
Here are some error messages that you are likely to see in popular IDEs.
IDLE (A popular Python IDE)
• SyntaxError: invalid syntax – part of the code breaks one of the rules of the programming language
• IndentationError: expected an indented block – statements after a colon must be indented
• TypeError: Can’t convert ‘int’ object to str implicitly – trying to join a string and an integer together
• ZeroDivisionError: integer division or modulo by zero – trying to divide a value by 0
• NameError: name is not defined – referring to a variable or subprogram that does not exist
Eclipse (A Popular Java IDE)
• ; expected – each statement should end with a semicolon
• Cannot find symbol – referring to a variable or subprogram that does not exist
• Incompatible types – trying to mix data of different types, for example a string and an integer
86
It has identified that the programmer has started a string but has not
indicated where it finishes. The rules of the language state that strings must
be enclosed in speech marks.
Deciding how to test the finished program to make sure that it fully meets
the requirements can’t be left to the last moment either. As part of the
design stage, you should create a test plan listing the tests you will carry
out, the data that will be used for each test and the expected result.
Marks to grades
RECEIVE examMark FROM (INTEGER) KEYBOARD
This algorithm converts an exam mark into a grade. It should only accept
marks between 0 and 100.
87
Test no Purpose of the test Test data Expected result Actual result Action needed/
comments
1 To check correct conversion of valid 0 'FAIL'
mark 55 'D'
65 'C'
75 'B'
85 'A'
2 To check correct conversion of 0 'FAIL'
boundary mark 1, 59 'D'
60, 69 'C'
70, 79 'B'
80, 100 'A'
3 To check response to erroneous mark –5 Error message
105 Error message
Only the first four columns of the test plan table can be completed at the design
stage. The remaining columns are filled in when the program is complete.
It’s unlikely that you’ll be able to anticipate at the outset every test that will
be needed to ensure the program works as intended. The test plan is not a
fixed document. If additional tests are required they should be added to the
test plan.
If a test produces the expected result you can simply write ‘None’ in the
‘Action needed/comments’ column. If, however, that is not the case then you
should instead note what went wrong and what you did to put it right.
It is important to select suitable test data for the tests. Test data falls into
three different categories: normal, boundary and erroneous. (See also
validation tests on page 71 in this chapter.)
Normal Data that is well within the Test 1 uses normal data to check
data limits of what should be if marks are converted into grades
accepted by the program. correctly (e.g. a mark of 65 should
be converted to a grade C).
Boundary Data that is at the outer Test 2 uses boundary data to check
data limits of what should be that the program works correctly
accepted by the program. with marks at the upper and lower
boundaries (e.g. a mark of 60 and a
mark of 69 should both convert to a
grade C).
Erroneous Data that should not be Test 3 uses erroneous data to check
Table 2.6 Test data categories data accepted by the program that the program does not accept it.
88
Here is the completed test plan for the grade calculator program. As you can see, the expected result was not
produced when the program was tested with erroneous data. A range check had to be added to the program to ensure
that only marks between 0 and 100 can be entered.
Test no Purpose of the test Test data Expected result Actual result Action needed/comments
1 To check correct 0 'FAIL' 'FAIL' None
conversion of valid 55 'D' 'D'
score 65 'C' 'C'
75 'B' 'B'
85 'A' 'A'
2 To check correct 0 'FAIL' 'FAIL' None
conversion of 1, 59 'D' 'D'
boundary score 60, 69 'C' 'C'
70, 79 'B' 'B'
80, 100 'A' 'A'
3 To check response to -5 Error message 'FAIL' A range check has been added
erroneous score 105 Error message ‘A’ to ensure that only valid marks
can be added.
After you have carried out all of the tests and made all the necessary
Activity 35
changes, the program should be retested to ensure that the improvements
you have made haven’t introduced any new errors into the program. 1 Design an algorithm for a
simple guessing game. The
Evaluating programs algorithm must:
You need to be able to identify the strengths and weaknesses of your own a generate a random
programs as well as those created by other programmers. This will enable you number between 1 and 6;
to identify techniques that work well and aspects that could be improved. b ask the user to input a
number between 1 and 6;
Requirements – c reject any values outside
does the program meet
all the requirements
the acceptable range;
set at the start of d display ‘Well Done’ if the
the project? user guesses correctly or
Validation – has ‘Try Again’ if their guess is
Usability – is the
appropriate validation incorrect.
program easy to use?
been applied to user input?
2 Design a test plan for this
guessing game program.
Evaluating programs 3 Write the guessing game
program in the high-level
programming language you
Code readability – are studying.
the program – have have meaningful
loops and subprograms 4 Test your program by
been used to avoid and white space been carrying out the tests
unnecessary code used appropriately?
repetition?
you planned. Ensure you
update your test plan after
Figure 2.7 Consider the aspects shown above when evaluating a program. completing each test.
89
Summary
• Logic errors occur when there is an error in the logic of the code, causing the program to produce an
unexpected result.
• A syntax error occurs when part of the code breaks the rules of the programming language.
• A runtime error occurs while the program is running and it is asked to do something that is impossible to do.
• Trace tables can be used to manually trace the execution of an algorithm, allowing you to track the changes in
variable values.
• Before creating a program it is important to produce a test plan that outlines how the final program will be
tested to ensure it meets the requirements.
• Evaluating a program involves considering its strengths and weaknesses and identifying areas for improvement.
Checkpoint
Strengthen
S1 Describe the three types of error associated with program development and identify the stage(s) of
development at which they are most likely to occur.
S2 Describe the function of a trace table.
S3 Describe the features of an IDE that help programmers write error-free code.
S4 Describe the function of a test plan in program development.
S5 Describe what is meant by normal, boundary and erroneous data.
S6 Explain why it might be necessary to retest a program once all the planned tests are completed.
Challenge
C1 Identify the requirements for a program, design a solution and draw up a test plan for it.
C2 Design and implement the program. Conduct your planned tests and make any necessary changes to your
program, ensuring your test plan is kept up to date.
C3 Evaluate your program.
How confident do you feel about your answers to these questions? If you’re not
sure you answered them well, try completing the activities on pages 84, 86 and 89.
90
Data
3.1 Binary
Learning outcomes
By the end of this section you should be able to:
• explain why computers use binary to represent data and program instructions.
• describe how computers represent and manipulate numbers.
• convert between binary and denary whole numbers (0–255).
• perform binary arithmetic and understand the concept of overflow.
• explain why hexadecimal notation is used and be able to convert between hexadecimal and binary.
Numbers, text, graphics and sound are all represented in the same way, as a
series of 1s and 0s. The program instructions that the processor is following
allow it to interpret them in different ways.
92
Representing information
There are only two digits, 1 and 0, but to represent the symbols in text there
must be at least 52 separate items of information – 26 lower-case and 26
upper-case letters. Then there are punctuation symbols – the full stops,
question marks, etc.
When we write, we combine the letters in our alphabet to create words and
combine these into sentences to convey meaning. Key term
In a similar way the two binary digits (bits) can be combined into groups. Binary digit (bits): the smallest
unit of data that is represented in
A single bit offers two possibilities. In a graphics program it could give
a computer. It has a single binary
black (1) or white (0), for example. If we wanted colour we’d need more
value, either 1 or 0.
combinations. If two bits are used this gives four possible combinations, 00,
01, 10, 11, each of which can represent a colour, so four colours could be used.
Three bits would give eight combinations, 000, 001, 010, 011, 100, 101, 110,
111, allowing eight different colours to be represented.
• 21 = 2
• 22 = 2 * 2 = 4
• 23 = 2 * 2 * 2 = 8
• 24 = 2 * 2 * 2 * 2 = 16
Number systems
Binary is a number system based on two digits, 0 and 1.
We are more used to the denary system which has ten digits, 0 to 9, but the
binary system functions in the same way.
The difference is that the denary system works in powers of 10, whereas the
binary system uses powers of 2.
Denary system
Every digit has a place value and the one to the left has a value 10 times
higher than the one to the right.
93
Binary system
Key term The binary system has similar place values but they increase by powers of 2.
Byte: the basic combination of The following table shows the place values for the digits in a byte.
bits used to represent an item
Place values 27 26 25 24 23 22 21 20
of information. A byte typically
consists of 8 bits. 128 64 32 16 8 4 2 1
Worked example
Convert denary 213 to binary. Use the table and fill it in as you calculate
the equivalent.
Place values 27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
Place values 27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
1
The remainder is 85.
94
Step 2: Is 85 greater than or equal to 64? Yes, therefore place a ‘1’ in this column.
Place values 27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
1 1
The remainder is 21.
Step 3: Is 21 greater than or equal to 32. No, therefore place a ‘0’ in this column.
Place values 27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
1 1 0
The remainder is still 21.
Step 4: Is 21 equal to or greater than 16? Yes, therefore place a ‘1’ in this column.
Place values 27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
1 1 0 1
The remainder is 5.
Step 5: Is 5 equal to or greater than 8? No, therefore place a ‘0’ in this column.
Place values 27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
1 1 0 1 0
The remainder is still 5.
Step 6: Is 5 equal to or greater than 4. Yes, therefore place a ‘1’ in this column.
Place values 27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
1 1 0 1 0 1
The remainder is 1.
Step 7: Is 1 equal to or greater than 2? No, therefore place a ‘0’ in this column .
Place values 27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
1 1 0 1 0 1 0
The remainder is still 1.
Step 8: Is 1 equal to or greater than 1? Obviously, they are equal and Activity 2
therefore place a ‘1’ in the last column.
Calculate the binary equivalents
Place values 27 26 25 24 23 22 21 20 of the following denary numbers.
128 64 32 16 8 4 2 1
a 69
1 1 0 1 0 1 0 1 b 193
Therefore denary 213 is represented by binary 11010101. c 239
95
Binary arithmetic
Binary numbers can be manipulated in the same way as denary ones.
Denary addition
When addition is performed in denary, a ‘carry over’ is used if the result is
greater than 9.
Worked example
369 + 733
Place 103 102 101 100
values 1000 100 10 1
Carry over 1 1 1
3 6 9
7 3 3
Total 1 1 0 2
100 + 300 + 700 = 1100 10 + 60 + 30 = 100 9 + 3 = 12
The 1000 is carried over The 100 is carried over. The 10 is carried over
leaving 100. leaving 2.
Binary addition works in the same way but a ‘carry over’ is needed if the
result is greater than 1.
Binary addition
Worked example
11010110 + 01100111
Place 27 26 25 24 23 22 21 20
values 128 64 32 16 8 4 2 1
Carry over 1 1 1
1 1 1 0 1 0 1 1 0
0 1 1 0 0 1 1 1
Total 0 0 1 1 1 1 0 1
128 + 128 = 256 64 + 64 = 128 No No No 4 + 4 + 4 = 12 2+2=4 No
This should be The 128 is carry carry carry The 8 is The 4 is carry
carried over. carried over. over over over carried over carried over
needed. needed. needed. leaving one 4. over. needed.
96
We are adding eight-bit numbers and this has caused a problem. All eight
bits have been used and the 1 that was carried over in the last column
has nowhere to go – it has been carried out. Therefore the result of the
calculation would be wrong. This is called an overflow error.
This condition occurs when a calculation produces a result that is greater
than the computer can deal with or store.
When this occurs, the processor is informed that an error has occurred.
97
Also using this method zero could be both positive and negative.
Positive zero 0 0 0 0 0 0 0 0
Negative 1 0 0 0 0 0 0 0
zero
Two’s complement
The most common method used to represent signed integers in modern
computers is two’s complement.
This method works on the principle above – the result of any number added
to its negative equivalent should be zero.
Number 1 1 1 0 0 1 0 0
−28
Flip 0 0 0 1 1 0 1 1
Add 1 to 0 0 0 1 1 1 0 0
flipped value
The result is denary 28.
98
Place 27 26 25 24 23 22 21 20
values −128 64 32 16 8 4 2 1
1 1 1 0 0 1 0 0
Worked example
Convert −69 to an eight-bit two’s complement binary number.
Place 27 26 25 24 23 22 21 20
values 128 64 32 16 8 4 2 1
69 0 1 0 0 0 1 0 1
Flip 1 0 1 1 1 0 1 0
Add 1 1 0 1 1 1 0 1 1
So denary −69 is 10111011 in two’s complement binary.
We can check this using the place value method.
−128 + 32 + 16 + 8 + 2 + 1 = −128 + 59 = −69
28 0 0 0 1 1 1 0 0
−28 1 1 1 1 0 1 0 0
Result of addition 0 0 0 0 0 0 0 0
99
Worked example
Add 28 and −5.
This is more difficult. The result should be 23.
The two’s complement representation of −5 is
5 0 0 0 0 0 1 0 1
Flip 1 1 1 1 1 0 1 0
Add 1 1 1 1 1 1 0 1 1
Adding binary 28 to binary −5 gives
0 0 0 1 1 1 0 0
1 1 1 1 1 0 1 1
0 0 0 1 0 1 1 1
The result is 00010111, which is the binary equivalent of 23.
100
Binary shifts
In denary when we want to multiply a number by ten we move each number
one place value to its left and add a 0 to the end.
For example, 13 * 10 = 130.
Place values 27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
0 0 0 1 0 1 0 0
Result of shift 0 1 0 1 0 0 0 0
The product is (64 * 1) + (16 * 1) = 80 in denary as expected.
If we divide the same number by 4 (22) there would be two shifts to the
right. This time the right-most bits drop off the end and are replaced by 0s
at the left.
Place values 27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
0 0 0 1 0 1 0 0
Result of shift 0 0 0 0 0 1 0 1
The result is (4 * 1) + (1 * 1) = 5 in denary as expected.
101
a 00111010 * 23. 0 0 1 0 0 0 0 1
b 10011101 / 24. Result of shift 0 0 0 1 0 0 0 0
The result is 00010000, which is 16, but the correct result should be 16.5.
Worked example
Calculate the product of −36 * 2
−36 in two’s complement format is:
36 0 0 1 0 0 1 0 0
Flip 1 1 0 1 1 0 1 1
Add 1 1 1 0 1 1 1 0 0
Leaving the MSB in place and shifting the others to the left will produce
10111000
−72 1 0 1 1 1 0 0 0
Flip 0 1 0 0 0 1 1 1
Add 1 0 1 0 0 1 0 0 0
102
Our denary system has only digits for 0 to 9, so the higher hexadecimal
digits are represented by upper case letters. Key term
Hexadecimal: a base-16 number
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
system. There are 16 digits and
0 1 2 3 4 5 6 7 8 9 A B C D E F
the place values increase in
The following diagram shows how to convert binary to hexadecimal. powers of 16.
103
Exam-style questions
1 a Add together the following 8-bit numbers. (1 mark)
01011001
11100111
____________
b Identify the problem that this addition has created. (1 mark)
2 a Carry out a 3-place logical right shift on the following binary number. (2 marks)
10010011
b Explain the effect of performing a right shift on a binary number. (1 mark)
c Describe the steps needed to convert the binary number 11101110 into a hexadecimal one and show
the result. (4 marks)
104
Exam tips
1 a This is a straightforward question. Carry out the calculation and
write the result in the space provided. Remember to carry over if the
addition of each pair of digits is greater than 1.
b This just requires a one or two word answer.
2 a Again carry out the shift and write the result.
b Here you have to explain what effect the shift will have. You could
say that it is equivalent to multiplying the number by…
c A longer answer is required. You should show stage by stage how
the conversion is carried out. For example, you could start by saying
what the 8-bit binary number is divided into. You should set out the
explanation clearly and it could be in the form of a diagram.
Summary
• In a computer all data and programming instructions are represented by streams of 1s and 0s.
• Place values in the binary system increase by powers of 2.
• An overflow error occurs when a computer attempts to handle a number that is too large (e.g. a number
requiring 9 bits instead of 8 in an 8-bit processor).
• To multiply a binary number by powers of 2, you can use left shifts.
• To divide a binary number by powers of 2, you can use right shifts.
• Hexadecimal is used to represent binary numbers because it is easier for humans to remember and use.
• Binary, denary and hexadecimal numbers can be converted from one to the other.
Checkpoint
Strengthen
S1 Carry out the following addition: 11011001 + 10010010.
S2 Perform the following calculation in binary: 25 + (−13).
S3 Multiply the unsigned integer 01001001 by 23.
S4 Convert 11001101 to denary and hexadecimal.
Challenge
C1 Explain in your own words why binary is used to represent data and program instructions in computer systems.
C2 Explain the difference between a logical right shift and an arithmetic right shift.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try the following activities again:
105
Representation of text
When you press a key on the keyboard a stream of 1s and 0s are input to
the processor. The computer cannot recognise any characters such as ‘c’,
‘@’ or ‘D’, only a combination of 1s and 0s. The defined list of characters
Key term recognised by a computer’s hardware and software is known as its
Character set: the defined character set.
list of characters recognised
Obviously, an industry standard is needed or data will not be
by a computer’s hardware and
interchangeable between computers from different manufacturers.
software.
This standard is provided by the American Standard Code for Information
Interchange, or ASCII code.
Originally the ASCII code consisted of 7 bits and so 128 characters could
be represented.
All of the lower and upper case English characters, punctuation marks
and control actions such backspace, shift on, shift off and carriage return
were represented.
The printable characters of the 7-bit ASCII code are shown in Figure 3.3. The
Activity 8
codes for control actions (mentioned above) are not shown.
Using the table in Figure 3.3,
write down the ASCII codes for If you look at the table you can see that the codes are grouped according
the characters in the following: to function. Important groups are 65 to 90 for the upper case alphabetic
characters and 97 to 122 for their lower case equivalents. The numeric
ASCII code. characters 0 to 9 are represented by codes 48 to 57.
106
D B C D B C D B C D B C
32 00100000 space 57 00111001 9 82 01010010 R 107 01101011 k
33 00100001 ! 58 00111010 : 83 01010011 S 108 01101100 l
34 00100010 “ 59 00111011 ; 84 01010100 T 109 01101101 m
35 00100011 # 60 00111100 < 85 01010101 U 110 01101110 n
36 00100100 $ 61 00111101 = 86 01010110 V 111 01101111 o
37 00100101 % 62 00111110 > 87 01010111 W 112 01110000 p
38 00100110 & 63 00111111 ? 88 01011000 X 113 01110001 q
39 00100111 ‘ 64 01000000 @ 89 01011001 Y 114 01110010 r
40 00101000 ( 65 01000001 A 90 01011010 Z 115 01110011 s
41 00101001 ) 66 01000010 B 91 01011011 [ 116 01110100 t
42 00101010 * 67 01000011 C 92 01011100 \ 117 01110101 u
43 00101011 + 68 01000100 D 93 01011101 ] 118 01110110 v
44 00101100 , 69 01000101 E 94 01011110 ^ 119 01110111 w
45 00101101 - 70 01000110 F 95 01011111 _ 120 01111000 x
46 00101110 . 71 01000111 G 96 01100000 ` 121 01111001 y
47 00101111 / 72 01001000 H 97 01100001 a 122 01111010 z
48 00110000 0 73 01001001 I 98 01100010 b 123 01111011 {
49 00110001 1 74 01001010 J 99 01100011 c 124 01111100 |
50 00110010 2 75 01001011 K 100 01100100 d 125 01111101 }
51 00110011 3 76 01001100 L 101 01100101 e 126 01111110 ~
52 00110100 4 77 01001101 M 102 01100110 f 127 01111111 DEL
53 00110101 5 78 01001110 N 103 01100111 g
KEY: D = denary
54 00110110 6 79 01001111 O 104 01101000 h
B = binary
55 00110111 7 80 01010000 P 105 01101001 i
C = character
56 00111000 8 81 01010001 Q 106 01101010 j
Analysing a string
To a computer a string is just a stream of binary codes that represent the
characters. Programming languages have functions that will return the
denary equivalents of those codes and vice versa.
The Edexcel pseudo-code does not have these functions built in, but in the
Python programming language they are ord() and chr().
107
Using the ord() function the ASCII codes of the characters can be found
when traversing a string.
By using the chr() function you can build up a string using characters
entered by their denary codes.
The following algorithm will allow you to enter ten numbers which will be
converted to characters and appended to the string.
The ord() function needs to take the character as a parameter and return
its denary code.
108
FUNCTION ord(character)
BEGIN FUNCTION
SET arrayAscii TO [[“!”][33], [“””][34],…[“z”][122]] #A two-dimensional array
containing all of the
characters with their denary
codes.
FOR index FROM 0 TO LENGTH(arrayAscii) – 1 DO
IF character = arrayAscii[index, 0] THEN
code = arrayAscii[index, 1]
END IF
END FOR
RETURN code
END FUNCTION
Activity 9
Create a function in pseudo-code that will return the character from the
denary code entered.
The first number indicates the number of pixels along the width of the
image and the second is the number in the height.
Thus a 640 × 480 image is made up of 307, 200 pixels and a 4064 × 2704
image comprises 10,989,056 pixels.
109
There are different numbers of pixels per square inch in the two images.
Key term
Each pixel in the 100 × 66 image is a larger size than those of the
Resolution: the number of 4288 × 2848 image. The greater the number of pixels within a given area the
pixels per inch when the image higher the resolution and the more detail shown. An image of size 100 × 66,
is displayed (for example, on a when displayed in the same area as one of 4288 × 2848 pixels, will have a far
monitor or on paper). lower resolution as there are far fewer pixels. The lower the resolution the
less the amount of detail seen.
For example, if one bit is used for each pixel, then there will be only two
colours – black and white.
The following diagram shows an encoding system with one bit used to
represent each pixel.
110
If 0 represents black and 1 white, the letter ‘H’ could be encoded with the
following bit pattern.
11011011
11011011
11011011
11000011
11000011
11011011
11011011
11011011
The complete image consists of 64 pixels and because 1 bit is used for each
square, the size of the image is 64 bits or 8 bytes. Key term
As the colour depth increases so too does the number of colours that can Colour depth: the number of bits
be represented. used to encode the colour of each
pixel.
If 1 bit is used then the number of colours is 21 or 2.
The current standard represents the colour of each pixel in 24 bits. There are
8 bits for each of the red, blue and green primary colours. Did you know?
As the size of the image and its
There are therefore 256 variations of each primary colour contributing to colour depth increases so too
the overall colour of the pixel and that gives 256 × 256 × 256 or 16,777,216 does the amount of data that has
different colours. to be stored and manipulated.
This standard allows lifelike images, so it is often referred to as true colour. If you were visiting a website
with 4288 × 2848 24-bit images
This shape is filled with a colour named ‘Alice blue’. then, unless you had a very fast
connection, you would quickly
In binary you would have to enter 111100001111100011111111 every time
become frustrated as you waited
you wanted to use it.
for them all to download. Lower
However, as mentioned in section 3.1, hexadecimal comes to our rescue. resolution images have to be
Using hexadecimal you would only have to enter #F0F8FF. used as a compromise between
quality and file size.
File sizes
The file size for a bitmap image is calculated by finding the total number of Activity 11
pixels and multiplying that by the number of bits used to represent each
pixel or Calculate the file sizes of the
following images and express the
Width × Height × Colour depth sizes in bits, bytes and megabytes.
The file size of the left-hand image on page 110 is 1 A 256 colour image with a size
of 640 × 480 pixels.
4288 (width) × 2848 (height) × 24 (bit colour depth) = 293,093,376 bits 2 A true colour image with a
That is 36,636,672 bytes or almost 37 megabytes. size of 640 × 480 pixels.
111
Representation of sound
All sounds are caused by vibrations. As objects such as our vocal cords or
guitar strings vibrate backwards and forwards they push the air molecules
alongside them, sending a wave of compressed molecules through the air.
When these compression waves, or sound waves, reach our ears they set up
vibrations in tiny sensory hairs in the inner ear. This sends nerve impulses to
the brain, which interprets them as the sounds we hear.
Figure 3.5 Sound waves travelling through the air from a vibrating bell
Figure 3.6 illustrates the different waves for loud and quiet and high- and
low-pitched sounds.
Amplitude
Louder Quieter
Figure 3.6 The bigger the wave, the louder the sound; the greater the frequency of
the waves, the higher the pitch
112
When the disc is played the movements of another stylus through the
groove are converted back into voltage changes. After amplification, these
cause the diaphragm of a speaker to vibrate, reproducing the original sound.
These ‘snapshots’ of the sound are called samples and the process of taking
Key term
them is called sampling.
Sampling: taking measurements
The following diagram shows an analogue sound wave being sampled. of the sound wave at regular but
distinct intervals of time (e.g.
44,100 samples per second).
Each line represents a new sample. The time between each line/sample represents the
sampling period, which equals 1/44,100 of a second, for a CD with a sampling rate of 44.1 kHz.
Figure 3.7 How a sound wave is sampled digitally
113
Sample rate
This is the number of samples taken per second – the higher the rate the
higher the fidelity.
A sampling rate of 44,100 per second (44.1 kHz) is used for CDs and of
96,000 Hz (96 kHz) for Blu-ray™ audio.
Original sound wave Wave after sampling • Using 8 bits allows 256 (28)
gradations to be measured.
• 16 bits allows (216) 65,536, and
Low sample
rate • 24 bits allows (224) over 16.7
million.
114
Exam-style questions
1 Explain what is meant by a ‘pixel’. (2 marks)
2 The following diagram shows a black and white image consisting of 36 pixels.
a Explain why 36 bits are needed to represent the pixels in the image. (2 marks)
b Write the bit pattern needed to represent these pixels. (4 marks)
c State the number of bits per pixel that would be needed if the image was 16 colours rather than 2. (1 mark)
3 Jane is using a computer program to record herself playing the guitar.
a Describe how the program converts her music into a file. (3 marks)
The software allows her to increase the sample rate.
b Describe two effects this will have on her recording. (2 marks)
c She eventually records the music in mono with a sample rate of 44.1 kHz and a bit depth of 16. If the
recording lasts for two minutes, calculate the size of the file produced. Express your answer in
megabytes. (2 marks)
115
Exam tips
1 This simply requires a one-sentence definition.
2 a You need to explain why 1 bit will be needed to encode the colour information of each pixel in a black and
white image.
b You should show the bits required for each line of the image. Don’t forget to state the type of bit used to
represent each colour.
c You could represent this in powers of 2.
3 a This requires a longer answer. You should start with describing how the sound reaches the recording device
and then how it is converted into a digital format.
b Two effects are needed – quality and size.
c This requires you to apply the formula to calculate file size. Read the question carefully so that you use the
correct figures in the calculation and remember that sample rate is defined as samples per second.
Summary
• All data is represented by bit patterns – sequences of 1s and 0s.
• The ASCII code represents text characters as patterns of 7 or 8 bits.
• A pixel is the smallest single point of colour in a graphic image.
• The amount of colour information used for each pixel is called the ‘bit depth’.
• The file size of a bitmap image is calculated by multiplying the number of pixels in the width of the image by
the number in the height and then multiplying by the number of bits used to encode each pixel.
• Digital sound recordings are produced by sampling analogue sound waves.
• The sample rate and the bit depth affect the fidelity of the recording and also the file size.
Checkpoint
Strengthen
S1 Represent the following sentence using the denary ASCII codes for the characters:
Data is represented as bits.
S2 An image is described as being 640 × 480. If it has a bit depth of 24, calculate the size of the file.
Challenge
C1 In your own words explain why some digital images are clearer and display more detail than others.
C2 Explain the factors influencing the file size of a digital audio recording.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try the following activities again:
116
Data storage
All data consists of bits – 1s and 0s. In section 3.2 (on page 111), you
calculated the size of an example image file as being 293,093,376 bits or
36,636,672 bytes, and the size of an example sound file (on page 115) to be
211,680,000 bits or 26,460,000 bytes – a typical byte is equivalent to 8 bits.
When converting binary to hexadecimal in section 3.1 (on page 104), you
used a unit called a ‘nibble’, which is equal to 4 bits.
This is because of the method used to calculate higher units. Ubuntu uses
SI units (International System of Units), which treats ‘kilo’ as meaning 1,000 Key terms
as it does when measuring distance and weight. This method is known as Decimal prefix: multiplies a unit
decimal prefix because it uses powers of 10 (e.g. 103 for 1,000). by powers of 10.
The other method, the binary prefix, uses powers of 2, so ‘kilo’ is defined as Binary prefix: multiplies a unit
210 or 1,024. by powers of 2.
Therefore, every higher unit is 1,000 times larger than the previous one with
decimal prefix, whereas in binary prefix it is 1,024 times larger.
117
Activity 13
1 A hard disk is described as having a storage capacity of 1.5 TB.
What is this in:
a gigabytes
b megabytes
c kilobytes
d terabytes?
2 An image file has a size of 363,143,213 bits. What is its size in
gigabytes?
Data compression
Files have to be stored somewhere, for example on local or remote hard disk
drives or on USB drives. If they are graphics or audio files, then these files
can be very large.
118
Millions of image and audio files are uploaded and downloaded each day.
There are many benefits to the files being made as small as possible. The
users don’t waste time while their data is being transferred or saved and the
providers can store them in as little space as possible. Key terms
That is why file compression is so important. Compressed files use Compression: changing the
less network bandwidth to upload and download, there is less internet format of a data file so that the
congestion and it makes possible the streaming of video and audio files. size of the file becomes smaller.
Lossless compression:
Compression algorithms are used to make the files as small as possible.
compressing a file in such a way
There are two types of compression – lossless and lossy compression.
that it can be decompressed
Lossless compression without any loss of data.
When lossless compresion is used, no data is lost and the original file can Lossy compression:
be restored. compression where some of the
data is removed; the original file
It is especially useful for files where the same data occurs many times, for
cannot be restored when the
example text, where there are relatively few different items of data (i.e.
lossy file is decompressed.
character and punctuation symbols). In a page of text there are many words
that are used more than once. This is referred to as redundancy.
cccmmmmmsssssdddcccccc
would be represented by
3c5m5s3d6c
If one byte is used to encode each letter in the original uncompressed text,
22 bytes in total are required.
In the RLE version, one byte is used for the letter and one byte is used for
the number, making a total of only 10 bytes.
Obviously, this method is only effective where there are strings of the same
data. If the original data is very different, without repeating strings, then the
encoded version would have a greater file size.
For a black and white image, like the one in Figure 3.10, compression using
RLE would be effective. A colour image in which there are very short runs of
different colours would not be encoded as effectively.
119
To demonstrate run-length encoding, the letters ‘w’ and ‘b’ are used to
represent the white and black pixels instead of 1s and 0s
The file size for this character has been reduced from 64 to 48 bytes. It could
be reduced even further if the number 1 is omitted where there is a run of
only one. The algorithm could be easily adapted.
Activity 14
Show the effect of applying run-length encoding to the following graphic.
120
E
YES
121
Activity 15
1 Six variables are used in the flowchart. List the variables and the data
items they represent.
2 Five items are labelled A to E in the flowchart. Match each item below
with the statement that describes it (e.g. A3).
1 This checks to see if the end of the input text has been reached.
2 If there is only one character it and its run length are added to
the output file.
3 This adds the character and its run length to the output string.
4 This checks to see that some text has actually been entered.
5 This checks to see if the next character in the input string is the
same as or different from the one that is being checked.
Lossy compression
Lossy compression decreases the file size by deleting some of the data. The
original file therefore cannot be re-formed entirely when it is decompressed,
so it cannot be used with text or program files.
It can be used for bitmap image and audio files where we often cannot
notice that data has been removed.
Bitmap images
Digital images can comprise millions of pixels each with a 24-bit depth. They
result in very large files. However, many of the minute differences in colour
are wasted on us. Our eyesight is not capable of distinguishing these small
differences.
A lossy compression algorithm analyses all of the data in the image and
when it finds areas with minute differences it gives them the same colour
values. It can then rewrite the file using fewer bits.
The images on the next page demonstrate JPEG compression. The one on
the left has a file size of 6 MB, while the compressed version on the right is
only 350 KB. They both have the same number of pixels, but the algorithm
made pixels with similar colour values the same. The size has been reduced
over 17 times, but to our eyes there is little difference in detail and clarity.
122
Digitally compressing an image makes little difference to our eyes but huge savings in file size
Audio files
Uncompressed audio files that contain all of the sampled sound data are
saved in Waveform Audio (WAV) format. A three-minute recording typically
has a file size of about 30 MB.
Much of the data in an audio file encodes tones and frequencies that our
ears cannot hear and small differences in volume and frequency that they
cannot distinguish.
The most commonly used algorithm compresses them and encodes them
as MPEG-1 audio layer 3 files or, as they are commonly called, MP3 files, by
removing the redundant or superfluous data.
123
Summary
• File sizes are measured in kilobytes, megabytes, gigabytes and terabytes.
• Each unit is 1,000 (103) times larger than the previous one.
• Files can be compressed using lossy and lossless compression algorithms.
• In lossy compression some of the data is lost.
• In lossless compression none of the data is lost.
• Run-length encoding is an example of lossless compression.
Checkpoint
Strengthen
S1 List the units used to measure file size in ascending order starting at ‘bit’.
S2 Convert 2.3 terabytes into bytes.
S3 Encode the following using run-length encoding
aaabbaaaaaaaaacccddd
and calculate the file size of the original and the encoded versions.
Challenge
C1 Explain why it is difficult to compress digital images using lossless compression algorithms.
C2 In your own words explain why an audio file compressed using a ‘lossy’ algorithm often sounds the same as
the original uncompressed version.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try the following activities again:
124
3.4 Encryption
Learning outcomes
By the end of this section you should be able to:
• explain why data encryption is needed.
• explain how a Caesar cipher algorithm works.
When we send sensitive information such as bank or credit card details over
an internet connection we check (or should check) the URL to ensure that
it starts with ‘HTTPS:’, denoting a secure connection. Internet connections
are insecure and messages are easily intercepted. The secure connection
ensures that the transferred data is encrypted and is read only by the
intended recipient.
Symmetric encryption
This method encrypts and decrypts a message using the same key.
This is the method used by an HTTPS connection. The client contacts the server
and they establish a secure connection. The client generates the key to be used
using an agreed algorithm and sends it to the server using the server’s public
key (asymmetric encryption). The server decrypts it using its private key and
then they both use the common key for their secure transmissions.
125
With a key of of –3 the letters of the alphabet shift three places to the left,
so the word ‘SECRET’ becomes ‘PBZOBQ’.
Notice how the letters at the end move to the start of the alphabet.
126
All this seems straightforward, but there are some more sub-problems.
Input ‘e’ for encryption
or ‘d’ for decryption
• Your program will need to check that the user input – the key and the
message – has actually been entered.
Is NO
• The algorithm will have to start at the first character and move to the input ‘e’
or ‘d’?
last (i.e. it must traverse the string), therefore your program will have
to find the length of the message. YES
• Some of the letters may be entered in upper case and some in lower Input shift to be used + or – and a
number of places (between 1 and 25)
case. Your program should be able to cope with this and encode or
decode them.
NO
• Your program will have to leave any characters such as spaces, Is key
appropriate?
commas and full stops as they are.
YES
Input mesage to be
The ‘biggest problem’ encrypted or decrypted
NO
End
127
For example, functions are used Return encrypt or decrypt Get method
to:
M
• ask the user if they want to A
I Return key Get key
encrypt or decrypt. N
• ask the user for the key. P
R Return message Get message
• ask the user for the message.
O
• carry out the encryption or G
R
decryption. A
Return encrypted message Encrypted message
M
Each of these functions will return
values that are used by the main Return decrypted message Decrypt message
program.
Figure 3.15 An example of structured programming
Here is the main program in pseudo-code – the subprograms are not included.
SET run TO 1
WHILE run = 1 DO #A WHILE loop is started. It will continue until the variable
‘run’ does not contain the value 1.
SET method TO findMethod() #This calls the function for the user to ask for encryption
or decryption.
SET key TO findKey() #This calls the function for the user to enter the key.
SET message TO getMessage() #This calls the function for the user to enter the message.
IF method == “e” THEN
SET newMessage TO encrypt(message, key) #Functions to encrypt or decrypt are called.
ELSE
SET newMessage TO decrypt(message, key)
END IF
SEND newMessage TO DISPLAY #The encrypted or decrypted message is displayed.
SEND “Please enter x to quit or any other key to continue” TO DISPLAY
RECEIVE response FROM (CHARACTER) KEYBOARD
IF response = “x” then #The user is asked if they want to encrypt or decrypt
again. If not the variable ‘run’ is changed from 1 and
the loop will stop.
SET run TO “x”
ELSE
SET run TO 1
END IF
END WHILE
128
Activity 17
Create and test, in the language you are studying, a program to encrypt
or decrypt a message using a key entered by a user.
Exam-style questions
1 Encrypt the following word using the Caesar cipher with a key of +6.
Computer (3 marks)
2 Decrypt the following word using the Caesar cipher with a key of +3.
Pzfbkzb (3 marks)
Exam tip
These questions do not require you to give an explanation or description.
They are asking you to apply the Caesar cipher algorithm to encrypt
and decrypt. You should transpose the characters to the left or right as
instructed by the key.
Summary
• Encryption is needed to ensure that sensitive data is secure and is read only by the intended recipient.
• Encryption keys are used to encrypt the data into cipher text and decrypt it back into normal (plain) text.
• Asymmetric encryption uses two different keys and symmetric encryption uses the same one.
• In the Caesar cipher algorithm the letters of the alphabet are shifted a set number of places as defined by the key.
Checkpoint
Strengthen
S1 Encrypt the sentence below using the Caesar cipher with a key of +9:
Data should be encrypted.
S2 Explain how a Caesar cipher algorithm works.
Challenge
C1 Use a diagram to demonstrate that a shift of +5 followed by a shift of –2 is the same as a shift of +3 when
applied to the word ‘gold’.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try the following activity again.
129
3.5 Databases
Learning outcomes
By the end of this section you should be able to:
• describe the characteristics of structured and unstructured data.
• decompose, organise and manage data in a structured database.
• describe and use the following:
o tables
o records
o fields
o relationships
o keys.
People have been recording and storing data since writing systems were
invented thousands of years ago.
Database: an organised store of A database is a data store in which the data is organised in an ordered way
data. so that information is quickly retrieved when the data is being searched or
queried.
Computer databases
Computers are ideal for storing and manipulating data because they
can search and sort the data far more quickly than a human can.
Storing the data in a structured way makes searching it much faster and
more efficient.
130
A record
In the table, the same items of information are being stored about each
learner. These items of information are called fields. All the information
about a particular learner is called a record. Each row of the table represents
one record.
131
Primary key: a unique identifier This identifier is called the primary key.
for each record in the table. It is
In the example table for the school database, none of the fields will
usually a field that is guaranteed
necessarily have a unique value. There could be many learners with the same
to hold unique information for
first name, surname, date of birth, year or tutor group. It would be a strange
each record.
school if the year field had a unique value – it would mean there is only one
learner in each year.
Each record can now be uniquely identified through the UPI field, its
primary key.
This table will store the details of all the learners in the school. It can be
searched to find specific individuals or all the learners in a particular tutor
group, for example, and print out the list.
Now imagine that the database is such a success that the school would like it
to manage the lending of books from the school library.
It will now need to store all of the learner information plus the details of the
books they have borrowed.
Care is needed when designing how this data will be organised and stored.
Data redundancy
The data could be organised in the following way, again with the UPI field
being the primary key.
UPI first name surname date of birth year tutor book name author borrow return
group date date
01560 Stepen Jackson 12/5/2004 7 R1 Computing J. Smith 1/10/15 15/10/15
01356 Catherine Byrne 13/1/2002 9 A1 Gardening F. Green 3/10/15 17/10/15
01356 Catherine Byrne 13/1/2002 9 A1 Plants F. Green 3/10/15 17/10/15
132
The database illustrates some serious flaws in the way in which the
data is being stored.
• The same data is being entered more than once – UPI, first name,
surname, year and tutor group have to be written every time a
book is borrowed. Also the same book information will have to
be rewritten when it is borrowed by another person. This is time
wasteful. Having the same data entered more than once is called
data redundancy.
• When the same data is entered more than once, errors can occur. In
the second version of the database Stephen is misspelt as ‘Stepen’.
This is known as data inconsistency.
You could solve this problem by adding lots of new fields so that the
learners can borrow as many books as they want.
UPI first surname date year tutor Book Author_1 borrow return book author_2 borrow return
name of group name_1 date_1 date_1 name_2 date_2 date_2
birth
There may be too many fields for some learners and not enough for others.
For many learners some will never be used.
Not enough thought has been given to creating the structure of the
database. This illustrates the importance of database design.
133
The ‘book number’ field is the primary key. A new number is issued when a
new book is bought for the library.
There are now three tables, but they are linked. The primary key fields from
the Learner and Book tables are both in the Borrowing table.
tblBook
UPI* title
author
surname tblBorrowing
return date
The primary key fields in tblLearner and tblBook are indicated with asterisks.
tblLearner and tblBook are linked to tblBorrowing because their key fields
are included in it. tblBorrowing is said to be a link table.
Key terms When a key field from one table is included in another it is called a
Foreign key: a field in one table foreign key.
that uniquely identifies records in
By linking the tables we have structured the data to create a relational
another table (i.e. it is a primary
database.
key in another table).
Relational database: a database Whenever a learner borrows a book, their UPI, the book number and
that allows data elements in one the borrowing and return dates are entered into a new record in the
table to be related to any piece Borrowing table.
of data in another table as long
This allows the librarian to create a search or query, linking the three tables,
as both tables contain a common
to find overdue books and the learners who have borrowed them.
element.
134
If a book is overdue, all the librarian has to do is identify the learner from
their UPI by looking in the Learner table and the book from its book number
by looking in the Book table. They can send an overdue letter to the learner
stating the book borrowed and when it should have been returned. They can
also work out the fine.
Compound keys
A key field has not been identified for tblBorrowing.
There is still a problem. What if a learner wants to reread a book? The database
will not allow this as the UPI and book number fields will contain the same data
as in the previous record.
Table relationships
Each learner appears only once in tblLearner, but can appear many times
in tblBorrowing.
Each book appears only once in tblBook, but also can appear many times
in tblBorrowing.
Extend your knowledge
There are therefore ‘one-to-many’ relationships between the Learner and
Book tables and the Borrowing table. The process of structuring
the data to create relational
This is shown as follows.
databases is called normalisation.
tblLearner tblBorrowing tblBook Find out about first, second and
third normal forms.
Figure 3.18 A one-to-many table relationship
135
Activity 19
Catherine is creating a database. She wants to store details of all her books and also information about their
authors.
BOOKS
Book_ID*
Title
Publisher
Date_Published
Author_Surname
Author_First_name
Author_DOB
Author_Nationality
When she starts to enter data, she creates records as shown below.
136
Exam-style questions
A sports centre uses a database to store details of their members.
MEMBER, SPORT and TEAM are three of the tables used in the database.
1 The data for the first three members in the MEMBERS table is shown below.
MemberNumber Surname FirstName DOB SportCode TeamCode
00033 Abbot Catherine 13/1/1980 HCK HCK_A
00045 Ali Mohammed 10/10/1995 FBL FBL_C
00069 Anderson Patricia 6/6/1994 HCK HCK_C
a State the field that is used for the primary key in the MEMBER table. (1 mark)
b Explain why your chosen field is most likely to be used as the primary key. (2 marks)
2 The database also contains a SPORT and TEAM table.
Explain why SportCode and TeamCode have been included in the MEMBER table. (2 marks)
137
Exam tips
1 You should explain what is meant by a primary key and why the field you
choose is the only one that could contain unique data.
2 You should explain how relational databases function and how tables can
be linked using foreign keys so that data does not have to be duplicated.
Summary
• A database is an organised store of data.
• An entity is something with an independent existence that is uniquely identified and about which data can
be stored.
• A table is a collection of rows and columns that are used to store data in a structured and organised manner.
• A field is one item of information in a database table.
• A record consists of all the fields about an individual entry in a database (e.g. all the details about one learner in
a table).
• The primary key uniquely identifies each record in the table. It is usually a field that is guaranteed to hold
unique information for each record.
• A relational database allows data elements in one table to be related to any piece of data in another table as
long as both tables contain a common element.
• Massive amounts of unstructured data are produced every day. Unstructured data is information that isn’t
organised in a pre-defined structure. Examples include tweets, photos, emails and output from sensors.
Organisations use powerful analytic software tools to extract valuable information from unstructured data.
Checkpoint
Strengthen
S1 Describe the property needed by a field if it is selected as the primary key.
S2 Explain why the key field from one table may be included in another table.
S3 Explain how unstructured data differs from structured data.
Challenge
C1 Explain how relational databases minimise data redundancy and data inconsistency.
C2 Explain what is meant by the term ‘big data’ and why the effective use of big data gives organisations a
competitive advantage.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try the following activities again:
138
Computers
You might be surprised to know that every device shown above contains
Key terms an embedded computer. A computer is a machine that takes some kind of
Input: to enter data into a input from its surroundings, processes the input according to given rules,
computer. and provides some kind of output.
Process: to change the meaning
or format of some data.
Output: to display or output
data that has been processed (or INPUT PROCESS OUTPUT
has been stored).
140
You are probably familiar with a desktop computer: the keyboard provides
Exam-style question
input and the screen provides output (which depends on what you type). In a
similar way, the embedded computer in a washing machine has controls that List three outputs and three
provide input and a motor and heater as outputs; how fast they spin and how inputs of a smartphone.
much heat they produce depends on the input setting. (6 marks)
The clever bit in computer science is the activity that goes on between the
inputs and outputs – the processing. Processing means performing a series
of actions on the inputs according to a given set of rules. You might have
met this idea in maths already if you have studied ‘function machines’. You
might also have processed ingredients according to a recipe to make a cake;
or put together steps according to a choreography to make a dance; or
musical notes in harmonic combinations to make a song.
In Chapter 1 (page 2) an algorithm was defined as a precise method for
solving a problem, consisting of a sequence of step-by-step instructions.
In order for processing to produce meaningful results it has to follow an
algorithm. So you might also think of an algorithm as the sequence of steps
that is used to process the input to produce the output.
INPUT OUTPUT
3 ×2 +2 8
141
142
4.2 Hardware
Learning outcomes
By the end of this section you should be able to:
• describe the function of the hardware components of a computer system (CPU, main memory, secondary storage)
and how they work together.
• describe the function of different types of main memory (RAM, ROM, cache).
• describe 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 – in the fetch-decode-execute cycle (the von Neumann model).
• explain how data is stored on physical devices (magnetic, optical, solid state).
• describe the concept of storing data in the ‘cloud’ and other contemporary secondary storage.
• describe the need for embedded systems and their functions.
Activity 4
Produce a leaflet describing the pioneering work of the ENIAC
programmers. Add the details of any modern, influential female computer
scientists to your leaflet.
143
144
Writing
Address 3 11101000
Address 2 00000000
CPU
Address 1 10010111
RAM is described as volatile which means that its contents are lost when
the power is turned off. Because of this, computers also need non-volatile
Key terms
memory to store any programs that must run when the computer is first Volatile: memory that is erased
turned on – this memory is called read-only memory (ROM). Programs when the power is turned off.
permanently stored in ROM are known as firmware. Typically these programs Non-volatile: memory that is not
are small and carry out specific tasks, such as initialising the hardware lost when the power is turned off.
components and starting the operating system when a computer is switched Read-only memory (ROM):
on. You might have heard of these programs being called the BIOS (short for memory that cannot be altered
Basic Input/Output System) or UEFI (short for Unified Extensible Firmware and is not lost when the power is
Interface). Nowadays, UEFI has replaced BIOS as the standard firmware used turned off.
in desktop computers.
The photograph below shows the main circuit board of a 1980s computer (the
Sinclair ZX81). The ZX81 had either one or two RAM chips. Two are shown in
the diagram along with three other chips: CPU, ROM and a fourth chip that
connects the input and output devices. You can also see groups of parallel
silver ‘tracks’; these are the buses (the wires connecting the chips together).
145
Activity 7
Research the two chip
manufacturers Intel and
ARM. Compare how their
microprocessors are designed CPU High speed Cache Low speed RAM
and where they are used.
In a CPU cache, frequently used code or data is loaded in chunks from the
slower RAM into the cache. The CPU accesses the cache memory at its own,
faster speed. This means the CPU isn’t slowed down by having to wait for
data from RAM.
146
Activity 9
1 What is cache memory used for?
2 Describe the difference between RAM and ROM. Suggest a use for ROM.
3 Make a cartoon strip or storyboard showing the fetch-decode-execute
cycle.
4 Look back at the photographs and descriptions of the ZX81 and
Raspberry Pi. Working with a partner, identify the similarities and
differences between them. After 35 years, what has changed and what
is the same?
Fetch-decode-execute: in detail
There are many different kinds of CPU, but they share some features in
common. To understand the fetch-decode-execute cycle in more detail you
need to know a bit more about what goes on inside the CPU.
The part of the CPU that does calculations and logic operations is called the
Key terms
arithmetic/logic unit (ALU). Arithmetic/logic unit (ALU): the
part of the CPU that performs
Inside the CPU are a number of memory locations called registers. These
calculations and logic operations.
are extremely fast to access but there are usually only a relatively small
number of them. Some registers play a specific role in the fetch-decode- Register: a storage location
execute cycle, for example holding a memory address, an instruction or a inside the CPU used to hold an
piece of data, while others are general purpose. Some of the registers with a instruction, an address or other
specific role that are found in most computers include the following. single item of data.
Control unit: the part of the CPU
that organises the actions of the
Common registers
other parts of the CPU.
Accumulator: holds the results of calculations performed by the ALU. Clock: an electronic device
All input and output from the CPU passes through the accumulator. inside a CPU that ‘ticks’ at
Program counter: holds the memory address of the next instruction regular intervals and is used to
to be fetched. synchronise the actions of the
Current instruction: holds the instruction currently being executed. other parts of the CPU.
The steps in the cycle are controlled by a control unit and synchronised by
an electronic clock. You have probably seen the advertised clock speeds of
CPUs; for example, a 2.2 GHz CPU means that its clock ‘ticks’ 2200 million
times per second.
ON (1)
OFF (0)
147
Control Clock
Registers ALU
Bus
Bus width: the number of The number of connections on a bus is called the bus width. Since each
wires that make up a bus – this connection represents a binary digit, a 1 or a 0, a greater bus width means larger
determines the range of binary number values can be communicated. For example, an eight-bit address bus can
numbers that can be transmitted. send values from 00000000 (0) to 11111111 (255), only 256 possible addresses.
By contrast, a 32-bit computer can address up to 4 GB of RAM.
Now that you know a little more about the parts of a CPU you can understand
in more detail what happens at each step of the fetch-decode-execute cycle.
Fetch-decode-execute cycle
Fetch
The CPU control unit places the memory address of the next instruction
on the address bus. It also sends a signal on the control bus requesting
to read from memory.
The memory receives the signal and looks up that memory location. The
data in memory is copied on to the data bus. The CPU copies this into a
special register.
Decode
The control unit analyses the contents of the register and sends signals
to the other parts of the CPU telling them what to do (e.g. add numbers,
store data back into memory).
Execute
The instruction is completed by the CPU.
148
Activity 10
1 In groups of three, stage a debate on the best way to improve CPU
performance. One person should research and speak on behalf of
clock speed, another cache size and the third on type and size of RAM.
2 Work with a partner to produce definitions cards for the keywords in
this chapter.
Use your cards to play a describing game. See if you can describe the
keyword on your card to your partner without using the words in
the definition.
Secondary storage
In most situations you need to be able to store your data and programs
after the power is turned off. You have learned that RAM is volatile so most
computers need to be able to copy the contents of their RAM to another
kind of storage that is not volatile – a type that doesn’t lose its contents Key terms
when there is no power. This more permanent storage is called secondary Secondary storage: any kind of
storage. Secondary storage is non-volatile and, compared to RAM, is slower to permanent storage to which the
access, cheaper and has much higher storage capacity. Typical capacities are contents of ROM/RAM are copied
now in the terabytes, i.e. millions of megabytes. (usually a hard disk, optical or
You remember that all data is stored as binary: 1s and 0s. There are three solid-state device).
physical ways of recording 1s and 0s that do not require power. Magnetic storage: secondary
Magnetic storage uses the fact that magnets have north and south poles. By storage that works by
magnetising something, the north and south poles can represent the 1s and 0s magnetising parts of a substance
of your data. This is used in hard disks and magnetic tape storage. as north and south poles to
represent binary 1s and 0s.
Optical storage is used by CDs and DVDs. Shinier or more reflective parts of the
disk represent the 1s or 0s. Optical storage: secondary
storage that works using
Solid-state storage or ‘flash’ memory (such as USB memory sticks or SD cards)
differences in light reflection
represents the 1s and 0s by little pools of trapped electrons on a microchip.
from a material.
Magnetic secondary storage – hard disks Solid-state storage: secondary
Inside a hard disk drive is a stack of disks called platters with a magnetic coating storage that works by storing
on each surface. Tiny magnetic recording heads on the end of an arm float charge (electrons).
a millionth of a centimetre above the disk spinning at 110 km/h underneath.
Data is recorded on each disk along circular tracks, each split into smaller
parts called sectors.
When data is read:
1 The arm moves across to be above the right track.
2 The required sector comes around under the head.
3 The magnetised surface induces a tiny current in the head.
4 The disk controller translates this into 1s and 0s.
Because of these steps, data does not come from the disk immediately. Each
step takes some time. The time for step 1 is called the seek time; for step 2 it is
called the latency.
149
150
Both magnetic hard disks and solid-state flash memory are developing
rapidly and will continue to increase in capacity and decrease in size for
several years. The largest computer companies are currently researching
Electron pool
some brand new technologies that might become important in the
future. For example, HP is experimenting with a new type of electrical
component called a memristor to create super-fast, high capacity, non-
volatile memory chips. IBM is developing race track memory – a type
Silicon chip
of magnetic storage that stores information as magnetic patterns on
tiny wires, providing much faster data access speeds than current hard
Figure 4.11 Flash drive drives or flash disk technologies are capable of. You might also have
heard of quantum computing. Quantum computers store data as qubits.
Qubits can be individual atoms and each one is capable of storing (and
processing) more than one bit at once.
Activity 11
1 What is the purpose of secondary storage?
2 Name one kind of magnetic storage and one kind of solid-state
storage.
3 Monty runs his own gardening business. Explain which kind of
secondary storage you would advise Monty to use for:
a storing his garden design software
b taking garden designs to show potential customers
c backing-up all his financial records for seven years.
4 Produce a presentation about future storage: you can start your
research by searching online for the keywords in the text (e.g.
memristor, qubit).
5 If you have an old hard disk you no longer need, try taking it apart
to look at some of the components you have learned about. Your
tutor might have one to show you; you could also ask your school’s IT
technician.
Cloud storage
Sometimes it can be useful to have your secondary storage in a different
place from your computer. For example, you might want to have a back-up
copy of your data in a secure location or share your data with users of other
computers; or you might want to be able to access your data when you are
not sitting at your usual computer.
In Chapter 5 you will learn more about how networks can be used to share
computing resources. Computers that share their secondary storage across a
network are called file servers. You are probably used to using the secondary
storage on a server on your school network (your home drive or a learner
shared area). Some hard disk drives are able to connect directly to a network
(called NAS or network-attached storage). Others connect to several servers
151
server
Cloud
SAN
Internet
NAS
Your computer
Figure 4.12 Cloud storage diagram
152
Activity 13
1 Modern cars contain an engine management system. This is an
example of an embedded computer system.
a Suggest two inputs to and two outputs from this system.
b Explain one function of the engine management system.
2 Make a table listing five other embedded systems you might
encounter in your daily life. You could look back to page 140 at the
beginning of this chapter to get some initial ideas.
153
Checkpoint
Strengthen
S1 Describe in detail the fetch-decode-execute cycle. Include the roles of the memory, buses and special registers.
S2 Describe the advantages and disadvantages of different kinds of secondary storage.
S3 Explain what is meant by the term internet of things.
Challenge
C1 Describe the operation of a von Neumann architecture computer.
C2 Compare the features of CPU registers, cache memory and RAM.
C3 Explain the purpose of secondary storage and justify an example of a situation where optical storage is
the best choice.
C4 Evaluate the use of embedded systems to produce ‘driverless cars’.
How confident are you in your answers to these questions? If you are not sure that you
answered them well, go back and review the fetch-decode-execute section on pages 147–148.
Try to explain this to a family member at home; this will help you to understand it yourself.
154
4.3 Logic
Learning outcomes
By the end of this section you should be able to:
• construct truth tables for a given logic statement (AND, OR, NOT).
• produce logic statements for a given problem.
Truth tables
You learned in Chapter 1 (page 9) about selection – determining which
parts of a program run depending on certain conditions – usually
expressed using IF statements.
In the example the game is only over if both parts of the condition are
health <= 0 lives = 0 Game over?
true. We can show all the possibilities for this condition in a table.
No No No
This kind of table is called a truth table. It is the truth table for the AND
No Yes No
operator. The outcome is only true if both of the conditions are true.
(Usually you will see a ‘Yes’ written as a ‘1’ and a ‘No’ written as a ‘0’.) Yes No No
Yes Yes Yes
Perhaps the game doesn’t have lives and we only want to end the game
if you run out of ‘health’ or you pass the winning score of 1 million.
Health score > Game over?
IF health <= 0 OR score > 1000000 THEN <= 0 1000000
SET game_over TO true 0 0 0
END IF
0 1 1
Here are the possibilities – this time it is the truth table for the OR
1 0 1
operator.
In this case the outcome is true if either of the conditions is true. 1 1 1
155
god_mode NOT health <= 0 NOT score > 1000000 game over?
god_mode god_mode AND
health <= 0
0 1 0 0 0 0
0 1 0 0 1 1
0 1 1 1 0 1
0 1 1 1 1 1
1 0 0 0 0 0
1 0 0 0 1 1
1 0 1 0 0 0
1 0 1 0 1 1
The three logic operations AND, OR and NOT are very easy to build as electronic
circuits known as logic circuits.
Activity 15
1 Write, in words, the condition for the game to be over in the final
example in the table. (You will see why Boolean logic is clearer and
easier to use than English when the situation gets complicated.)
2 State which part of the CPU works out the logic for conditions.
3 Write a computer program that asks for your year group, grade and
target and then prints out whether you are invited to revision class or
not. Start by assuming revision classes are only for Year 11 students
whose grade is below target, then you can invent your own criteria for
invitations to revision class.
4 Write the truth table for the statement below. This time there are
three parts to the condition. Remember: as in maths, brackets are
worked out first.
IF year = 11 AND (grade < target OR target > 7) THEN
SET revision_class TO true
END IF
156
Worked example
Monty has decided to expand his gardening business to give online advice. He is writing a web page that
advises customers which fertiliser to buy from his online shop. The web page will ask customers about their
plants and then give relevant advice using these rules:
• If plants have yellow leaves mainly near the soil then they need a nitrogen fertiliser.
• If plants have brown leaves that are small then they need a phosphorous fertiliser.
To program this, you first need to work out what are the important features in the rules – these will be your
variables. In this case, the colour of the leaves is important. We can write the rules in pseudo-code like this.
Notice that we started off by assuming the plants were growing normally and only changed the advice if certain
conditions were met.
Monty points out that if leaves go brown, but are normal sized, then the customer needs potassium fertiliser –
now the size of the leaf is also important.
157
Activity 16
1 Add the following rules to Monty’s website logic on page 157.
• If plants have cracked or misshapen leaves then they need a
calcium fertiliser.
• If plants have leaves that are yellow mainly on the tips then they
need a magnesium fertiliser.
2 Why is Monty’s advice an example of abstraction?
158
Summary
• AND, OR and NOT are called logical or Boolean operators.
• They are used in selection statements such as IF.
• A truth table shows all possible combinations of inputs and the output produced.
• The order of precedence is: brackets, NOT, AND, OR.
Checkpoint
Strengthen
S1 Construct a truth table for the AND operator.
S2 Write a logic statement for the operation of headlamps on a car – the headlamps come on if the light sensor
reading is dark or if the headlamp switch is turned on.
S3 Produce a revision summary of this chapter.
Challenge
C1 Construct a truth table for the expression P = (A AND B) OR C.
C2 What combination of logic operations would produce an output of 1 only if the two inputs were different?
How confident are you in your answers to these questions? If you are not
sure that you answered them well, go back and review the examples on
pages 157–158.
159
4.4 Software
Learning outcomes
By the end of this section you should be able to:
• describe what an operating system is and how it manages files, processes, hardware and the user interface.
• describe the purpose and functions of utility software (managing, repairing and converting files; compression;
defragmentation; backing up; anti-virus, anti-spyware).
• describe how software can be used to simulate and model aspects of the real world.
160
Operating
system
Hardware
Figure 4.13 The operating system sits between the hardware and the programs
161
Exam tip Information about how much the hardware is used by each process is
displayed, including.
The command word in Question 1
is ‘describe’ and two marks are • the proportion of the CPU time spent on this process.
allocated to the question, so
• the amount of memory used by this process.
make sure you make two distinct
points in your answer. • the amount of available memory (real memory and virtual memory, VM).
These processes are running one at a time, being given their share of the CPU
Key terms time by the scheduler. They are all running alongside one another, but not
really at the same time, so we say they are running concurrently. An operating
Concurrent: processes that run
system that allows processes to run concurrently is called a multi-tasking
apparently at the same time are operating system. Almost all modern operating systems are multi-tasking.
described as being concurrent.
As well as allowing multi-tasking, some operating systems allow different
Authentication: the process of users to use the hardware. In some cases only one user can use the
proving to a computer system hardware at once (single user), whereas in others several users can be
who you are (e.g. using a sharing the hardware (multi-user). Your smartphone is an example of a
username and password). single user system, whereas your school network servers will be running a
User interface: the way the multi-user operating system so that many of you can log on at the same
user interacts with the operating time. In a multi-user system, each user has to identify themselves (with a
system username, fingerprint etc.) and provide a password (or other log in) to prove
they are who they say they are – this is called authentication. Authority
to use the facilities of the computer system is based on who you are, for
example learner users might not be able to print, whereas tutor users
can. In such systems there is usually also a super-user (sometimes called
administrator or root) who can access all resources and also control the
access rights of other users.
The operating system also provides and runs the user interface. Sometimes
this is a graphical user interface (GUI) with windows, icons, menus and
pointers (WIMP) like Microsoft® Windows® or MacOS, or sometimes it is just a
text, command-line interface (CLI) like MS DOS.
The choice of user interface depends on the applications a computer is
expected to run. An embedded system might not have an operating system
as such, and therefore does not have a user interface. A computer acting
as a web server might only need a command-line interface because it is
intended only ever to be managed by technicians. A computer used for
video-editing will almost certainly need a graphical user interface.
162
Activity 19
1 Run/open the program that lists the running processes on an
operating system you are familiar with. Make a table listing the
application software, utility software and operating system processes
that are running.
2 There are several different algorithms that operating systems can use
for scheduling. The one described on page 161 is called round-robin
scheduling. Research and describe at least one other method used for
scheduling.
3 Take one of the programs you have already written and add a feature
to save or load data using the operating system API.
Utility software
Utility software is software that does a useful job for the user. It is
• inessential to the operating system, and
• not the reason for using a computer in the first place (that is, it is not
application software).
Utility software, sometimes called tools, can be split into three areas:
• basic tools;
• file management;
• security.
Basic tools
Basic tools include things like a simple text editor (e.g. Notepad or nano),
calculator, command prompt, hex editor and software for accessibility
such as for producing large print. They are usually included as part of the
operating system.
163
164
165
Activity 21
1 Using a programming language that you are learning,
a write a simulation of rolling a die. The die must come up with 1 to 6 with equal probability.
b adapt your program so that the die is biased to give 6 more often than it should.
2 In a group, research techniques used in machine learning and illustrate your findings using presentation
software. Bring your talks together and present them back to the class.
Summary
• An operating system (OS) is software that
o manages a hardware device;
o manages users’ access to the I/O devices, including files on secondary storage;
o provides the user interface;
o shares the computing resources such as CPU and memory between processes.
• Processes are tasks running on the computer, including application programs.
• Utility software does useful jobs for the user, such as file management.
• Computer modelling enables experts to ask ‘what if?’ questions without affecting the real world.
• Computer models are very useful, but contain assumptions and simplifications so you can’t rely completely
on their results.
Checkpoint
Strengthen
S1 Name one operating system and list the main functions it performs.
S2 Describe the differences between command-line and graphical user interfaces.
S3 Explain why anti-virus software is utility software.
S4 Computer models suggest that if we continue burning fossil fuels at the current rate, average sea levels will rise
25 cm by 2050. Discuss the advantages and disadvantages of using a computer model for this problem.
Challenge
C1 State what is meant by a process and explain the purpose of scheduling.
C2 Suggest why some users might prefer a command-line interface to a graphical one.
C3 Write a program to model the populations of rabbits and foxes in a habitat. Use the following
information to help you.
Foxes eat rabbits
Foxes die of starvation if they don’t eat enough rabbits
Rabbits and foxes both reproduce, but rabbits reproduce faster
Rabbits and foxes both die of other causes
How confident are you in your answers to these questions? If you are not
sure that you answered them well, make a revision resource by researching
operating systems, utility software and computer models online before
attempting the questions again.
166
One other thing you learned in section 4.2 is that the CPU can only do a
few very simple things. It only ‘understands’ a few very simple instructions.
Complex things like playing games emerge from having a really large
number of those simple things happening very fast. Those few simple
instructions that a CPU knows how to do are called the instruction set
for that type of CPU. Each instruction is given a binary code and it is these
codes that make up your programs. The binary codes representing a
program are called machine code. Key terms
A machine code program would look something like the diagram below Instruction set: the list of all
– you can probably see why programming in machine code would be possible commands a particular
completely impractical. CPU knows how to carry out.
Machine code: the binary
0110100100100101010101011111010101100101011010110101010 codes representing each of the
instructions in the instruction set.
Figure 4.14 An example of a machine code program
Translator: a program that
converts source code into
Not only would it be incredibly difficult to work out the sequence of 1s and machine code.
0s you need, but also your program would only work on that kind of CPU
Source code: the text of the
because different CPUs have different instruction sets. Instead, you need
program that a programmer
a translator which converts your programs into the CPU’s machine code
writes.
language.
Assembly language: a low-
Translators are also programs. Their input is the text of your program – this level language written using
is called your source code. Their output is machine code which the CPU can mnemonics.
run (execute).
Low-level programming
The simplest translator is called an assembler. It converts assembly language language: a programming
to machine code. Assembly language is called a low-level programming language that is closely related to
language. This is because the programmer is working at the lowest level of the CPU’s machine code.
167
168
169
Summary
• CPUs can only run programs written in machine code.
• A translator is a program that converts source code into machine code.
• Humans program computers mostly using a range of high-level languages, but sometimes in assembly language (a
low-level language).
• Compilers and interpreters are types of translator, each having their own advantages and disadvantages.
Checkpoint
Strengthen
S1 Explain what is meant by machine code.
S2 What is the job of a translator?
S3 Momina is a C programmer and uses a compiler. Denis is a Python programmer and uses an interpreter. What
challenges will Momina face in developing software compared with Denis?
Challenge
C1 Explain the difference between high-level and low-level programming languages.
C2 Evaluate the choice of translator between compilers and interpreters.
How confident are you in your answers to these questions? If you are not
sure that you answered them well, you could review the chapter by
searching online.
170
5.1 Networks
Learning outcomes
By the end of this section you should be able to:
• explain why computers are connected in a network.
• describe the different types of networks and usage models.
• describe the characteristics of network topologies.
• describe wired and wireless connectivity.
• explain that network data speeds are measured in bits per second.
• describe the role and need for network protocols.
• explain that data can be transmitted in packets using layered protocol stacks.
172
share data.
LAN
LAN
Server Printer
Server Printer
Computer
Computer
173
174
Terminator
Terminator
Computer Computer
Printer
175
Top tip
Algorithms can be represented in many different ways. Improve your skills
and understanding by drawing a flowchart (see section 1.1 of Chapter 1,
page 3) that shows how the CSMA/CD algorithm works.
176
Ring
Computer Server
Computer
Printer
Computer Computer
A ring network is a network in which the cable connects one network device to
another in a closed loop, or ring. Each network device has what can be thought
of as an ‘in’ and an ‘out’ connection.
Messages sent on a ring network all travel in the same direction and, unlike
a bus network, there are no collisions. Data is passed from one device to the
next around the ring until it reaches its destination.
177
Computer
Printer
Hub or Switch
Server
Computer
Computer
Figure 5.4 Star network topology
178
Mesh
Fully connected Partially connected
Computer Computer
Computer Computer
Computer
Computer
Computer Computer
Figure 5.5 Mesh network topology showing fully connected and partially connected types
There are two main types of mesh topology – fully connected and partially
connected. In a fully connected mesh network every network device is
connected to every other network device. In a partially connected mesh
network some network devices may be connected to multiple other devices,
but others might only be connected to one other device. Each device in a
mesh network will pass messages on to other devices within the network.
Mesh networks can be wired or wireless. In a wired network the number
of connections needed for a fully connected network soon becomes very
expensive and difficult to implement as the number of network devices
increases. Wireless mesh networks don’t have the same problems.
Mesh networks are very fault tolerant. If a device or connection fails,
messages are simply routed around it. In a fully connected mesh network a
large number of failures could happen and the rest of the network devices
would still be able to communicate. The ability of a partially connected mesh
network to carry on working correctly will depend on how many connections
to each device are available. Key term
Internet: a worldwide system
The largest mesh network of all is the internet.
of interconnected networks
Mesh networks are used to communicate between sensors in the ‘internet of that enables information to be
things’, especially if the devices are mobile, because the optimum network exchanged and shared.
paths can be used no matter where a sensor is located.
179
Exam-style question
A small business has a network consisting of three computers, one printer
and a server. The network uses a star topology. Draw a diagram of the
network and label all parts. (4 marks)
Exam tip
• This question tests your knowledge of network topologies and the
components within them.
• A good answer will show the network cables in the correct layout,
any required network hardware and the correct items connected
to the network. The diagram will need to be labelled to show what
each item is.
Activity 3
1 Close this book and draw an annotated diagram for each of the
following topologies: ring, star, bus and mesh.
2 From memory, list the advantages and disadvantages for each
network topology you drew in Question 1.
3 Research which of the four network topologies in Question 1 would
be most suitable for providing local access to the internet in a
country that doesn’t have reliable wired communication links. Write a
paragraph explaining your decision and how the system could work.
Communication media
Key term There are many different ways that computers can be connected together to
Communication media: form a network. Networks are classified as either wired or wireless according
the means by which data is to the transmission or communication media used.
transmitted between devices on Wired
a network. Coaxial cable, fibre-
Wired connection methods involve a physical connection between the
optic cable and microwaves are all
computer and the network. Most wired connections are made of copper
forms of communication media.
wire but they could also be fibre-optic cable, which is made of either glass
180
Wireless
Wireless connectivity does not require a physical connection between
devices. Most wireless connections transmit and receive radio signals, but
other connection methods such as infra-red light or microwaves can be used
over limited distances. Did you know?
Important methods of wireless communication are the mobile phone Many TV remote controls use
network, Bluetooth® and Wi-Fi®. infra-red light to send signals
to control the volume or
Extend your knowledge change the channel.
The internet of things is a term that covers the growing range of
physical items that can be connected to the internet. These physical Key terms
items (the ‘things’) can report details about themselves and be
Protocol: a set of rules that
controlled over the internet. So your central heating could report the
govern how communications on a
temperature of your house and allow you to adjust the thermostat
network should be formatted and
from any internet-connected device, including your mobile phone.
what data they should include.
ZigBee is an open-source protocol for allowing devices to be
Eavesdrop: having unauthorised
controlled by using low-power digital radio transmitters/receivers that
sight of data being sent from
are embedded within the device.
one computer to another over
a network. This is covered more
Wired or wireless connectivity? fully in the later section on
There are advantages and disadvantages to both wired and wireless technical weaknesses (section 5.2,
connection methods. page 189).
Advantages Disadvantages
Wired connectivity
Faster than wireless connectivity Expensive to install and reconfigure
181
Although the units listed above have similar names to the units used to
measure storage (such as the capacity of a hard drive or the amount of
RAM a computer has), they are different and you should make sure not to
confuse them.
For example, you might measure storage in kilobytes (see Chapter 3,
section 3.3, page 117); however, you measure the speed of a network
connection in kilobits per second.
Key term
These times are only theoretical as transmission speeds are not constant
Bandwidth: the amount of data
and other factors affect the speed of data transmission.
that can be carried on a network
in a given period of time. The amount of data that can be carried from one point to another on a
network in a given period of time is referred to as its bandwidth. Bandwidth
182
is usually expressed as bits per second (bps). Another important factor is the
latency of the network connection. Latency refers to any kind of delay that
data travelling through a network might encounter. A low latency network
connection is one where any delays are small and a high latency network is
one where data suffers from long delays.
Activity 4
1 Find out how fast your school, home and mobile phone internet
connections are. Compare these, ensuring you use the correct units
(kbps, Mbps or Gbps). Make a note of your findings.
2 Work out how long it would take to download a 300 MB file using
your home and school networks.
Protocols
In relation to computer systems and networks a protocol is a set of rules
that determine how communications between devices are formatted and
how these communications will be sent/received.
Without protocols different computers and other hardware wouldn’t be able
to communicate with each other because they would essentially be speaking
different languages.
A protocol might contain details of:
• how each computer will be identified (its address);
• what route the data will take to get to its destination (routing
information);
• how errors will be detected and dealt with (error checking);
• whether each part of a message should be acknowledged as received
correctly;
• what to do if data isn’t received correctly;
• how the data is to be formatted;
• how the data is to be sequenced (i.e. does it need to be sent in order or
can it be put in its correct order later?);
• how the speed of the sender and receiver can be synchronised.
183
There are many different protocols for different purposes. The main ones
in use relating to networks and the internet are detailed below.
Email protocols
Emails are sent and received using a set of standard protocols. This means
that when you send an email it doesn’t matter what email provider the
recipient uses or what type of computer system they have.
There are three main email protocols in use.
Protocol Description
SMTP Simple Mail Transfer Protocol. This protocol is used when sending email
through the internet. It details the format that messages are sent in,
what commands email servers should understand and how they should
respond to them.
POP3 Post Office Protocol, Version 3. POP3 is the current version of the Post Office
Protocol that is used for retrieving email from an email server. Normally
email clients using POP3 will connect to the mail server, download any
messages and then delete the messages from the server.
IMAP Internet Message Access Protocol. IMAP allows emails to be accessed
using multiple email clients. For example, you might access your email
using an email client on your computer, your tablet and your mobile
phone. IMAP leaves messages on the server until you delete them. This
means that no matter which email client you use, you should see an up-
to-date list of your email messages.
Network protocols
Ethernet
Ethernet is a family of protocols that are used in wired LANs. They cover
everything from the physical parts of a network, such as type of cable or
optical fibre and type of connector to be used, to the logical parts, such as
how data is sent, checked for errors and the speed that data can
be transmitted.
Wi-Fi
Wi-Fi is a digital communications protocol that sets out how data is
transmitted on wireless LANs. Wi-Fi is a trademarked term that is owned by
Wi-Fi Alliance.
184
TCP
The Transmission Control Protocol provides a reliable connection between
computers. In this context ‘reliable’ means that the receiving computer can be
certain that it has received all the data it should have (none of it is missing)
and the data received is identical to the data sent (the data is correct).
TCP does this by:
• specifying that the receiving computer sends acknowledgements that
each section of the data sent has been received; Key term
• using checksums to ensure that the data received is accurate; Checksum: an error detection
technique. A mathematical
• allowing the receiving computer to tell the sending computer to slow
formula is applied to the data and
down transmission, so the receiving computer has time to process the
the resulting numerical value is
received data (this is called flow control);
transmitted with the data. The
• ensuring that data sent up to the application layer (see below) contains recipient computer applies the
no duplicates and is in the correct order. same formula to the received
TCP is used when you access web pages, send/receive email or upload/ data and then compares the
download files. checksum sent with the data to
the calculated checksum. If the
TCP/IP checksums don’t match the data
is likely to have been corrupted
TCP/IP stands for Transmission Control Protocol/Internet Protocol. TCP/IP
and the recipient computer
is a protocol stack, which means that it is a collection of protocols that
requests the data again.
work together. It is named after the two most important protocols used
in the stack.
These protocols work in a hierarchical set of layers, where each layer deals
with a particular function of the network. Each layer will pass information up
Did you know?
and down the protocol stack as it is processed.
You will sometimes see TCP/
The TCP/IP protocol stack has four layers. Data passes down the stack when
IP described as protocol suite
sending, and back up when receiving.
rather than a protocol stack.
These two terms are often used
Layer Description interchangeably, but some
Application This is the top layer of the stack. It is the layer which sources state that the suite is
interacts with the user to provide access to services and the definition of the protocols
data that is sent/received over a network. Examples of and that stack is the software
protocols that work at this layer are HTTP, FTP and email implementation of the protocols.
protocols.
Transport This layer manages end-to-end communication over a Top tip
network. There are two main protocols that operate at this
layer – TCP and UDP. (You only need to know about TCP for Remembering a list of items
GCSE Computer Science.) where the order is important
Internet This layer deals with sending data across multiple (such as the TCP/IP layers) can
networks (possibly the internet), from the source network be difficult. Why not try using a
to the destination network. This is known as routing and mnemonic like ‘Another Truck
is the role of the Internet Protocol (IP). Is Late’, where the initial letter
Link This layer controls the transmission and reception of of each word matches the initial
packets of data to/from a local network. letter of the layer name?
185
Figure 5.6 Layers within the TCP/IP suite pass data down the stack when sending and back
up the stack when receiving
HTTP
Extend your knowledge
The HyperText Transfer Protocol is used when sending and receiving data
Another protocol that plays between web browsers and web servers. The HTTP protocol covers how
a major part in the TCP/IP data should be formatted, what commands the web server and web browser
protocol suite is called UDP. should understand and how they should react to each command.
Research this and create a
For example, if you request a web page that doesn’t exist on the web server,
table explaining the differences
then the web server will send your web browser a 404 error message and your
between TCP and UDP.
web browser will display this error and a short description of the error.
186
HTTPS Activity 5
The HyperText Transfer Protocol Secure is the secure version of HTTP. This
means that the data sent between your web browser and the web server is When you use online banking
encrypted, which should prevent the data being sent/received from being your data will be transferred
read by a third party. HTTPS also helps the web browser and user to know that using HTTPS. Research why
they are communicating with the intended web server rather than a fake that HTTPS is used rather than HTTP.
is trying to steal sensitive data, such as passwords or bank details. What advantages does HTTPS
give the bank customer and
FTP the bank itself? Create a poster
File Transfer Protocol is used to transfer files over a network that uses the TCP summarising this information.
protocol (see above), such as the internet.
FTP is often used when sending web pages and other associated files that
have been created on a web developer’s computer to the web server. Once the
files are on the web server, other computers connected to the internet will be
able to view them.
Internet protocols
IP
At the internet layer, the Internet Protocol (IP) deals with:
• the addressing system to identify individual computers/servers on the
network (usually the internet);
• splitting data into packets and adding the packet header with details
such as the sender/receiver addresses.
Each device linked directly to the internet has a unique IP address assigned
to it to allow data to be routed to it. There are two versions of the Internet
Protocol (IP) in use: the earlier version is called IPv4 (Internet Protocol
version 4) and the newer version is called IPv6. IPv6 has a number of
improvements compared with IPv4. One of the major ones is that it has
a larger address space than IPv4. Address space refers to the number of
addresses available to assign to devices.
187
Activity 6
Create a revision sheet that includes the following:
1 a definition of what a protocol is;
2 a table that includes the following protocols: SMTP, POP3, IMAP,
Ethernet, Wi-Fi, HTTP, HTTPS and FTP along with a brief description of
what each one is used for;
3 a diagram showing the TCP/IP protocol layers – you should annotate
this to show what each layer does and which protocols run at each
layer.
Summary
• A network is a number of computers and other devices connected together to allow them to communicate
and share data.
• A LAN covers a relatively small area. A WAN connects LANs together and spans a large geographical area.
• Client–server and peer-to-peer networks are two different network configurations. In a client–server network
a central computer fulfils service requests from client computers, whereas in a peer-to-peer network each
computer can act as a client and a server.
• Network devices can communicate using wired or wireless connections.
• A protocol is a set of rules governing communications on a network. They specify the format for
communications and how they must be sent/received.
• There are many different protocols, each serving a different purpose.
• A network topology is how the connections between devices in a network are configured.
Checkpoint
Strengthen
S1 What is the difference between a LAN and a WAN?
S2 What are the advantages of wireless networks compared with wired networks?
S3 What are the three major protocols used in sending and receiving email?
S4 What is a protocol stack?
S5 From memory, draw a diagram showing the bus, ring and star topologies.
Challenge
C1 Why might a chain of supermarkets use a WAN to connect all its stores?
C2 Explain the advantages of a peer-to-peer network in terms of privacy when sharing files.
C3 Most networks in use in schools and offices today are based on the star topology. Why do you think this
topology is used?
C4 IMAP is now used much more than POP3. Can you explain the link between this and the popularity of
smartphones?
How confident do you feel about your answers to these questions? If you’re not sure
you answered them well, try drawing a concept map showing how the information
in this section links together. Your concept map can have brief definitions on it and
some people use colours/pictures to help them memorise the contents.
188
189
Top tip
Articles relating to network security can often be found on news websites
under the technology section. Reading these articles will broaden your
knowledge about the latest security threats. You can then look for further
details on specialist information technology websites, where you will
often find details of what happened and how to protect against that kind
of threat.
190
Activity 7
Think about how you would write a program so that only approved users
who enter their password correctly can get beyond the log-in screen.
1 Write the pseudo-code for the username and password checking part
of the program.
2 Write your program.
3 Discuss how secure you think your implementation is. How could it
be improved?
191
193
194
Activity 10
Many providers offer cloud storage aimed at individuals.
1 Create a table to compare what three of these providers offer.
Headings for the table should include: Amount of free storage space,
Cost for more storage, Security measures.
2 Research and write a brief report that explains what commitments
these providers make to keep your data secure. Relate this to the
security measures you found out about in step 1 of this activity.
195
Phishing
A phishing attack is an attempt to get sensitive, confidential information from
the user of a computer system or service. Often the information the phishing
attack is targeting is usernames and passwords or financial details such as bank
account or credit card details.
There are various forms of phishing. The most common types are through email
or fake websites that look legitimate, but phishing can also happen via phone
calls or instant messaging.
A typical phishing attack might start with an email that asks the user to update
details at a bank, online payment system, online auction website or social
network. Sometimes the bogus reason given for this request is that there has
been a fraud attempt on the user’s account.
Once the user clicks on a link within the email, a website that looks and acts like
the real website will open, but when the user enters his or her login details they
are passed to the attacker, who will use them for financial gain.
Phishing takes advantage of people trusting the contents of emails to be
genuine. Many people don’t bother to look at the web address they have been
taken to or think about whether their bank is really going to email them to
request a security update.
Shoulder surfing
Shoulder surfing means gaining access to confidential information by directly
observing a user, possibly literally looking over their shoulder, as they complete a
task. Often shoulder surfing is used to get a person’s username/password or PIN.
196
Activity 11
1 Research recent cyberattacks. What kinds of organisations are targeted?
How successful were the attacks? What information was taken?
2 Some organisations have been known to deny that a cyberattack
successfully happened or downplay the effects of a cyberattack. Why
might they do this? What are the risks to a business of lying about
cyberattacks?
Technical weaknesses
Other forms of cyberattack rely on technical weaknesses (vulnerabilities) in the
system being attacked. Some common examples are described below.
Unpatched software
Software is very complicated and usually security issues are found as it is used
in the real world. The maker of the software will normally provide updates
(referred to as patches) to fix security issues as they are found.
Often these security issues are discussed on the internet and some people will
Key term
use this knowledge to attack unpatched software to gain unauthorised access Unpatched software: software
to information. that hasn’t had the latest security
The patches to fix the security issues often have to be manually installed by updates applied to it, making it
a technician. Sometimes these patches get forgotten about or the organisation vulnerable to attack.
doesn’t have any technicians to install the patch, so the software remains
vulnerable.
USB devices
You read earlier about the weaknesses associated with USB flash drives, but
any USB device can potentially be a security threat because it might contain
malware that could be transferred to your system or copy data to the attacker
via the internet.
Eavesdropping
One form of attack mentioned earlier is an eavesdropping attack. In
computer security terminology, eavesdropping means intercepting data
being sent to/from another computer system. In a similar way that a person
can eavesdrop on a conversation without the speakers knowing about it,
eavesdropping on a network is simply reading data without actually copying
or stealing it. The owner of the data might not know that the data has been
read until it is used by the criminal.
Security weaknesses such as unpatched software or a USB device might allow
malware to be installed on the network that allows an eavesdropping attack to
be carried out, but specialised hardware might also be used.
197
198
Identifying vulnerabilities
Ethical hacking is the branch of computer science that relates to cybersecurity
and preventing cyberattacks from being successful. Ethical hacking is
Activity 12
essentially ‘good’ hacking – it is looking for weaknesses in software and A patch has become available for
systems so that they can be improved – whereas hacking is seen as ‘bad’ – a major web browser. Research
trying to gain access to a system to steal data or cause damage. the risks that the user faces by
not updating his/her software
There are many ways that vulnerabilities can be identified and some of
and add details of the potential
these are explained below. Once a vulnerability has been found, steps to
risks to your revision notes.
remove or reduce its impact can be taken.
199
200
Summary
• Authentication is the process of checking the identity of someone trying to use a computer system.
• Cloud storage is storing data using a third party, usually in a system connected to the internet.
• A cyberattack is any kind of electronic attack on a computer system, server, network or other IT device.
• There are many different types of cyberattack; most exploit technical weaknesses or human behaviour.
• Many different measures need to be taken to prevent cyberattacks from being successful. These start at the
design stage of the system and include keeping systems up to date, training staff and proactively testing the
security of the network regularly.
Checkpoint
Strengthen
S1 What is the most common authentication method in use?
S2 What is access control?
S3 What is social engineering?
Challenge
C1 In your own words, summarise the different types of cyberattack.
C2 The number of cyberattacks happening is increasing each year. Why do you think this is the case?
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, try writing a list of questions then work
through this section adding relevant points as you find them.
201
The internet
The word internet is a shortened form of the words ‘inter’ and ‘network’,
which together means interconnected networks. It’s a good idea to think
of the internet as a network of networks (i.e. the biggest WAN of all). Your
school, many organisations and many people’s homes all have a network
that is a part of the internet.
The reach of the internet is global – all countries have multiple cables, fibre-
optic links and satellites that connect it to other countries. These links plus
others within each country can be thought of as the internet backbone – the
connections that link all the networks together.
202
203
Key term 1 The user of a computer enters the web address of the information he or she
HyperText Markup Language wants to look at. A web address is also known as a URL (Uniform Resource
(HTML): essentially a text Locator). A URL may be for a web page or an individual file, for example
document that contains any text a URL could be just for a particular picture file rather than a web page.
to be displayed along with: 2 The computer uses a system called the Domain Name Service (DNS) to
• details of how the text should find the IP address of the required web server.
be formatted (e.g. font size, 3 The web browser connects to the web server using the IP address and
colour etc.); requests the relevant web page or other object (picture, sound or
video file).
• details of any hyperlinks and
where they link to using a URL; 4 A web page is transferred from one computer (normally a web server)
to another using HTTP or HTTPS, which were covered in ‘Network
• details of any objects such as protocols’ in section 5.1.
pictures or videos that should
5 Data sent from a web server to a web browser is in HyperText Markup
be shown within the web page
Language (HTML) format. The web browser displays the web page as
when it is displayed.
described by the HTML.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
</body>
</html>
Figure 5.7 How a web browser display of a web page is generated from HTML
Exam-style question
Which of the following protocols would not be used when transferring a
web page from a web server using the internet? (1 mark)
a TCP
b IP
Exam tip c IMAP
• As this is question is for d HTTP
1 mark, only one answer is
correct. If the user selects a hyperlink, then the URL that the link points to will be
loaded using the method outlined above. The URL might point to a web
page on the same web server or on a different web server. This is where the
‘web’ part of World Wide Web comes from – different pages and web servers
are linked together in a complex pattern.
204
Summary
• The internet is a global network of networks. It is used to transfer data between different computer systems.
• The internet has many different services running on top of it. Two common services are the World Wide Web
and email.
• The World Wide Web runs on top of the internet. Although people often mix the two terms up, they are not
the same and as a learner studying Computer Science you need to use the terminology correctly.
Checkpoint
Strengthen
S1 Write your own definition of what the internet is.
S2 Write a definition of what the World Wide Web is and add an explanation of how it is different from
the internet.
S3 Describe what happens when you enter a web address into a web browser.
Challenge
C1 Explain in your own words the role of all the protocols used when requesting a web page from a web server.
C2 Explain why you think the internet and World Wide Web are so embedded in modern life.
205
It’s hard to imagine what life would be like without the internet, social
Key terms
media, search engines and e-commerce. Global demand for smartphones,
tablets and other forms of computing technology, including embedded Computing technology: an
processors, web servers, sensors and hard drives, is growing rapidly year on all-encompassing term referring
year. At the same time, the pace of new product releases and consumers’ to the hardware, software and
desire to own the latest model is shortening the lifespan of these devices. infrastructure that underpin
The average life expectancy of a smartphone, for example, is estimated to current and emerging computer
be less than two years. The more computing technology we buy, the more systems.
we throw away. e-waste: any form of discarded
electronic equipment, including
Not surprisingly, the manufacture, use and disposal of computing
computing technology.
technology have a significant impact on the environment, using up
dwindling resources of non-renewable materials, creating massive piles
of potentially harmful e-waste, consuming vast quantities of energy and
damaging people’s health.
207
208
Usage
The amount of energy consumed in the manufacturing process pales into
insignificance when compared with the energy required to keep mobile
phones, computers, networks, telecommunication links, etc. up and running
day after day. Even though each individual device doesn’t require a huge
amount of electricity, close to two billion connected PCs and laptops and
more than six billion mobile devices collectively do.
In recent years considerable efforts have been made to improve the energy
efficiency of computing devices. However, the amount of energy they
Did you know?
actually consume depends on how they are used and what they are used for. A lot of energy is wasted while
The task that a computer is performing and the software being used are key a computer or printer sits idle.
determinants of the actual energy usage. High-end applications, complex Using the ‘sleep mode’ when a
calculations, 3-D modelling and video games are particularly power hungry. device is not in use can reduce
Cloud computing (see section 4.2, page 151) and data centres in particular consumption by more than
are major energy guzzlers. Vast amounts of electricity are needed to power 50 per cent.
and cool all the computer equipment that is needed, putting them ahead of
the aviation industry in terms of the energy they consume. The worst culprits
are the small, inefficient data centres hosted by private organisations and
government departments, which tend to be far less efficient than the large
facilities operated by cloud providers such as Google® and Apple.
Energy efficiency measures and the use of renewable energy can
significantly reduce the carbon footprint of data centres. Facebook, for Activity 1
example, has built a huge data centre in northern Sweden, just 100 km Research why using a laptop
south of the Arctic Circle. Its location was selected because of its access rather than a desktop is more
to renewable hydroelectricity and the cold climate that helps to keep the energy efficient.
servers cool.
209
Disposal
The disposal of redundant computing technology represents another
serious threat to the environment. The quantity of e-waste is growing at a
tremendous pace. According to the UN’s StEP Initiative, e-waste will soon
weigh as much as eleven of the great Egyptian pyramids.
Although great efforts are now being made to recycle more e-waste, large
amounts are still shipped overseas to developing countries where they are
dumped in landfill sites. This can have serious consequences for the environment
and public health. The problem is compounded by the fact that the developing
nations themselves are generating more and more waste of their own.
E-waste that is not recycled properly can be a serious health and environmental
issue. As you know, computer products contain a whole host of dangerous
materials. Once a computer is dumped in a landfill site, the likelihood is
that some, if not all, of these toxic substances will leak out into the ground,
contaminating water supplies, infiltrating the food chain and polluting the air.
For every one million mobile phones, 24 kg of gold, 250 kg of silver and nine
tonnes of copper can be recovered. The presence of these valuable metals
in old computing technology is a powerful incentive for local people living
near the landfill sites, many of whom are desperately poor, to try to recover
them. However, dismantling old computer equipment without protective
clothing and specialist training is extremely dangerous. People who do so
risk exposure to hazardous materials such as mercury and lead and are in
danger of inhaling toxic fumes.
Activity 3
‘One person’s cast-off is another person’s treasured possession.’
Research, then briefly describe two initiatives that aim to prolong the
life of pre-owned computing technology.
210
Early warning
Tsunami early warning systems use sensor networks to detect approaching
tsunamis and a communications infrastructure to issue timely warnings so
that coastal areas at risk can be evacuated.
Conservation
Information from GPS and satellites is being used to track Malaysian
elephants. The results are analysed by computer to help improve
conservation strategies and assess the effectiveness of the Malaysian
Government’s elephant conservation programme.
Mobile phones are being used to listen out for illegal logging activities in the
rainforest and provide rangers with real-time alerts.
Energy
Engineers at Manchester Metropolitan University are working on a project
to make buildings more energy efficient. Sensors in each room monitor A lioness with a tracking device.
light levels, temperature, how many people are present and electricity GPS collars help to monitor and
consumption. Real-time analysis of the room data enables automatic conserve wildlife.
adjustment of electricity usage.
211
Summary
• Some of the materials used in the manufacture of computer components are non-renewable and in
short supply. Others are dangerous and pose a risk to human health.
• The Restriction of Hazardous Substances (RoHS) Directive restricts the use of hazardous materials in
computing technology, forcing producers to find more environmentally friendly alternatives.
• Computing technology consumes huge amounts of energy. Data centres are one of the worst culprits.
• Energy efficiency measures and use of renewable energy can significantly reduce the carbon footprint
of computing technology.
• There is a possible health risk, especially for children, from exposure to the electromagnetic fields
generated by wireless devices, such as smartwatches and smart clothing.
• Unregulated disposal of e-waste in landfill sites poses a significant threat to the environment.
• The Waste Electrical and Electronic Equipment (WEEE) regulations set targets for responsible recycling
of e-waste.
• Computing technology is helping to preserve the environment in a number of ways, including monitoring
and modelling climate change, conservation and smart energy.
Checkpoint
Strengthen
S1 Identify two hazardous substances used in the manufacture of computing technology.
S2 Which UK law restricts the use of hazardous substances in the manufacture of computing technology?
S3 Why is dumping e-waste in landfill sites harmful to the environment?
S4 List two ways of reducing the environmental damage caused by data centres.
S5 List two ways in which computing technology is helping to preserve the environment.
Challenge
C1 Summarise the health risks associated with the manufacture and disposal of computing technology.
C2 Describe three ways in which computing technology can help to reduce energy consumption.
212
6.2 Privacy
Learning outcomes
By the end of this section you should be able to:
• explain why computing technology poses a threat to privacy.
• weigh up the benefits and drawbacks of giving away personal information.
• describe the legislation that protects against computer misuse.
Now that you know about the damaging environmental impact that
computing technology has, does it make you have second thoughts about
swapping your smartphone for the latest model? Might you decide that your
concern for the environment outweighs your desire for a new phone? If so,
Key terms
you are making an ethical decision. Ethics relate to what is right and wrong Ethics: a set of moral principles
and govern a person’s behaviour. that govern a person's behaviour.
An action might be legal, but not necessarily ethical. For example, it’s perfectly Privacy: the right to be left
legal to leave your old desktop computer gathering dust in the attic, but if you alone and free from unwanted
know that someone in a developing country would benefit enormously from scrutiny and intrusion.
having it, the right thing to do might be to dust it down and pass it on Personal data: information that
to them. is personal and unique to an
individual.
Computing technology confers a wide range of social and economic benefits,
but it also creates a host of challenging ethical issues. Privacy and security
are two of them.
While most people would agree that computing technology has helped to
create a much more open society, some would argue that it comes at too high
a cost. The amount of personally identifiable information that is gathered,
stored and analysed represents a massive invasion of privacy.
Personal data
Every time you post an update on social media, sign up for an online
account, use a web-based email service or a search engine you are adding,
knowingly or unknowingly, to an enormous hoard of personal data that is
held about you – where you live, what you look like, who your friends are,
your likes and dislikes, your bank account details, products you’re interested
in buying, the route you take to school each morning.
This personal data is stored on servers that belong to online services, such
as Facebook and Google®, not to you.
Did you know?
Facebook has the biggest
Every organisation you come into contact with, not just the online
database of faces in the world,
companies, is likely to collect information about you. Your school, for
with over 350 million photos
example, stores your attendance record, your end-of-year exam results,
posted and tagged to the
which books you’ve borrowed from the library, the after-school activities you
website every day.
take part in and much more besides. Does this worry you?
213
Activity 5
1 What information can you find out about yourself by typing your
name into a search engine? Is it accurate? What sort of impression of
you does it portray?
2 Research the Safe Harbor Decision privacy principle. Why has the EU
declared it invalid?
One reason why so many people voluntarily give away information about
themselves is that it enables an organisation to understand their needs
better and provide them with a more personalised service. For example,
setting up an account with an online supplier makes it faster and more
convenient to purchase from them.
You might not mind that an online retailer knows who your favourite band
is if it means you get to hear quickly when their next album is released, but
is it right to target a financially vulnerable person with adverts for products
they will want but can’t afford?
214
Big data
Data analysts are able to learn more and more about us and gain insights
into our behaviour by analysing huge volumes of personal data gathered
from various sources.
Analysis of so-called ‘big data’ can benefit society. For example, by helping
to identify adverse side effects of drugs that might otherwise go unnoticed,
optimising energy use in cities and providing insights into the spread of disease.
But is the price we pay too high? Where do we draw the line? Big data
comprises large amounts of information, each piece of which on its own could
be seen as being harmless, for example your phone number, what music you
download, your hobbies, etc. However, when collated together these individual
bits of information produce a very accurate, detailed profile of an individual,
revealing far more about them than they might have willingly disclosed. This Activity 6
might lead to the individual becoming a victim of identity theft or an intruder Find two examples of how
illegally accessing personal information through social engineering. (See society is benefiting from big
'Personal data' on page 213.) data analysis.
Surveillance
Have you any idea how often you’ve been watched on CCTV today? Could
a drone have been hovering overhead taking aerial photographs of you on
your walk to school? If you’ve driven anywhere by car, travelled by public Key terms
transport or been in a shop, the chances are you’ve been recorded by some Surveillance technology:
form of surveillance technology. CCTV, drones, number plate
Most people are willing to allow the security forces to use surveillance recognition, bugging and
technology to track people’s movements and tap their phones if it enables tracking devices used to monitor
them to uncover terrorist plots. But what if it were used by companies to and record people’s activities,
monitor your shopping habits or by criminals noting the time you leave often without their knowledge.
the house each morning? It’s not unheard of for employers to use hidden Whistle-blower: someone who
cameras to check up on their staff. Is this acceptable? draws attention to the activities
Some people believe that use of surveillance technology goes too far. In of an organisation or person
2013 Edward Snowden, a so-called ‘whistle-blower’, raised awareness of the believed to be acting illegally
extent to which governments worldwide are now monitoring and spying on or unethically.
their citizens.
215
Tool Purpose
Encryption Prevents unauthorised people from reading your data.
Cookie cleaners, anti-spyware and ad Software that detects and removes cookies, spyware and adware installed on
blockers your computer.
Identity management services A trusted third party holds evidence of your identity and issues you with
an identifier that enables you to conduct transactions with other parties
without revealing any personal information about yourself.
Password managers Stores all your website login information in an encrypted password
database with a master password, which is the only one you have
to remember.
Cyber-security
As you learnt in Chapter 5 (see page 192), hacking represents a serious
security threat.
The Computer Misuse Act (1990) makes hacking a crime. It identifies three
types of illegal activity.
216
Summary
• Computing technology enables organisations to gather, store and analyse vast quantities of personal
information about the people they come into contact with.
• Individuals give away all sorts of personal information about themselves online.
• Collecting and analysing information about the people they come into contact with enables organisations
to provide a more personalised service.
• Big data analysis benefits society at the expense of many individuals’ privacy.
• Surveillance technology helps keep us secure, but encroaches on our privacy. It is difficult to determine
what level of surveillance is acceptable.
• The Computer Misuse Act (1990) makes hacking illegal.
Checkpoint
Strengthen
S1 Describe two ways in which an individual’s personal data could end up stored in databases owned by
a third party.
S2 What might happen if personal information falls into the wrong hands?
S3 List two privacy-enhancing tools and describe what they do.
S4 List the activities that the Computer Misuse Act (1990) makes illegal.
Challenge
C1 Why do some people decide that the benefits of revealing personal information about themselves
outweigh the drawbacks?
C2 Describe a situation where the right to privacy is less important than the needs of society.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, reread this section and have another go at
the activities.
217
Digital inclusion: ensuring Digital inclusion is about providing everyone with affordable access to
that everyone has affordable computing technology and the skills to use it.
access to computing technology
The gap between those who are ‘technology-empowered’ and those who are
and the necessary skills to take
‘technology-excluded’ is known as the digital divide.
advantage of it.
Digital divide: the gap between There is a digital divide between industrialised and developing countries
people who are technology- and also between people who live in the same country.
empowered and those who are
technology-excluded.
Impact
There are many reasons why technology exclusion is not a good idea.
Information and The internet is becoming the default option for accessing
services information, public services and entertainment.
Employment Having poor digital literacy skills makes it harder
to find a job and limits employment opportunities,
relegating individuals to poorly paid work with little
prospect of progression.
Democracy The internet gives people a voice and lets them
express their views to a worldwide audience. This is
particularly important where citizens have limited
freedom of expression.
Economic growth Businesses that are able to exploit computing
technology to the full have a competitive advantage
over those that can’t.
Saving money Paying bills and shopping online often saves
consumers money and gives them better protection.
Social isolation Having access to the internet helps people to keep in
touch with friends and relatives.
218
• 96 per cent of adults aged 16 to 24 accessed the internet ‘on the go’,
compared with only 29 per cent of those aged 65 years and over;
• 61 per cent of adults used social networking and, of those, 79 per cent did
so every day or almost every day;
• 76 per cent of adults bought goods or services online;
• 86 per cent of households (22.5 million) had internet access, up from
57 per cent in 2006.
Other industrialised nations in North America, Europe and Northern Asia are
also doing well.
The same can’t be said for other parts of the world. According to the United
Nations’ State of Broadband report (2015), billions of people living in the
developing world are still without broadband internet, including 90 per cent
of those living in the poorest nations.
Age, disability, disinterest, poverty and cultural norms all play a part in
digital exclusion, but lack of connectivity is one of the major causes.
The good news is that, according to the World Bank, 77 per cent of the
world's population already live within range of a mobile phone network. In
areas with a limited or non-existent landline infrastructure, mobile phone
technology can fill the gap. Even though the number of phones per 100
people in poor countries is much lower than in the developed world, they
are having a huge impact.
Activity 10
Use the internet to find some actual examples of how mobile phones
are being used in Africa or elsewhere to promote digital inclusion.
Write a brief report summarising your findings.
Efforts to bridge the digital divide require more than simply giving people
internet access. The UK’s digital inclusion strategy sets out the actions that
the government and its partners are taking to reduce digital exclusion.
Ambitious digital literacy programs are
under way in India, Kenya, Colombia
Activity 11 and elsewhere to ensure that people
have the know-how and skills they need
Identify five actions a government can take to reduce digital exclusion.
to exploit digital technology to the full
219
Summary
• Digital inclusion means that everyone has affordable access to computing technology as well as the skills to
use it.
• A number of factors contribute to the digital divide – lack of or poor connectivity is one of the main ones.
• Someone who is ‘technology-excluded’ misses out on all the opportunities computing technology offers.
Checkpoint
Strengthen
S1 What is meant by the terms ‘technology-empowered’ and ‘technology-excluded’?
S2 List three drawbacks of being ‘technology-excluded’.
S3 List four factors that contribute to the digital divide.
S4 Describe two ways of providing access to the internet in areas with a poor landline infrastructure.
Challenge
C1 Access to the internet is a key factor in reducing the digital divide. Describe two further measures that
governments can take to promote digital inclusion.
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, reread this section and have another go
at the activities.
220
6.4 Professionalism
Learning outcomes
By the end of this section you should be able to:
• explain what professionalism means in the context of computer science.
State what course of action the programmer should take and explain why. • The British Computer Society
(BCS)
(3 marks)
• The Institute of Electrical and
Electronics Engineers (IEEE)
Exam tip • The Association for Computing
This is a scenario-based question, so make sure you relate your answer to the Machinery (ACM).
scenario – don’t mention what you personally would do, but what the Airtest
programmer should do. Don’t forget to refer back to the code of conduct.
221
Activity 12
A computer scientist is developing an embedded system to control
car brakes.
Discuss in a group how she should respond if:
1 she spots a bug, but knows that fixing it will delay completion of
the project;
2 she’s aware that there is a remote possibility that someone could hack
into the code and stop the brakes from working;
3 a competitor offers to pay her for details of the code;
4 her employer makes claims for the system that are not entirely true.
Discuss any other ethical dilemma she might encounter as a professional.
Summary
• Computer scientists must abide by the law.
• They should behave ethically by adhering to a professional code of conduct.
• It is important that programmers demonstrate professionalism as the work they do could put the lives of other
people at risk.
Checkpoint
Strengthen
S1 What does the BCS Code of Conduct say a computer scientist should do?
Challenge
C1 What does ‘professionalism’ mean for a computer scientist?
How confident do you feel about your answers to these questions? If you’re
not sure you answered them well, reread this section and have another go at
the activities.
222
223
224
Checkpoint
Strengthen
S1 Describe the purpose of a software licence.
S2 What are the main differences between open-source and proprietary software?
Challenge
C1 Explain how a patent would protect you as an inventor of a product.
C2 Identify relevant legislation that computer scientists should be familiar with.
How confident do you feel about your answers to these questions? If you’re not
sure you answered them well, reread this section to enhance your knowledge.
225
This section of the book will help you to prepare Explain. Providing points that are linked to a
for the external assessment. GCSE (9–1) Computer justification. For example: ‘Explain why increasing
Science has two written examination papers and a the size of the cache will improve a computer’s
project. You must carry out the project in the final performance.’ This requires you to describe the
year of your course and sit both examinations in your purpose of the cache and then go on to explain why
final term. its size makes a difference.
226
Smarts Leisure owns a chain of fitness centres throughout the north-west. Its head office is in Stockport.
Each fitness centre has its own wireless LAN. All the fitness centres are connected to head office via a WAN.
1(a) (i) Identify two ways in which a LAN differs from a WAN. (2 marks)
1 LAN on one site.
2 WAN covers a wider geographic area.
Radio waves are used to connect devices on a wireless network. Each device has a wireless network
adapter that sends and receives radio signals. A router broadcasts a wireless signal that devices can
detect and ‘tune’ into.
1(b) Files are divided up into packets before transmission. Two fields in a network packet are source
and destination.
Give two additional fields found in a network packet. (2 marks)
1 Sender
2 Sequence Number
1(c) Members of a fitness centre can log on to Smarts Leisure’s website to view details of their training.
Explain the server-side processing that takes place when a member views their
training online. (3 marks)
The server receives the request from the client computer and retrieves training data from the database.
It sends data to the client computer.
Social engineering involves tricking people into revealing confidential information. Hackers use a variety of
methods to get people to give away confidential information.
An attacker could call the Smarts Leisure help desk pretending to be an employee who has forgotten their
password. Staff might be fooled into helping them to log on remotely and reset their password.
Smarts Leisure must make sure that all their employees are aware of this kind of attack and know that
they should always verify a caller’s identity before giving them access to the network or parting with
sensitive information over the phone.
227
Verdict
1(a) (i) The student has only given one difference
Exam tip
between a LAN and a WAN, not the two
that were required. Each answer should They could have said that a LAN is relatively
compare a LAN with a WAN. inexpensive to set up and run, whereas a WAN
requires costly hardware and dedicated leased
1(a) (ii) The candidate has made three lines. Using ‘whereas’ is a good way of making sure
distinct points, demonstrating sound you compare one thing with another.
understanding.
1(b) There are lots of possible answers to this question, but you would get no marks for giving
source or destination as an answer, since these are provided in the question. 'Sender' is just
another name for 'source' so in this case the candidate only identified one additional field.
1(c) This question is worth three marks, suggesting that the examiner is expecting three
distinct points to be made. The student has only given two points so loses out on a mark.
They omitted to say that the server constructs
a web page to display results of a search. Exam tip
Your answers should take account of the context of
They have, however, related their answer to the question. You could miss out on marks by giving
the scenario, which is a sensible thing to do. generic responses.
228
Calculators are not allowed but you can use a stencil to help you draw flowchart symbols.
Pseudo-code
Some of the questions will use pseudo-code. It’s a good idea to familiarise yourself with the Edexcel pseudo-
code before the exam. But don’t worry if you don’t remember all the commands. You will have a clean copy of
the pseudo-code booklet to refer to in the exam. You don’t have to use the Edexcel pseudo-code in any of your
answers. Any version is acceptable providing it is unambiguous and easy to follow.
All the questions are compulsory. They are split into a number of parts, some more straightforward than others.
Here are some of the command words you might encounter in this paper.
Amend. Making changes. For example, amending an algorithm so that it performs differently or in order to
correct an error.
Assess. Judging or deciding the value, or appropriateness, of something. For example, assessing the
appropriateness of a particular data structure.
Complete, fill in. Filling in gaps. For example, completing a flowchart or a table.
Construct, draw. Creating something. For example, constructing a flowchart, an expression or a validation test.
Longer answers in this paper are likely to involve creating an algorithm expressed either in pseudo-code or as
a flowchart.
229
1 The Riverside is a large, out-of-town shopping mall with shops, restaurants and leisure facilities.
(a) The rent that retailers pay depends on the area of their unit (in square metres) and the
number of years they have been there.
The rental charge is £100 per square metre, with a discount of 1 per cent for each year
of occupancy. Construct a general expression showing the rent that a retailer will
have to pay. (2 marks)
(b) The Riverside wants to limit the discount it gives to no more than 10 per cent.
Write an algorithm expressed in pseudo-code to calculate the rent for each retailer,
taking into account the maximum discount. (2 marks)
2 The Riverside produces visitor statistics. Data about the number of people who visit the shopping
mall each day is collected and analysed.
Complete the table to show an input, process and output. (2 marks)
3 The number of security and medical staff required each day depends on how many visitors
are expected.
The pseudo-code for a subprogram that calculates the number of security and medical staff
required is shown.
230
1 PROCEDURE staff_required
2
3 SET days TO [80000, 30000, 30000, 50000, 55000, 60000, 100000]
4 SET useAgain TO ‘no’
5 WHILE useAgain = ‘yes’ DO
6 SEND ‘Enter day number (Sunday = 0)’ TO DISPLAY
7 RECEIVE dayNumber FROM (INTEGER) KEYBOARD
8 SET visitors TO days[dayNumber]
10 SET security TO visitors DIV 1000
11 SET medical TO visitors DIV 20000
12
13 SEND ‘Do you want to enter another day (yes or no)?’ TO DISPLAY
14 RECEIVE useAgain FROM (STRING) KEYBOARD
15 END WHILE
16 END staff_required
231
Loyalty NO
loyalty card End
card?
YES
Input
thisSale
Is NO
thisYear toPay = thisSale End
>= 250?
YES
Is NO toPay = thisSale – 5%
thisYear thisYear = thisYear End
>= 500? + thisSale
YES
End
5 Riverside tries to keep the temperature constant at 20°C and the humidity at 50 per cent by opening
air vents if it is too warm or too humid and closing them and using heaters when it is too cold.
Temperature and humidity sensors are located at various positions within the shopping mall. The
external temperature is also monitored.
The air vents are operated by electric motors, which rotate a bar clockwise to open them and
anticlockwise to close them.
If the temperature falls below 20°C the air vents are closed and the heaters are switched on. If the
external temperature is 26°C or above and the air vents are open, then the air vents are closed and
the air conditioning units are switched on.
(a) Complete the table to give the inputs and outputs of this system. (6 marks)
Inputs Outputs
Internal temperature Air vent motors
Humidity Heaters
External temperature Air conditioning unit
(b) Write an algorithm to control the temperature and humidity. Use pseudo-code. (9 marks)
232
Verdict
1(a) Order of precedence is key here. The student has used brackets so that the subtraction is done before
the multiplication. Remember BIDMAS! This answer was awarded two marks.
1(b) In this question there was one mark for getting the condition in the IF statement correct - ‘years > 10’
would also have been correct. The second mark was for correctly calculating the rent in both cases.
2 One mark is awarded for each empty cell correctly filled in with the correct information.
The candidate has given a full description of the process needed to identify the month with the highest
number of visitors.
233
3(a) The student has clearly identified the error and has indicated how it can be fixed but they
haven’t spelled it out. They should have given the correct code, i.e. SET useAgain TO ‘yes’.
3(b) This explanation is far too vague. The candidate probably knew that the DIV operator returns
the total number of times that one number divides into another without a remainder but should
have spelled it out to be sure of getting two marks.
3(c) One mark for using the append operator (ampersand) and one mark for using the correct
variables in the output statement.
3(e) The student has jotted down a list of separate benefits without any justification. A better
answer would have been:
The use of subprograms makes code clearer and easier to understand (1) because the main
program is shorter and doesn’t contain any repeated blocks of code (1) and because by putting a
name to a block of code it is easier to see at a glance what it does. (1)
4 The student has done a great job of completing the flowchart. Three marks are awarded
for functionality. In other words, a solution that works. The other three marks are for
correct notation.
5 There’s a lot of text at the start of this question, but don’t be tempted to skip it. Read
it carefully and underline key words, these are references to values that need to be stored
and processed.
5(a) The student achieved all six available marks for this question.
5(b) The student has written very clear, readable code and has earned all nine marks - three for
the functionality of the algorithm, three for the accuracy of the pseudo-code and three for the
efficiency of the solution.
This is a well thought-out response that includes keeping track of whether the heaters and air con
units are on or off and whether the air vents are open or closed.
A few more explanatory comments would have been helpful, including an explanation of the
subprogram check_if_open.
234
You must complete the project under controlled conditions. This means that you are not allowed to seek help
from anyone else and must only work on the project when supervised by your teacher. Use of the internet is
not permitted. But you are allowed access to a clean copy of the pseudo-code booklet, a syntax guide for the
programming language you are using and a flowchart stencil.
There are four stages to the project: analysis, design, implementation, and testing, refining and evaluation.
At the end of the project you must submit the program you have written, along with a report showing the
development process and including an evaluation.
Train company managers want to use the data that is collected each week to find out the average number
of passengers per day, the average service delay time and the average number of passengers travelling on a
particular service, e.g. the 06.48 train.
Your task is to analyse this problem and design, implement, test and evaluate a solution.
235
Sub-problems
Decompose the problem into a set of sub-problems. Briefly describe each sub-problem. Write a short
explanation of why you have decomposed the problem in this way.
I have decomposed the problem into a set of sub-problems that cover all of the specified requirements.
Data import: The data from the text file must be read in and stored in an appropriate data
structure. In the course of a year 104 text files will be created. The user will need to specify which
one they want to use. Currently there is no requirement to compare data for different weeks.
Menu: The program must provide the user with three menu choices –
(1) calculate daily average passenger numbers,
(2) calculate average delay, and
(3) calculate average passenger numbers for a specified service.
It must also give the user the option of exiting the program. User input must be validated so that
inappropriate data entry does not cause the program to crash.
Average number of passengers: The program must compute the average number of passengers per day.
To do this it needs to calculate the number of passengers who travelled each day and divide by the
number of services on that day. The results should be displayed as a table with two columns – Day
and Average number of passengers.
Average delay: The program must calculate the average service delay. To do this it needs to compute
the total amount of time taken up by delays in a given week and divide by the total number of
services in that week.
Service average: The program must prompt the user to select a particular service and compute the
average number of passengers who travelled on that service in the given week.
Each of the three menu options requires an average to be calculated. I plan to write a subprogram that
returns the average of a set of values that is passed to it. This will avoid code duplication and maximise
the efficiency of the solution.
I have broken the problem down in this way so that each sub-problem focuses on a specific
requirement. For example, the sub-problem ‘Average number of passengers’ deals with the
requirement to calculate and display the average number of passengers per day.
When I move on to design my solution I will write a subprogram to solve each sub-problem.
236
PROCEDURE Menu()
BEGIN PROCEDURE
SEND ‘Please select one of the following options:’ TO DISPLAY
SEND ‘1: Calculate average number of passengers’ TO DISPLAY
SEND ‘2: Calculate average delay:’ TO DISPLAY
SEND ‘3: Calculate the total number of passenger within a time period:’ TO
DISPLAY
SEND ‘4: Exit’
RECEIVE Choice FROM (Integer) KEYBOARD
IF Choice >= 1 AND Choice <=4 THEN
IF Choice = 1 THEN
avgPassengers()
END IF
IF Choice = 2 THEN
avgDelay()
END IF
IF Choice = 3 THEN
avgService()
END IF
IF Choice = 4 THEN
exitProgram
ELSE
SEND “Please select a valid option” TO DISPLAY
Menu()
END IF
END PROCEDURE
…
Activity 1
Complete the design for the service analysis program.
237
Exam tip
Don’t fall into the trap of only testing that your program works when valid data is entered. You should
use normal, boundary and erroneous test data to ensure that your program runs correctly in all
circumstances. Don’t forget you’ll need to test that the subprograms interact as intended.
Try to make your test plan as comprehensive as possible. It might help to ‘walk through’ your solution mentally,
jotting down anything that needs testing as you go along. Pose ‘what if’ questions as key points.
It’s not sufficient to have a strategy in your head. You need to summarise what your tactics are and explain
why you think this approach will work.
Activity 2
Copy and complete the test plan. Ensure the tests cover all
aspects of the planned program.
238
The report
You will need to gather evidence of the work you have done during the project and submit it as a report. This is
what should be included.
Stage 1 – Analysis • A brief summary of the problem
• A list of the requirements of the problem
• A list of the sub-problems you have identified, together with a brief description of
each one
• A short explanation of your rationale for breaking down the problem in the way you have
Stage 2 – Design • Your algorithms
• A brief description of your test strategy
• Your initial test plan with the first four columns filled in
Stage 3 – • A copy of your program code
Implementation • A couple of screenshots showing debugging in action
Stage 4 – Testing, • The completed test plan with all six columns filled in and extra lines added if required
refining and • The evaluation of your program
evaluation
239
240
Checksum: an error detection technique. A mathematical Concatenation: the linking together of two or more items
formula is applied to the data and the resulting numerical of information.
value is transmitted with the data. The recipient computer Concurrent: processes that run apparently at the same
applies the same formula to the received data and time are described as being concurrent.
then compares the checksum sent with the data to the
Constant: a ‘container’ that holds a value that never
calculated checksum. If the checksums don’t match the
changes. Like variables, constants have unique identifiers.
data is likely to have been corrupted and the recipient
computer requests the data again. Construct: a component from which something is built.
Letters and numbers (i.e. a to z and 0 to 9) are the constructs
Client–server network: a network that has at least one
we use to build our language and convey meaning. Bricks
server to provide services to the client computers.
and cement are the basic constructs of a building.
Clock: an electronic device inside a CPU that ‘ticks’ at
regular intervals and is used to synchronise the actions of Control unit: the part of the CPU that organises the
the other parts of the CPU. actions of the other parts of the CPU.
Cloud storage: secondary storage, often belonging to Creative Commons: an organisation that allows people to
a third party, that is accessed via a network, usually the set copyright terms for their intellectual property. One use
internet, and so is not in the same physical place as the of a Creative Commons licence is to allow people to copy
machine’s RAM/ROM. Files stored ‘in the cloud’ can be material as long as it is not used commercially.
accessed from anywhere via an internet connection. Cyberattack: any kind of malicious attack on a network-
Code vulnerability: a computer program (the code) that connected device.
has been written in such a way that it creates a security Data structure: an organised collection of related
issue that may be taken advantage of to gain access to the elements. Arrays and records are two common data
computer system or data within it. structures used in programming.
Colour depth: the number of bits used to encode the Data type: specifies what kind of data it can hold. Common
colour of each pixel. data types are integer, real, Boolean and character. The
Communication media: the means by which data is data type of a value determines the operations that can be
transmitted between devices on a network. Coaxial performed upon it.
cable, fibre-optic cable and microwaves are all forms of Database: an organised store of data.
communication media.
Decimal prefix: multiplies a unit by powers of 10.
Compiler: a translator that converts high-level language
Decomposition: breaking a problem down into smaller,
source code into object code, often machine code. The
more manageable parts, which are then easier to solve.
source code is translated all at once and saved to be
executed later. Definite iteration: this is used when the number of
Compound key: a key that consists of two or more fields iterations, or turns of the loop, is known in advance. It can
used to identify a record uniquely. be set to as many turns as you want. This sort of loop is
said to be count controlled.
Compression: changing the format of a data file so that
the size of the file becomes smaller. Defragmenter: a utility that moves file clusters on a
disk so they are closer to each other in order to speed up
Computational thinking: the thought processes involved
disk access.
in formulating problems and their solutions so that the
solutions are represented in a form that can be effectively Denial of service (DoS): an attack on a network that
carried out by a computer. attempts to prevent legitimate users from accessing
its services.
Computing technology: an all-encompassing term
referring to the hardware, software and infrastructure that Descending order: this is arranging items from largest to
underpin current and emerging computer systems. smallest (e.g. 6, 5, 4, 3, 2, 1 or f, e, d, c, b, a).
241
242
IF…THEN…ELSE statement: the IF…THEN…ELSE Logic circuit: an electronic circuit that has inputs and
statement allows a choice to be made between two outputs that follow one of the Boolean operators.
alternatives based on whether or not a condition is met Logic error: an error in an algorithm that results in
(e.g. IF it is cold THEN wear a jumper ELSE wear a T-shirt). incorrect or unexpected behaviour.
Indefinite iteration: this is used when the number of Logical operator: a Boolean operator using AND, OR
iterations is not known before the loop is started. The and NOT.
iterations stop when a specified condition is met. This sort
Lossless compression: compressing a file in such a way
of loop is said to be condition controlled.
that it can be decompressed without any loss of data.
Infinite loop: a loop that is never-ending since the
Lossy compression: compression where some of the data
condition required to terminate the loop is never reached.
is removed; the original file cannot be restored when the
Initialisation: the process of assigning an initial value to lossy file is decompressed.
a variable.
Low-level programming language: a programming
Input: to enter data into a computer. language that is closely related to the CPU’s machine code.
Instruction set: the list of all possible commands a Machine code: the binary codes representing each of the
particular CPU knows how to carry out. instructions in the instruction set.
Integer: a whole number (e.g. 3, 6, 9). Magnetic storage: secondary storage that works by
Integrated Development Environment (IDE): a package magnetising parts of a substance as north and south poles
that helps programmers to develop program code. It has a to represent binary 1s and 0s.
number of useful tools, including a source code editor and Main memory/random-access memory (ROM): a
a debugger. One of the most useful features of an IDE is temporary store for data and instructions (programs).
the debugger. One of its tasks is to flag up syntax errors in
Malware: short for ‘malicious software’. It is used as a
the code and issue helpful error messages.
generic term for any kind of software that is designed to
Intellectual property (IP): a creation of the human mind disrupt the use of a computer system.
that is unique and has a commercial value.
Median: the middle number when the numbers are put
Internet: a worldwide system of interconnected networks in ascending or descending order (e.g. if there are 13
that enables information to be exchanged and shared. numbers, then the 7th number is the median). If there
Internet Service Provider (ISP): an organisation that are an even number of items in an ascending list, choose
provides its customers with a connection to the internet. the item to the right of the middle (e.g. if there are 10
Interpreter: a translator that converts high-level numbers, then choose the 6th as the median).
language source code into object code, often machine Memory address: a number that uniquely identifies a
code. The source code is translated and executed one line (memory) storage location.
at a time. Mnemonic: a short, simple, acronym that represents each
Iteration: a construct that means the repetition of a of the instructions in a CPU’s instruction set, e.g. LDR (load
process. An action is repeated until there is a desired register), STR (store) and CMP (compare).
outcome or a condition is met. It is often referred to as Modular testing: testing each block of code as it is
a loop. completed to ensure the code works as expected.
Local area network (LAN): a network that covers a Monte Carlo methods: carrying out a statistical analysis
relatively small geographical area, often a single site. of a number of random samples in order to obtain
Local variable: a variable that is accessed only from within approximate solutions to a problem. The larger the number
the subprogram in which it is created. of samples used, the more accurate the result is likely to be.
Location-based services: services that enable people to Most significant bit (MSB): the bit with the highest value
access and share real-time location information online. in a multiple-bit binary number.
243
Runtime error: an error that occurs while the program Syntax error: an error that occurs when a rule of the
is running – the operation the computer is asked to do is programming language is broken.
impossible to execute. Table: a collection of cells organised in rows and columns
Sampling: taking measurements of the sound wave at used to store data in a structured and organised manner.
regular but distinct intervals of time (e.g. 44,100 samples Text file: a sequence of lines, each of which consists of
per second). a sequence of characters.
Scheduling: the algorithm that the OS uses to allow each Trace table: a technique used to identify any logic errors
running process to use the CPU. in algorithms. Each column represents a variable or output
Scope: the region of code within which a variable is visible. and each row a value of that variable.
245
246
248
249
250
251
252
253
254
255
you need for your written exams as well as your practical Computer Computer
Science Science
assessments.
REVISION REVISION
Through an engaging introduction to the core principles, GUIDE WORKBOOK
you will develop skills in problem solving and computational
thinking. Fo
r the
r the
9–1 Fo
9–1
Other key features of the Student Book include:
n
iti o
exa ms
Inc
ed
de exa ms
lu
s fr e n e
e o n li
A LW AY S L E A R N I N G A LW AY S L E A R N I N G
• in-depth coverage of the core content you need for all ISBN: 9781292131207 ISBN: 9781292131191
components of your course
• a ‘Preparing for your exam’ section with tips and guidance
for achieving success in your written exams
• ‘Exam-style questions’ and ‘Exam tips’ throughout to give
you plenty of practice at answering the kind of questions
you might find in the exam
• ‘Checkpoint’ features to check your knowledge and
challenge your understanding
• ‘Worked example’ features to help you understand how to
carry out practical skills before you try an activity yourself.
Computer Science
Series Editor: Ann Weidmann
www.pearsonschools.co.uk T 0845 630 33 33 Authors: Chris Charles Alex Hadwen-Bennett David Waller Jason Welch Shaun Whorton
myorders@pearson.com
A LW AY S L E A R N I N G