You are on page 1of 68

GCSE COMPUTER SCIENCE

Topic 1 – Problem Solving


1.1 Algorithms
Algorithms

An algorithm is a precise step-by-step set of instructions for solving a problem or carrying out a task

They are often used to improve efficiency by removing the need for human input

A computer following an algorithm can decide what decision to make far more quickly than a human

Book
Algorithms

Watch this video to find out more about algorithms

Link: https://www.youtube.com/watch?v=jWWOM53Zh20
Algorithm Constructs

The three constructs used in algorithms are: sequence, selection and iteration

These are the same three constructs used in programming

Once an algorithm has been written, it can be reused with slight changes for solving similar problems, which is much
quicker than starting over each time

Book
Algorithm Constructs

Sequence Instructions are carried out in order one after the other

Selection Allows a choice to be made between different instructions

Iteration Instructions are repeated until a desired outcome or a condition is met (sometimes called a loop)

You can see examples of these constructs on the next slide

Book
Algorithms

Example Algorithm with Constructs

1 Person A thinks of a number between 1 and 5


Sequence
2 Person B makes a guess

If the guess is too high:


3 (a) person A says “Too high”
(b) person B goes back to step 2

If the guess is too low: Iteration


4 (a) person A says “Too low” Selection
(b) person B goes back to step 2

5 If the guess is correct person A says “Correct”


Activity

Worked Example 1 Fill the kettle with water Sequence

Here is an algorithm to make a cup 2 Turn on the kettle Sequence


of tea (think of the steps you take!)

3 Place tea bag in the cup Sequence


It should be annotated to show
where sequence, selection and
iteration have been used 4 Pour water into the cup Sequence

5 Add one spoon of sugar if required Selection

6 Repeat until desired amount of sugar Iteration

Remember: There is usually more


than one correct answer 7 Add milk into the cup if required Selection
Book
Activity

Now Try These

1 Making a slice of toast 2 Guessing a number between 1 and 20

1. Slice bread
2. Put bread in toaster 1. Set number to guess (11)
3. Turn on the toaster 2. Other person guesses
4. Wait until toaster pops 3. If too high, say too high and try again
5. Check if toasted enough, if not re-toast 4. If too low, say too low and try again
6. Add butter 5. If correct say correct
7.Add jam
Activity continued

Now Try These

3 Getting from room 4.1 to room 1.14 4 Steps taken when the fire alarm goes off

1. Exit room 4.1 1.Remain calm


2.Turn 90 degrees left/anti-clockwise 2.Leave all belongings
3. Walk until staff staircase 3. Exit room 1.14
4. Enter staircase and descend until floor 1 4. Walk forward 15 metres
5. Turn 90 degrees right /clockwise
5. Exit staircase and turn 90 degrees
6. Walk several steps forward and enter student
left/anti-clockwise staircase
6. Walk forward 40 meters 7. Descend staircase and exit building
7. Turn 90 degrees right /clockwise 8. Walk clockwise around the school until MUGA
8. Walk forward 15 metres 9. Locate correct tutor group
10. Line up silently
9. Enter room 1.14
Algorithms

There are four methods of writing algorithms

On the next four slides we will look at the same algorithm written in each method
Types of Algorithms

There are four methods of writing algorithms

All algorithms can be represented in each of these methods

The four types are..


Types of Algorithms

1 Written Description

Step by step instructions written in plain English

Book
Types of Algorithms

2 Flowchart

A graphical method of showing the flow of information using a series of symbols and arrows

Book
Types of Algorithms

3 Pseudocode

Uses structured English and keywords with a heavy focus on the order of the instructions (the logic)

Half way between plain English and a programming language

Book
Types of Algorithms

4 Program Code

Code that is produced using a programming language such as Python

Book
Algorithms

Watch this video to find out more about algorithms

Link: https://www.youtube.com/watch?v=McM9YCeyt_Q
Flowchart Symbols

Algorithms can be displayed as flowcharts

Terminal Shows the start and end of an algorithm

Shows yes/no or true/false decisions where there are two


Decision/Selection
possible outcomes

Process Shows data processing, such as a calculation

Input / Output Indicates an input or output

Subroutine Shows a function or procedure that has its own flowchart

Line Represents control passing between the other symbols


Book
Flowcharts

Example Flowcharts (Easy)

Start Terminal
Start Terminal

Print “Hello
Output
world” Print “Mr” Output

End Terminal
Print “Lyon” Output

End Terminal

Change this to your name!

Book
Flowcharts

Example Flowchart (Medium)

Start Terminal

Process
Alarm Rings

Ready NO
Hit snooze
to get Process
button
up? Decision
YES

Get out of bed Process

End Terminal
Book
Flowcharts

Example Flowchart (Difficult 1)

Start Terminal

Input mark1,
mark2, mark3
Input

Decision
NO
Is mark1 > Is mark2 > NO
mark2? mark3?
Decision
YES YES Output Output
NO Output Output
Output Is mark1 > mark2 mark3
Output mark3 mark3?
Decision
YES
End End
End
Output
Terminal Terminal
Terminal mark1 Output

DO NOT DRAW THIS FLOWCHART End


Terminal
IT IS ON THE SHEET Sheet
Flowcharts

Example Flowchart (Difficult 2)

On the next slide is a flowchart used by a theme park to calculate how much a group of people should pay for entry

Part 1
Flowcharts
Start
1
2
Example YES
Is age >=
Flowchart Charge = 0 60? Charge = £9
(Difficult 2) Total = 0
Number = 0 NO

Total = Total + Charge


Input
customer

Is there YES
Charge = £10 another
Number = Number + 1 customer in
the group?

NO
Input age
YES
Is Number
> 4? Total = Total - £10

YES
Is age < Charge = £5 NO
13? Number = Number - 1
Output
Total
NO
Part 2 1 2
End
Activity

Example Flowchart (Difficult 2)

1 List the variables that are used in the algorithm

2 Label where each of the three constructs (sequence, selection and iteration) are used

3 Describe how the algorithm calculates the total amount that should be paid

The Smith family are visiting the park. The family consists of two children, one aged 8 and one aged 10, their two
4
parents and their grandfather, who is aged 65. Use the algorithm to calculate how much the family should pay

Part 3
Activity

Answers

1 Charge, total, number, customer, age

Sequence: First two boxes


2 Selection: The three decisions
Iteration: Where it loops to input another customer

The regular charge is £10, but children under 13 pay £5 and people aged 60 or above pay £9. A group consisting of
3
five or more adults get a discount of £10.

(2 × £5) + (2 × £10) + £9 = £39 (They are not eligible for a group discount: there are five people, but the
4
variable 'number' comes to 3, not 5. Why is that?)

Book
Activity

Calculate the Area of a Rectangle


Start Terminal

Input
Input
width

Input Input
height

Area = width
Process
* height

Output
Output
area

End Terminal

Sheet
Activity
Start Terminal
Making a Cup of Tea
Fill up and turn
kettle on Process Guessing a Number Between 1 and 20 Written Description
Process
Kettle
NO Set number to 11
boiled? Wait longer User inputs a guess
Decision
If guess is the same as the number, display ‘well done’
YES Else output ‘unlucky, try again’ and allow another guess
Add tea bag and pour
Process
water into cup

Take Add sugar Process


sugar?
Decision YES
NO
Add milk Process

End Terminal
Sheet
Activity

Start Terminal Guessing a Number Between 1 and 20

number = 11 Process Written Description

Set number to 11
Input guess Input User inputs a guess
If guess is the same as the number, display ‘well done’
Decision Else output ‘unlucky, try again’ and allow another guess
Does Output
guess =
number?
“unlucky, try
NO Output
again”
YES
Output
“well done” Output

End Terminal

Sheet
Activity

Making a slice of toast

Sheet
Pseudocode

Algorithms can be displayed using Pseudocode

A way of expressing an algorithm in structured English that resembles computer language

Does not use strict syntax, it just needs to be clear and consistent

Book
Pseudocode

Use of Pseudocode

Uses commands, keywords and structures similar to those found in computer languages

Cannot be understood by computers, but is used to develop the logic of a program without needing to worry about the
syntax

A human can follow the logic of an algorithm even if there are spelling mistakes, missing brackets or quotation marks
etc.

A solution in pseudocode can then be converted into a high-level language such as Python

Sheet
Pseudocode

An Algorithm in Pseudocode: Example 1

Written Description Flowchart Pseudocode

Start

SEND ‘Enter first number’ TO DISPLAY


Enter num1 RECEIVE num1 FROM (INTEGER) KEYBOARD
SEND ‘Enter second number’ TO DISPLAY
Enter first number Enter num2 RECEIVE num2 FROM (INTEGER) KEYBOARD
Enter second number
Add together to get the total total = num1 + SET total TO num1 + num2
Output the total num2 SEND total TO DISPLAY

Output END
total

End
Pseudocode

An Algorithm in Pseudocode: Example 2

Written Description Flowchart Pseudocode

Start SEND ‘Enter your name ’ TO DISPLAY


RECEIVE name FROM (STRING) KEYBOARD
Enter name
SEND ‘Enter a colour’ TO DISPLAY
Enter your name
Enter colour RECEIVE colour FROM (STRING) KEYBOARD
Enter your favourite colour
If the colour chosen is red
If NO Output IF colour = red THEN
display ‘That is my favourite colour ‘That is SEND ‘That is my favourite colour’
colour’ = red not..’
TO DISPLAY
Else if the colour chosen is YES
Output ELSE
anything else display ‘That is ‘That is also End SEND ‘That is not a nice colour’
not a nice colour’ my fav..’
TO DISPLAY
End END IF
END
Pseudocode

An Algorithm in Pseudocode: Example 3

Written Description Flowchart Pseudocode

Start
SEND ‘Enter age’ TO DISPLAY
Enter age
RECEIVE age FROM (INTEGER) KEYBOARD
Enter your age
NO
If the age is larger than 17 set If age >
type = child
IF age > 17 THEN
17
the type to adult SET type TO adult
Output
Else if the age is less than or YES ‘child’
ELSE
equal to 17 set the type to type = adult SET type TO child
End
child END IF
Output
Display the type ‘adult’
SEND type TO DISPLAY
End
END
Algorithms

Purpose of an Algorithm

You need to be able to work out what the purpose of an algorithm is


Algorithms

Purpose of an Algorithm

Flowchart Copy the following flowchart. On your diagram


label the following:

ACTIVITY
Start Terminal
- processes
Iteration - decisions
weight = weight
of parcel Process - terminals
- iteration
Decision
is NO - input/outputs
weight Output
cost = 2
>2 cost
YES Process
Output
excess
Process
= weight - 2

cost
= 2 + (excess * 3) Process
Algorithms

Purpose of an Algorithm

Flowchart Pseudocode

Start SET parcel TO ‘y’


WHILE parcel = ‘y’ DO
weight = weight RECEIVE weight FROM (FLOAT) KEYBOARD
of parcel IF weight <= 2 THEN
SET cost TO 2
is NO ELSE
weight Output
cost = 2 SET excess TO weight – 2
>2 cost
YES SET cost TO 2 + (excess * 3)
excess END IF
= weight - 2 SEND cost TO DISPLAY
SEND ‘press y for another parcel’ TO DISPLAY
cost RECEIVE parcel FROM (STRING) KEYBOARD
= 2 + excess x 3 END WHILE
Algorithms

Purpose of an Algorithm

Pseudocode Explanations

SET parcel to ‘y’ While loop ensures the program keeps running
WHILE parcel = ‘y’ DO
RECEIVE weight FROM (FLOAT) KEYBOARD User enters the weight of the parcel
IF weight <= 2 THEN
SET cost TO 2 If/else statement to work out the cost of the
ELSE parcel depending on if the weight is above or
SET excess TO weight – 2 below 2
SET cost TO 2 + (excess * 3)
END IF
SEND cost TO DISPLAY Outputs the final cost depending on the weight
SEND ‘press y for another parcel’ TO DISPLAY User is asked if they wish to run the program again
RECEIVE parcel FROM (STRING) KEYBOARD
END WHILE Programs ends if ‘y’ is not entered
Algorithms

Purpose of an Algorithm

Pseudocode Questions Answers

SET parcel to ‘y’ weight = 2 cost = 2


WHILE parcel = ‘y’ DO
RECEIVE weight FROM (FLOAT) KEYBOARD weight = 3 cost = 5
IF weight <= 2 THEN
SET cost TO 2 weight = 1.5 cost = 2
ELSE
SET excess TO weight – 2 weight = 5 cost = 11
SET cost TO 2 + (excess * 3)
END IF weight = 10 cost = 26
SEND cost TO DISPLAY
SEND ‘press y for another parcel’ TO DISPLAY
RECEIVE parcel FROM (STRING) KEYBOARD
END WHILE
Output of an Algorithm

A trace table is used to identify the values of variables, inputs and outputs of a program at each stage

Trace tables have columns for each of these values

You will practice the trace tables on the next slides

Book
Output of an Algorithm

Trace Tables – Worked Example

SET num1 TO 4 num1 index output


FOR index FROM 1 TO 4 DO 4
SET num1 TO num1 + index
SEND num1 TO DISPLAY 5 1 5
NEXT FOR 7 2 7
10 3 10
14 4 14

Book
Activity

Trace Tables – Example 1

SET digit TO 7 digit index output


FOR index FROM 1 TO 5 DO 7
SET digit TO digit * index
SEND digit TO DISPLAY 7 1 7
NEXT FOR 14 2 14
42 3 42
168 4 168
840 5 840

Book
Activity

Trace Tables – Example 2

SET num TO 3 num index output


FOR index FROM 1 TO 3 DO 3
SET num TO (num * index) + 2
SEND num * 3 TO DISPLAY 5 1 15
NEXT FOR 12 2 36
38 3 114

Book
Errors

You need to be able to identify and fix errors in algorithms

There are three types of errors in computer programs:

Syntax errors occur when algorithms are converted into program code

Runtime errors occur when the program is executed

Logic errors are errors in the design of algorithms

Pseudocode does not have syntax or runtime errors, so the user can focus on the logic of the program

Sheet
Errors

Example 1 Fix and correct the errors in this algorithm

SET index TO 1

Question
WHILE index < 10 DO
SEND index TO DISPLAY
END WHILE

SET index TO 1
WHILE index < 10 DO

Answer
SEND index TO DISPLAY
SET index TO index + 1
END WHILE

Sheet
Errors

Example 2 Fix and correct the errors in this algorithm

SET index TO 1

Question
WHILE index < 10 DO
SEND index TO DISPLAY
SET index TO index - 1
END WHILE

SET index TO 1
WHILE index < 10 DO

Answer
SEND index TO DISPLAY
SET index TO index + 1
END WHILE

Sheet
Errors

Example 3 Fix and correct the errors in this algorithm

SET index TO 1

Question
WHILE index < 1 DO
SEND index TO DISPLAY
SET index TO index + 1
END WHILE

SET index TO -5 #or any number less than 1


WHILE index < 1 DO

Answer
SEND index TO DISPLAY
SET index TO index + 1
END WHILE

Sheet
Standard Algorithms

You know an algorithm is a precise step-by-step set of instructions for solving a problem or carrying out a task

Two of the most common tasks in computer programs are sorting data into a particular order and searching for
particular items of information
Standard Algorithms

7,9,12,4,6,3,7,98,9,2,8,4,5,6,3,2,1,4,7,8,1,1,2,3,6,5,9,8,7,4,1,5,8,56,11,45,101
There might be thousands of items of stored data and searching for information would be very inefficient if the data
was not sorted

All sorting and searching algorithms work on lists of data. But variables only store a single value, so we need to use an
array, sometimes called a data container

SET friend1 TO ‘Andy’ ARRAY


Standard
SET friend2 TO ‘Tom’
variables
SET friend3 TO ‘Sarah’ An organised collection of
related values

friends = [‘Andy’, ‘Tom’, ‘Sarah’] Array

Book
Standard Algorithms

You need to understand two sorting algorithms and to searching algorithms

Sorting Algorithms

Bubble Sort Merge Sort

Searching Algorithms

Linear Search Binary Search


Standard Algorithms

Sorting Algorithms

As sorting is such a widely used procedure, many algorithms have been created to carry it out

As with all algorithms, some are more efficient than others

Bubble Sort Merge Sort

Less efficient More efficient

Book
Activity

Bubble Sort Demonstration

Unsorted
2 1 3 6 5 4 1 2 3 5 4 6
List

1 2 3 6 5 4 1 2 3 5 4 6

1 2 3 6 5 4 1 2 3 5 4 6
First pass Second pass
1 2 3 6 5 4 1 2 3 5 4 6

1 2 3 5 6 4 1 2 3 4 5 6
Sorted
1 2 3 5 4 6 List 1 2 3 4 5 6
Activity

Bubble Sort Demonstration

See other presentation


Activity

Bubble Sort Cards


Activity

Bubble Sort Practice Question 1

92 30 56 42

30 56 42 92 Pass 1

30 42 56 92 Pass 2

30 42 56 92 Pass 3
Activity

Bubble Sort Practice Question 2

76 85 98 27

76 85 27 98 Pass 1

76 27 85 98 Pass 2

27 76 85 98 Pass 3

27 76 85 98 Pass 4
Activity

Bubble Sort Practice Question 3

99 35 59 26

35 59 26 99 Pass 1

35 26 59 99 Pass 2

26 35 59 99 Pass 3

26 35 59 99 Pass 4
Standard Algorithms

Bubble Sort Summary

The bubble sort algorithm works through a list, comparing pairs of values and swapping them if necessary

Each complete traversal of the list is called a pass

The process is repeated until there are no swaps during a pass, indicating that all items must be in the correct order

Book
Standard Algorithms

Bubble Sort Activity

https://goo.gl/FrEQSK
If you pass 50 points you have mastered bubble sort

Who will be the first to reach 100 points?


Activity

Merge Sort Demonstration

6 3 5 1 8 2 4 7 Unsorted List

6 3 5 1 8 2 4 7

6 3 5 1 8 2 4 7

6 3 5 1 8 2 4 7

3 6 1 5 2 8 4 7

1 3 5 6 2 4 7 8

1 2 3 4 5 6 7 8 Sorted List
Activity

Merge Sort Practice Question

12 9 13 4 1 22 7 11 Unsorted List

12 9 13 4 1 22 7 11

12 9 13 4 1 22 7 11

12 9 13 4 1 22 7 11

9 12 4 13 1 22 7 11

4 9 12 13 1 7 11 22

1 4 7 9 11 12 13 22 Sorted List
Standard Algorithms

Merge Sort Summary

A merge sort divides a list into two smaller lists and then divides these until the size of each list is one

The left and right lists are recursively (repeatedly) merged together with the items in the correct order

Book
Standard Algorithms

Searching Algorithms

To find a specific item in a list involves carrying out a search

Like sorting, some methods of searching are more efficient than others

Linear Search Binary Search

Basic and inefficient More complex and efficient

Book
Activity

Linear Search Demonstration

3 8 1 4 7 6 9 2 12 11 10 5

Why is this method inefficient? Searching for the number 7


Activity

Linear Search Practice Question

3 7 1 4 11 6 28 2 12 85 17 5

How does it find the number 11? Searching for the number 11
Standard Algorithms

Linear Search Summary

Linear search starts at the first element in the list and checks every element until it finds the one it is looking for

The problem with this method is it isn’t very efficient

If the value it was searching for was at the end of the list it would take a long time

Book
Activity

Binary Search Demonstration

0 1 2 4 5 6 8 10 12 13 14 15

What do you notice about how the numbers are arranged? Searching for the number 14
Activity

Binary Search Practice Question

18 19 20 21 22 23 24 25 26 27 28 29

Searching for the number 28


Standard Algorithms

Binary Search Summary

Binary search is a good way of searching lists that have been sorted (also called pre-processed)

At each step it halves the number of items it needs to look at until it finds the correct item

Book

You might also like