topic badge

12.11 Debugging

Lesson

Debugging

Writing computer programs (coding) can be a process of trial and error. It is quite common for programmers to make errors when they are writing their code. These mistakes are known as 'bugs' and they stop the computer program (code) from doing what it is supposed to.

An important part of programming is testing your program and 'debugging' (which means removing or fixing the bugs).

Debugging tips:

  1. Try to identify where the program or code is not working.

  2. Go through this section of your code step by step thinking about what each command does, writing down the result of each step if possible.

  3. If you are still having difficulty finding the bug, take a break and look at it again, or ask a friend to look at your code.

Examples

Example 1

The linear search algorithm will find the position of the number 6 in the list of five numbers 9, \, 4, \, 6, \, 7, \, 3.

a

Each step of the algorithm is listed below.

Step NumberAlgorithm StepOUTPUT
1\text{Value} = 6
2\text{If first number in list = Value then print } 1
3\text{If second number in list = Value then print } 2
4\text{If third number in list = Value then print } 3
5\text{If fourth number in list = Value then print } 4
6\text{If fifth number in list = Value then print } 5

Which step of the algorithm produces an output?

A
Step 3
B
Step 1
C
Step 2
D
Step 5
E
Step 4
F
Step 6
Worked Solution
Create a strategy

Look for where 6 is in the list.

Apply the idea

From the list of numbers, we can see that the number 6 is the third number in the list. Step 4 checks whether \text{Value}=6 in in third place, which it is, so it will print a 3.

So option E is the correct answer.

b

What is the output of this algorithm?

Worked Solution
Create a strategy

Use the answer in part (a).

Apply the idea

When we apply Step 4, it will give us an output of 3.

c

A bug is an error in the algorithm which results in the algorithm not producing the desired output.

The algorithm below contains a bug.

Step NumberAlgorithm StepOUTPUT
1\text{Value} = 6
2\text{If first number in list = Value then print } 1
3\text{If second number in list = Value then print } 2
4\text{If third number in list = Value then print } 3
5\text{If fourth number in list = Value then print } 4
6\text{If third number in list = Value then print } 5

Which steps of the algorithm produce an output? Select all that apply.

A
Step 1
B
Step 5
C
Step 2
D
Step 4
E
Step 6
F
Step 3
Worked Solution
Create a strategy

Apply each of the steps of the algorithm to determine which steps produce an output (print a value). Use the list of numbers given as the input.

Apply the idea

The original list of numbers was 9, \, 4, \, 6, \, 7, \, 3.

Step NumberAlgorithm StepOUTPUT
1\text{Value} = 6
2\text{If first number in list = Value then print } 1
3\text{If second number in list = Value then print } 2
4\text{If third number in list = Value then print } 33
5\text{If fourth number in list = Value then print } 4
6\text{If third number in list = Value then print } 55

The algorithm steps that produced outputs are Step 4 and Step 6. So the correct answers are options D and E.

d

Which step contains the bug?

A
Step 6
B
Step 5
C
Step 3
D
Step 2
E
Step 1
F
Step 4
Worked Solution
Create a strategy

Use the answer in part (c) and compare it to the desired ouput.

Apply the idea

Our desired output is 3 since the number 6 is in the third position. Looking at the answers in part (c), Step 4 will give us that desired output while Step 6 will give us an incorrect output of 5. So Step 6 contains the bug, option A.

Example 2

Look at the pseudo code below, which sorts a list of five numbers from smallest to largest.

1
START
2
SET list = [4, 9, 7, 3, 6]
3
SET a = 0
4
FOR n FROM 1 TO 4
5
a = list element at position n
6
FOR m FROM (n + 1) TO 5
7
IF list element at position m < a
8
a = list element at position m
9
END IF
10
END FOR
11
SWAP a WITH list element at position n
12
PRINT list
13
END FOR
14
END
OUTPUT:
3, 9, 7, 4, 6
3, 4, 7, 9, 6
3, 4, 6, 9, 7
3, 4, 6, 7, 9
a

The pseudo code was changed and gave the following outputs:

INPUT:
4, 9, 7, 3, 6
OUTPUT:
3, 9, 7, 3, 6
3, 3, 7, 3, 6
3, 3, 3, 3, 6
3, 3, 3, 3, 6

Which line most likely contains the bug?

A
Line 7
B
Line 11
C
Line 8
D
Line 5
Worked Solution
Create a strategy

Consider what is happening in each output

Apply the idea

By looking at the outputs, we can see that instead of swapping the elements in the list with the element of the smallest value, it is replacing the elements in the list with the element of the smallest value.

So there must be a bug in the line that contains the SWAP command, which is line 11.

The correct answer is option B.

b

Which of the following replacement lines would have resulted in this output?

A
list element at position n = a
B
a = list element at position m
C
a = list element at position n
D
list element at position m = a
Worked Solution
Create a strategy

Refer to the answer in part a.

Apply the idea

In part (a), we learned that Line 11 most likely contains the bug:

SWAP a WITH list element at position n

Looking at the pseudo code, we can see that Line 11 should swap a with list element at position n. But instead of swapping, is replaced the list element at position n with a.

So it made list element at position n = a.

The correct answer is option A.

c

The pseudo code was changed and gave the following outputs:

INPUT:
4, 9, 7, 3, 6
OUTPUT:
3, 9, 7, 4, 6
3, 4, 7, 9, 6
3, 4, 7, 9, 6
3, 4, 7, 9, 6

Which line most likely contains the bug?

A
Line 4
B
Line 5
C
Line 11
D
Line 6
Worked Solution
Create a strategy

Consider what is happening in each output

Apply the idea

If the bug is in Line 4, this would change how many times we run the outer "FOR" loop. Since we have four lines of output, we know the outer "FOR" loop is running the right number of times. So the error is not Line 4.

If the bug is in Line 5, this would change the position of the element we are comparing initially. But we can see from the first two outputs the initial comparisons were correct. So the error is not Line 5.

If the bug is in Line 11, this would change whether or not a swap happened. But the swap happened correctly in the first two iterations. So the error is not in Line 11.

If the bug is in Line 6, this would change which elements we are comparing with \text{a}. The value of \text{a} in each loop depends on the value of \text{m} (in Line 8). The value of \text{m} is determined in Line 6.

So the line which is most likely to contain the bug is Line 6, option D.

d

Which of the following replacement lines would have resulted in this output?

A
FOR n FROM (m + 1 ) TO 5
B
FOR m FROM n TO 5
C
FOR m FROM (n +1 ) TO 4
D
FOR n FROM m TO 4
Worked Solution
Create a strategy

Refer to the answer in part c.

Apply the idea

All the outputs are in order, except for the 5th element. So it is like the 5th element is not being considered in the sort.

In Line 6 the code is FOR m FROM (n + 1) TO 5, which compares the value up to the 5th element. So if the 5th element is not considered, it means the code only considered comparing up to the 4th element. So the code would be FOR m FROM (n + 1) TO 4.

The answer is Option C.

Idea summary

Debugging tips:

  1. Try to identify where the program or code is not working.

  2. Go through this section of your code step by step thinking about what each command does, writing down the result of each step if possible.

  3. If you are still having difficulty finding the bug, take a break and look at it again, or ask a friend to look at your code.

What is Mathspace

About Mathspace