Back to Worked Examples
Testing & Debugging Term 3

Debugging Strategy

Problem Statement

The following algorithm is supposed to calculate the average of numbers in an array, but it contains bugs. Find and fix all the errors.

BEGIN CalculateAverage
    INPUT numbers[]
    total = 0
    count = 1

    FOR i = 1 TO LENGTH(numbers)
        total = total + numbers[i]
    NEXT i

    average = total / count
    OUTPUT "The average is: " + average
END

Debugging Process

1

Understand what the code SHOULD do

Before looking for bugs, clarify the expected behavior.

Expected behavior:

  • 1. Take an array of numbers as input
  • 2. Add up all the numbers (sum)
  • 3. Divide by the count of numbers
  • 4. Output the average

Example: For [10, 20, 30], average = (10+20+30)/3 = 20

2

Test with sample data

Trace through the code with a simple test case to see what actually happens.

Test input: numbers = [10, 20, 30]

Expected output: 20

Let's trace:

Step i numbers[i] total count
Init - - 0 1
Loop 1 1 20 (wrong!) 20 1
Loop 2 2 30 50 1
Loop 3 3 ERROR! - 1

Problem: We're accessing numbers[3] but the array only has indices 0, 1, 2!

3

Identify the bugs

List each bug found and classify its type.

Bug #1: Off-by-one error (Logic Error)

FOR i = 1 TO LENGTH(numbers)

Arrays are zero-indexed! Should start at 0 and go to LENGTH-1. Starting at 1 skips the first element and tries to access an invalid index at the end.

Bug #2: Wrong count value (Logic Error)

count = 1

Count is hardcoded to 1, but we need the actual number of elements. Should be count = LENGTH(numbers).

Bug #3: Missing edge case (Logic Error)

What if the array is empty? We'd divide by zero! Should check for this.

4

Fix each bug

Fix #1: Correct the loop range

FOR i = 1 TO LENGTH(numbers)
FOR i = 0 TO LENGTH(numbers) - 1

Fix #2: Get actual count

count = 1
count = LENGTH(numbers)

Fix #3: Add edge case check

IF count == 0 THEN OUTPUT "No numbers to average" ELSE ...

Corrected Code

BEGIN CalculateAverage
    INPUT numbers[]
    total = 0
    count = LENGTH(numbers)

    IF count == 0 THEN
        OUTPUT "No numbers to average"
    ELSE
        FOR i = 0 TO count - 1
            total = total + numbers[i]
        NEXT i

        average = total / count
        OUTPUT "The average is: " + average
    ENDIF
END
5

Test the fix

Verify the corrected code works with multiple test cases.

Test Case Input Expected Actual Pass?
Normal case [10, 20, 30] 20 20
Single element [5] 5 5
Empty array [] "No numbers..." "No numbers..."
Decimal result [1, 2, 3, 4] 2.5 2.5

Debugging Checklist

  • Check array indices (0-based vs 1-based)
  • Check loop boundaries (off-by-one errors)
  • Check variable initialization
  • Check edge cases (empty, single element, large numbers)
  • Check for division by zero
  • Verify operators (= vs ==, < vs <=)

Common Debugging Mistakes

  • Changing code randomly without understanding the problem
  • Only testing with one input value
  • Fixing symptoms instead of root causes
  • Not testing the fix thoroughly