Chapter 8- Debugging Exercises

NOTE: There are no TABS on the codes. THESE ARE MY PERSONAL solutions to the problem. Copy at your own risk.

1. Off-by-one error
BUG: The program loops out of bounds in the highlighted line below.
FIX: It should set upper bound to SIZE – 1
Constant Integer SIZE = 5
Declare String names[SIZE] = “Meg”, “Jack”, “Steve”, “Bill”, “Lisa”
Declare Integer index
For index = 0 To SIZE
Display names[index]
End For

2. highest not initialized
BUG: The pseudocode does not initialize variable highest. Therefore the the comparison will not work.
FIX: highest should be initialized to first array element
Declare Integer SIZE = 3
Declare Integer values[SIZE] = 1, 3, 4
Declare Integer index
Declare Integer highest
For index = 0 To SIZE – 1
If values[index] > highest Then
Set highest = values[index]
End If
End For
Display “The highest number is “, highest

3. found not initialized
BUG: Function doesn’t initialize found. Thus the While loop won’t execute, result is always not found.
FIX: found should be initialized to False
Function String searchName(String name, String names[], Integer size)
Declare Boolean found
Declare Integer index
Declare String result
While found == False AND index <= size - 1 If contains(names[index], name) Then Set found = True Else Set index = index + 1 End If End While If found == True Then Set result = names[index] Else Set result = "That names was not found in the array." End If Return result End Function

Leave a Reply

Your email address will not be published. Required fields are marked *