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:
Try to identify where the program or code is not working.
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.
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.
The linear search algorithm will find the position of the number 6 in the list of five numbers 9, \, 4, \, 6, \, 7, \, 3.
Each step of the algorithm is listed below.
Step Number | Algorithm Step | OUTPUT |
---|---|---|
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?
What is the output of this algorithm?
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 | \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.
Which step contains the bug?
Look at the pseudo code below, which sorts a list of five numbers from smallest to largest.
The pseudo code was changed and gave the following outputs:
Which line most likely contains the bug?
Which of the following replacement lines would have resulted in this output?
The pseudo code was changed and gave the following outputs:
Which line most likely contains the bug?
Which of the following replacement lines would have resulted in this output?
Debugging tips:
Try to identify where the program or code is not working.
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.
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.