2 - Practical Problem Solving & Programming / Debugging
|
|
Debugging
The terms "debugging" and computing "bug" actually originate from Admiral Grace Hopper in the 1940s. Grace was working on a Mark II computer at Harvard University where here associates found a moth stuck in the circuits of her computer. This "Bug" was actually preventing the computer from functioning correctly. Removing the bug "Debugging" actually fixed the system and hence the terms were born!
Finding errors
When initially running a program that you are working on you may be alerted that your code contains a syntax error. These are usually easy to find as you are often told which line they are on by the interpreter.
Logical Errors are often more difficult as the program will run as if there are no errors, however an unusual result may be produced or it will crash at an unexpected point. It is then the job of the programmer to find a mistake in the code that caused the problem.
Logical Errors are often more difficult as the program will run as if there are no errors, however an unusual result may be produced or it will crash at an unexpected point. It is then the job of the programmer to find a mistake in the code that caused the problem.
Common mistakes to look for
Once you have established that there is an error in your code through testing then it is a good idea to start debugging by looking for common problems. There are many possible reasons why a piece of code may not work, however the following points are a good place to start - especially in the IGCSE exam!.
Loops
Loops can be the route of many problems. Here are a few possible examples:
1. The Loop does not repeat enough times
Example - A program is designed to print the numbers 1 to 1000 (inclusive), This means that the code will need to loop 1000 times.
FOR x = 0 to 999
OUTPUT x + 1
This will only repeat 999 times and will only print the numbers 1 to 999 (Inclusive)
Try it yourself below:
1. The Loop does not repeat enough times
Example - A program is designed to print the numbers 1 to 1000 (inclusive), This means that the code will need to loop 1000 times.
FOR x = 0 to 999
OUTPUT x + 1
This will only repeat 999 times and will only print the numbers 1 to 999 (Inclusive)
Try it yourself below:
To fix this problem you could change the upper limit of the FOR loop from 999 to 1000 like this:
FOR x = 0 to 1000
OUTPUT x + 1
This adjustment will now allow the code to repeat 1000 times.
Try it for yourself below:
2. WHILE or REPEAT UNTIL loop has no exit clause
Example - your code will repeat forever until the input is "-1". An example situation may be that you need to continue entering numbers and totaling them until the the value -1 is entered/ An incorrect piece of code could look like this:
total = 0
num = 0
x = ""
WHILE x != "-1" DO
num = INPUT()
total = total + num
END WHILE
OUTPUT total
This code would repeat forever as the user is never given the opportunity to make x = to "-1".
You can test this out yourself below but note that entering a string rather than an integer will cause the program to crash. Further validation would need to be implemented to ensure that only integers are accepted.
Example - your code will repeat forever until the input is "-1". An example situation may be that you need to continue entering numbers and totaling them until the the value -1 is entered/ An incorrect piece of code could look like this:
total = 0
num = 0
x = ""
WHILE x != "-1" DO
num = INPUT()
total = total + num
END WHILE
OUTPUT total
This code would repeat forever as the user is never given the opportunity to make x = to "-1".
You can test this out yourself below but note that entering a string rather than an integer will cause the program to crash. Further validation would need to be implemented to ensure that only integers are accepted.
The code could be corrected by adding a line that allows the user to change x - e.g. x = INPUT()
total = 0
num = 0
x = ""
WHILE x != "exit" DO
num = INPUT()
total = total + num
x = INPUT()
END WHILE
Try the code below. This adjustment will now make it possible to exit the program by giving the user the opportunity to make x = -1 after each repetition.
The code could be corrected by adding a line that allows the user to change x - e.g. x = INPUT()
total = 0
num = 0
x = ""
WHILE x != "exit" DO
num = INPUT()
total = total + num
x = INPUT()
END WHILE
Try the code below. This adjustment will now make it possible to exit the program by giving the user the opportunity to make x = -1 after each repetition.
3. Wrong operator used in a WHILE or REPEAT UNTIL loop
Example - Your code is meant to repeat as long as x is greater than 100. An incorrect piece of code would look like this:
WHILE x < 100 DO
This would actually cause the code to repeat whilst the number is 99 or less. - The opposite of what is required.
WHILE x >= 100 DO
This is almost correct as it would keep the loop repeating for numbers while they are 100 or greater, however the scenario specifies that x must be greater than 100. This means 101 and up.
The correct code for this scenario may read:
WHILE x >100 DO
or
WHILE x >=101 DO
Example - Your code is meant to repeat as long as x is greater than 100. An incorrect piece of code would look like this:
WHILE x < 100 DO
This would actually cause the code to repeat whilst the number is 99 or less. - The opposite of what is required.
WHILE x >= 100 DO
This is almost correct as it would keep the loop repeating for numbers while they are 100 or greater, however the scenario specifies that x must be greater than 100. This means 101 and up.
The correct code for this scenario may read:
WHILE x >100 DO
or
WHILE x >=101 DO
4. Final output is within the loop causing excessive output
Example - Lets say that your program is meant to take 10 numbers from the user. Total them up and then output the result at the end. Here is an example of incorrect code:
Total = 0
FOR x = 0 to 10
OUTPUT "Please enter a number"
Num = INPUT()
Total = Total + Num
OUTPUT "Your total is" + Total
END FOR
The problem with this code is the position of the (OUTPUT "Your total is" + Total). This line is within the loop which means that it will actually output a subtotal to the user 10 times. The 10th output is actually all that the user needs to see.
Try this code out below to see the program working incorrectly:
Example - Lets say that your program is meant to take 10 numbers from the user. Total them up and then output the result at the end. Here is an example of incorrect code:
Total = 0
FOR x = 0 to 10
OUTPUT "Please enter a number"
Num = INPUT()
Total = Total + Num
OUTPUT "Your total is" + Total
END FOR
The problem with this code is the position of the (OUTPUT "Your total is" + Total). This line is within the loop which means that it will actually output a subtotal to the user 10 times. The 10th output is actually all that the user needs to see.
Try this code out below to see the program working incorrectly:
To fix this problem and to ensure that the total is only displayed at the end of the program, the line should be moved outside and after the for loop as below.
Total = 0
FOR x = 0 to 10
OUTPUT "Please enter a number"
Num = INPUT()
Total = Total + Num
END FOR
OUTPUT "Your total is" + Total
Try this code below to see how it has affected the program and corrected the mistake.
There are many other problems that could be caused by loops. When faced with a real life problem OR an exam question the ideas above are good starting points for the debugging process.
Selection
The use of selection in a program can often be the source of a problem. Here are a few examples of where you might find errors.
1. Incorrect operator used in condition
This is very similar to using an incorrect operator in a WHILE or REPEAT loop. Failure to use the correct operator can cause the program to produce an incorrect result.
Example - Your code is meant to allow a user to enter a number. If the number is greater than or equal to 10, the user will be told "This number is 10 or higher". If the number is less than 10 then the user will be told "This number is less than 10".
An example of pseudocode with an error could look like this:
Num = 0
OUTPUT "Please enter a number"
Num = INPUT()
IF Num > 10 THEN
OUTPUT "This number is greater than or equal to 10"
ELSE
OUTPUT "This number is less than 10"
The use of the > (Greater than) operator means that if num is 10, it will not be counted. If you enter 10 you will infact be told that 10 is less than 10.
You can test this out yourself below.
This is very similar to using an incorrect operator in a WHILE or REPEAT loop. Failure to use the correct operator can cause the program to produce an incorrect result.
Example - Your code is meant to allow a user to enter a number. If the number is greater than or equal to 10, the user will be told "This number is 10 or higher". If the number is less than 10 then the user will be told "This number is less than 10".
An example of pseudocode with an error could look like this:
Num = 0
OUTPUT "Please enter a number"
Num = INPUT()
IF Num > 10 THEN
OUTPUT "This number is greater than or equal to 10"
ELSE
OUTPUT "This number is less than 10"
The use of the > (Greater than) operator means that if num is 10, it will not be counted. If you enter 10 you will infact be told that 10 is less than 10.
You can test this out yourself below.
To fix this problem the >= (Greater than or equal to) operator should be used. The line of code would then look like this: IF Num >= 10 THEN
Use the code below to see the practical difference:
Counting and Totalling
It goes without saying that Counting and Totalling will provide plenty of opportunity for mistakes.
1. Counting
The idea of counting is to keep track of how many times something occurs e.g. a certain number being entered or the number of repetitions. Sometimes the programmer can have all the best intentions but can mix up the counting process with the totalling process.
Example - The program below is meant to accept 10 numbers from the user. The program should calculate the total of all the numbers and it should count how many times the number 5 is entered.
Count = 0
Total = 0
Num = 0
FOR x = 0 to 10
Num = INPUT()
Total = Total + Num
IF Num = 5 THEN
Count = Count + Total
END IF
END WHILE
OUTPUT Total
OUTPUT Count
The incorrect line here is in red. Rather than adding 1 to the count each time the number 5 is detected it simply adds the total to the count variable. This is definitely not keeping count and will produce a bizarre result for the user.
To fix this the line should be changed to:
Count = Count + 1
The idea of counting is to keep track of how many times something occurs e.g. a certain number being entered or the number of repetitions. Sometimes the programmer can have all the best intentions but can mix up the counting process with the totalling process.
Example - The program below is meant to accept 10 numbers from the user. The program should calculate the total of all the numbers and it should count how many times the number 5 is entered.
Count = 0
Total = 0
Num = 0
FOR x = 0 to 10
Num = INPUT()
Total = Total + Num
IF Num = 5 THEN
Count = Count + Total
END IF
END WHILE
OUTPUT Total
OUTPUT Count
The incorrect line here is in red. Rather than adding 1 to the count each time the number 5 is detected it simply adds the total to the count variable. This is definitely not keeping count and will produce a bizarre result for the user.
To fix this the line should be changed to:
Count = Count + 1
2. Totalling
Totalling is similar to counting in that it can cause issue if not properly set up.
The idea of totalling is to add new numbers to the existing total.
E.g.
Total = Total + Total
Mistakes could be made due to carelessness e.g.
Total = Total + 1
Total = Total
Total = Total - Total
These would all case incorrect totalling
The position of the Total = Total + Total line is also important when working with Loops.
Example
Total = 0
Num = 0
FOR x = 0 to 10
Num = INPUT()
END WHILE
Total = Total + Num
OUTPUT Total
This example will not add up 10 entered numbers as intended as the red line lies outside the loop. What it will actually do is add
The idea of totalling is to add new numbers to the existing total.
E.g.
Total = Total + Total
Mistakes could be made due to carelessness e.g.
Total = Total + 1
Total = Total
Total = Total - Total
These would all case incorrect totalling
The position of the Total = Total + Total line is also important when working with Loops.
Example
Total = 0
Num = 0
FOR x = 0 to 10
Num = INPUT()
END WHILE
Total = Total + Num
OUTPUT Total
This example will not add up 10 entered numbers as intended as the red line lies outside the loop. What it will actually do is add
Vertical Divider