1
Bugs in algorithms
Computing 2: Unit 4, pp. 54–65
GoalSay what a bug is and fix a bug in an algorithm.
New words
bug · a mistake in an algorithm or a programdebug · to find a bug and fix itswap · to change the places of two steps
Explanation
A bug is a mistake in an algorithm or a program. Often the steps are in the wrong order. To fix it, we read the steps one by one and ask: does this step make sense here? When we find the bug, we fix it. We might swap two steps. Then we read the whole algorithm again to check. Finding and fixing bugs is called debugging. In the picture, a flower cannot open before the shoot grows, so steps 2 and 3 swap.
| Step | With a bug | Fixed |
|---|---|---|
| 1 | Plant the seed | Plant the seed |
| 2 | A flower opens | A shoot grows |
| 3 | A shoot grows | A flower opens |
Worked examples
| Step | With a bug | Fixed |
|---|---|---|
| 1 | Dig a hole | Dig a hole |
| 2 | Cover the seed with soil | Put the seed in the hole |
| 3 | Put the seed in the hole | Cover the seed with soil |
| Step | Algorithm |
|---|---|
| 1 | Take two slices of bread |
| 2 | Eat the sandwich |
To debug, test the algorithm, find the bug, fix it and test it again. If it still does not work, look for another bug.
Class activity
Bug hunt: write a five-step algorithm for a daily job, such as getting ready for bed. Then copy it out again but swap two steps by mistake. Give this second version to a family member. Can they find the bug and fix it? Then let them make a bug for you to find. Which kind of bug was easier to find: a swapped step or a missing step?
Practice
1
The sun algorithm says: 1 The sun rises. 2 The sun sets. 3 The sun is high in the sky. What is the bug?
| Step | Algorithm |
|---|---|
| 1 | The sun rises |
| 2 | The sun sets |
| 3 | The sun is high in the sky |
Steps 2 and 3 are in the wrong order. The sun is high in the sky before it sets.
2
What is a bug?
A mistake in an algorithm or a program.
3
An egg algorithm says: 1 Put the egg in water. 2 Boil it for eight minutes. 3 Eat the egg. Which step is missing?
Take the egg out of the water and take off the shell, before step 3.
4
Which two steps must swap places?
Steps 2 and 3. The egg cracks open and then the chick hatches.