2.1 Workbook Answers
2.1 Workbook Answers
1 | Algorithms Craig’n’Dave
Resources
We recommend the OCR-endorsed text book from PG Online for use during your GCSE studies.
Craig ‘n’ Dave videos for SLR 2.1
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Abstraction
Abstraction means: Including the necessary details and leaving out the unnecessary details when solving a problem.
Example of abstraction:
• Tail
• Windows
• Decals
• Engines
• Landing gear
• Cockpit
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Abstraction
Cat: Cat icon: Necessary features of the icon: Unnecessary features of the icon:
Dog: Dog icon: Necessary features of the icon: Unnecessary features of the icon:
Rabbit: Rabbit icon: Necessary features of the icon: Unnecessary features of the icon:
Abstraction
A computer program outputs whether a capital city in Europe is north or south of another capital city in Europe. It only needs to know the latitude of the two
cities – the other detail is unnecessary. This is an example of abstraction; including the necessary detail and not including the unnecessary detail.
Dublin 53.3498
London 51.5074
Oslo 59.9139
Paris 48.8566
Madrid 40.4168
Program:
city = input("Enter city:")
latitude = float(input("Enter
latitude:"))
Decomposition
Decomposition means: Breaking a problem down into smaller sub-problems, making the overall problem easier to solve.
1. Start by getting your toppings ready – take your 1. Preheat the oven to 180C/350F/Gas Mark 4.
butter out of the fridge so it can come up to 2. Line your cake tin with paper cases.
[Picture of toast room temperature. [Picture of fairy 3. Cream the butter and sugar together in a bowl until
here] 2. Get your jam or marmalade out of the fridge or cake here] the mixture is pale. Beat in the eggs a little at a time
cupboard. and stir in the vanilla extract.
3. Get your plate and knife ready. 4. Fold in the flour using a large metal spoon.
4. Cut yourself two slices of bread. 5. Add a little milk until the mixture is a soft dropping
5. If you're using a toaster, set the control for how consistency and spoon the mixture into the paper
well-done you want the toast to be before you cases until they are half-full.
put it in. Push the button down and the toast 6. Bake them in the oven for 8 – 10 minutes or until
will pop up when it's ready. they’re golden-brown on top and a skewer inserted
6. If you’re using the grill, preheat it to high. into one of the cakes comes out clean.
7. Once it's nice and hot, put your slices of bread 7. Set them aside to cool for 10 minutes, then remove
under the grill to toast. them from the tin and place them on a wire rack.
8. Keep an eye on it, and when you've got a good 8. For the icing, sift the icing sugar into a large mixing
colour, carefully pull them out and flip each slice bowl and stir in enough water to create a smooth
over with some tongs. mixture. Stir in the food colouring.
9. Toast the other side until it’s done. 9. Drizzle the icing over the cakes, sprinkle them with
10. Spread the butter on the toast. decorations and set them aside until the icing
11. Spread the topping on the toast. hardens.
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Advantages of decomposition include: 1. Making problems easier to solve by breaking them down.
2. Different people can work on different parts of a problem, reducing development time.
3. Program components developed in one program can be reused in other programs.
Red beads
Chain
Purple beads
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
• Programs are easier to write – a solution can be reached quicker. Wages program
• Programs are easier to test.
• Programs are less prone to mistakes and errors during development.
• Multiple programmers can work on a program at the same time.
• If program requirements change during development, which is quite
common, the program is easier to change. Calculate
Calculate wage Print wage slip
• Maintaining the program is easier. deductions
Calculate basic
Calculate tax
wage
Calculate
Calculate
National
overtime
Insurance
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Pupil
management
program
Pupil
Pupils on roll LEA funds
attendance
New pupil
Expenditure
application
Remove pupil
Clearly, there are many other possible solutions to this problem – and that in
itself can be the basis for a decent class discussion.
A process is: Anything which happens to information or data during a programs execution e.g. performing calculations or convertions
width : real
Swimming pool volume Calculates the total volume length : real Volume = width * length * volume : real
of a swimming pool depth
depth : real
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Algorithmic thinking
Algorithmic thinking
Linear search
Explanation of a linear search: Starting from the beginning of a list, each item is checked in turn to see if it is the one being searched for.
Pseudocode of the linear book = ["Archaeology", "Art", "Biology", "Chemistry", "Computing", "English",
search algorithm: "French", "Geography", "History", "Maths", "Psychology"]
found = False
number = 0
find = "Geography"
WHILE found == False AND number < LEN(book):
IF book[number]==find:
found = True
number = number + 1
IF found == True:
PRINT “True”
ELSE:
PRINT “False”
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Binary search
Explanation of a binary search: Calculate the mid-point and check if that is the item being searched for. If not, and the item is lower than the mid-point,
repeat on the left half of the list. If the item is higher than the mid-point, repeat on the right half of the list.
Special condition for a binary search to work: The data items must be in order.
In most cases, the quicker search is the: binary search algorithm. However, this is not true if the first item in the list is the one being searched for.
If the item you want to find is first in the list, the linear search algorithm would be quicker.
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Binary search
Pseudocode of the binary book = ["Archaeology", "Art", "Biology", "Chemistry", "Computing", "English",
search algorithm: "French", "Geography", "History", "Maths", "Psychology"]
found = False
left = 0
right = LEN(book)-1
find = "Geography"
IF book[mid]==find:
found = True
ELSE:
IF find>book[mid]:
left = mid + 1
ELSE:
right = mid - 1
IF found == True:
print("True")
ELSE:
print("False")
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Bubble sort
How a bubble sort works: Note how 32 has “bubbled” to the top – The algorithm has been optimised, so it does not check the
this is how the bubble sort got its name. numbers already bubbled to the top. It can also stop if no
swaps are made after all the numbers are checked.
24 24 24 24 32 32 32 32 32 32
8 8 8 32 24 24 24 24 24 24
16 16 32 8 8 8 16 16 16 16
2 32 16 16 16 16 8 8 8 8
32 2 2 2 2 2 2 2 2 2
Check 2 Check 32 Check 32 Check 32 Check 2 Check 16 Check 16 Check 2 Check 8 Check 2
and 32 and 16 and 8 and 24 and 16 and 8 and 24 and 8 and 16 and 8
Swap Swap Swap Swap No swap Swap No swap No swap No swap No swap
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Merge sort
82 82 82
82 82
9 9 9
9 43
3 3 43 38
43
43 43 3 27
38
27 9
27 38 27
38 3
38 27 3
Original list Split list until Swap number Merge adjacent Until all lists
lists have two pairs if necessary lists together are merged
numbers
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Merge sort
24 24 24
Step
24 H 32
8 16
Ste I
8
pC 32 Step 24
16 8
D
16 ep 16 16
St Step G
Ste
pB
32 32 32 8 8
Step F
2 2 2 2 2
Step A Step E
Original Split into Swap numbers Merge adjacent lists together Merge adjacent lists together
list adjacent sub- if necessary in by comparing the first number by comparing the first number
lists of up to two each sub-list in each list, moving the in each list and moving the
numbers 8 and 16 swap smaller number into a new smaller number into a new
list, one number at a time list, one number at a time
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Insertion sort
24 24 24 24 24
Yellow dotted box:
unsorted data
8 8 8 8
32
16 16 16
32 24
2 2
32 16 16
32
32 16 8 8
8
Green solid box:
sorted data 32 2 2 2 2
Flowchart symbols
Is choice No
>0 and <4
Yes
END
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Should be OR
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
END
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
do
dice = random(1,12)
total1 = dice + p1attack + p1defence
dice = random(1,12)
total2 = dice + p2attack + p2defence Eliminate a draw resulting in player 2
being declared the winner by re-running
if total1 > total2 then the algorithm with new random numbers.
print("Player 1 wins")
endif
if total1 < total2 then
print("Player 2 wins")
endif
until total1 != total2
Note: Python does not support DO loops, so for a Python program, you will
need this code instead of the DO UNTIL structure:
total1 = 0
total2 = 0
while total1 == total2:
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
No
m="W"
Yes
AND
vm[3]=1
No
Return Return
False True
END
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
endfunction
valid_moves = [0,1,0,1]
move = "W"
print(valid_move(move,valid_moves))
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
To fix this error: Strings must be enclosed in quotation marks. Missing this will cause the computer to assume Player1 is a variable. It
then won’t understand what 1 wins means. The correct line would be:
print("Player 1 wins")
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
To fix this error: As currently written if total1 (which is player 1’s score) is less than total2 (which is players 2’s) score then print that
Player 1 has won, which is wrong, they have lost! The less than symbol needs to change to a greater than symbol:
To fix this error: The random function takes two parameters (integers) which must be separated by a comma, the comma is missing:
dice = random(1,12)
GCSE J277 Unit 2.1 | Algorithms Craig’n’Dave
Trace tables
Assuming the user enters the values: 01: x = int(input("Enter a number: "))
• Line 01: x = 2 02: y = int(input("Enter another number:
• Line 02: y = 7 "))
complete the trace table for this program. 03:
04: total = x
05:
X=2 Y=7 total counter 06: for counter = 1 to y
07: total = total * x
2 7 2 1 08: print(total)
4 2 09: next counter
8 3
16 4
32 5
64 6
128 7