topic badge
New Zealand
Level 6 - NCEA Level 1

Debugging

Lesson

 

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).

Example

Here is an algorithm which lists the instructions for making breakfast

  1. Take a bowl out of cupboard, a spoon out of the drawer and place them on the table
  2. Pour cereal and milk into the bowl
  3. Place the bowl in the sink
  4. Eat the cereal out of the bowl

Question: What is the bug in this algorithm?

Think: Imagine you are making breakfast in the morning and follow each step of the Algorithm.  Can you find the error?

Do:  Lines 3 and 4 are in the wrong order.

 

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.  

 

Worked Examples

QUESTION 1

The list of numbers 4, 9, 8, 1, 6 is to be sorted using the selection sort algorithm.

  1. Enter the output after each line of the selection sort algorithm.

    Step Number Algorithm Step OUTPUT
    $1$1 Find the smallest number.
    Swap it with the first number.
    $\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,
    $2$2 Find the second-smallest number.
    Swap it with the second number.
    $\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,
    $3$3 Find the third-smallest number.
    Swap it with the third number.
    $\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,
    $4$4 Find the fourth-smallest number.
    Swap it with the fourth number.
    $\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,

     

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

    The algorithm below contains a bug. What is the output of each line of the algorithm?

    Step Number Algorithm Step OUTPUT
    $1$1 Find the smallest number.
    Swap it with the first number.
    $\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,
    $2$2 Find the second-smallest number.
    Swap it with the first number.
    $\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,
    $3$3 Find the third-smallest number.
    Swap it with the third number.
    $\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,
    $4$4 Find the fourth-smallest number.
    Swap it with the fourth number.
    $\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,

     

  3. Which step contains the bug?

    Step 1

    A

    Step 3

    B

    Step 4

    C

    Step 2

    D

QUESTION 2

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

  1. Each step of the algorithm is listed below.

    Step Number Algorithm Step OUTPUT
    1 Value = 6
    2 If first number in list = Value then print 1
    3 If second number in list = Value then print 2
    4 If third number in list = Value then print 3
    5 If fourth number in list = Value then print 4
    6 If fifth number in list = Value then print 5

    Which step of the algorithm produces an output?

    Step 3

    A

    Step 1

    B

    Step 2

    C

    Step 5

    D

    Step 4

    E

    Step 6

    F
  2. What is the output of this algorithm?

    OUTPUT: $\editable{}$

  3. 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 Number Algorithm Step OUTPUT
    1 Number to be found = Value
    2 If Value = first number in list then print 1
    3 If Value = second number in list then print 2
    4 If Value = third number in list then print 3
    5 If Value = fourth number in list then print 4
    6 If Value = third number in list then print 5

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

    Step 1

    A

    Step 5

    B

    Step 2

    C

    Step 4

    D

    Step 6

    E

    Step 3

    F
  4. What are the outputs of these two steps of the algorithm?

    OUTPUT: $\editable{},\editable{}$,

  5. Which step contains the bug?

    Step 6

    A

    Step 5

    B

    Step 3

    C

    Step 2

    D

    Step 1

    E

    Step 4

    F

QUESTION 3

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
  1. 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?

    Line 7

    A

    Line 11

    B

    Line 8

    C

    Line 5

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

    list element at position n = a

    A

    a = list element at position m

    B

    a = list element at position n

    C

    list element at position m = a

    D
  3. 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?

    Line 4

    A

    Line 5

    B

    Line 11

    C

    Line 6

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

    FOR n FROM (m + 1) TO 5

    A

    FOR m FROM n TO 5

    B

    FOR m FROM (n + 1) TO 4

    C

    FOR n FROM m TO 4

    D

What is Mathspace

About Mathspace