Software Design and Development

Home > Software Design and Development > Core > Software Development Cycle > Detecting and correcting errors

Detecting and correcting errors- answers

Activity 1

The five errors are corrected and explained in red:

Private Sub cmdSkillsTest_Click() ' Math skills test
Dim lngBefore As Long

Dim lngAfter As Long

Dim lngTimeDiff As Long

Dim strAns As String

Dim intResult As Integer

lngBefore = Timer

Do
‘Missing keyword in grouping

‘VB variables cannot start with ‘digit (2time)

' Save seconds since midnight
strAns = InputBox("What is 150 + 235?","Hurry”) ‘Missing‘ punctuation
Loop Until Val(strAns) = 385

lngAfter = Timer
‘Typing mistake in reserved word

' Save seconds since midnight
‘The difference between the stored time values is how many seconds the user took to answer the question.
lngTimeDiff = lngAfter – lngBefore ‘Variable name typing error

intResult = MsgBox ("That took you only "& Str(lngTimeDiff) & " seconds!")
End Sub

Congratulations if you found all five errors! You have sharp eyes.

Back to activity

Activity 1 PHP Answer

CODE Error Fixed code
Ehco “Hello there”; Spelling of function Echo “Hello there”;
IF ($age < 13){
      Echo “Child”
}
1 error
Missing semi-colon ; that end each line of code
IF ($age < 13){
      Echo “Child”;
}
IFS (age < 13){
      Echo “Child”;
}
2 errors
Key work IF miss-spelt
Variable age must have a $
IF ($age < 13){
      Echo “Child”;
}
$count = 0;
WHILE count < 10
      $count = $count + 1;
      Echo “count “;
}
2 errors
Condition of the loop must be within brackets ( )
Curly brackets missing
$count = 0;
WHILE (count < 10){
      $count = $count + 1;
      Echo “$count “;
}

Back to activity

Activity 2

The hand trace shows the value stored in number has gone to –1 without ever becoming zero. An endless loop has beencreated.

Loop Iteration number picSequence
0 11 None
1 8 11
2 5 11

8
3 2 11

8

5
4 -1 11

8

5

2

Back to activity

Activity 2 PHP answer

  1. The following algorithms print out the numbers 1 to 5
    (Expected output is 1, 2, 3, 4, 5)

    Code Output Explain and fix the error
    $count = 0;
    WHILE ($count < 3) {
          $count = $count + 1;
          Echo “ $count”;
    }
    1
    2
    3
    Loop only executed 3 times, change condition to count < 5
    $count = 0;
    WHILE ($count < 5) {
          Echo “ $count;
    }
    0
    0
    0
    0
    ... endless loop
    Endless loop as the variable count is not incremented within the loop
    Add count = count + 1
    $index = 0;
    WHILE ($index < 5) {
          $count = $count + 1;
          Echo “ $count”;
    }
    0
    0
    0
    0
    ...endless loop
    Variable names change.
    Change all index to count
    $count = 0;
    WHILE ($count < 5) {
          Echo “ $count
    }
    0
    1
    2
    3
    4
    Variable is ouputted before be incremeted.
    Either set count to 1, or move count=count+1 to above ECHO
    $count = 0;
    WHILE ($count < 5) {
          $count = $count - 1;
          Echo “ $count”;
    }

    -1
    -2
    -3
    ...endless loop

    Varible is incremented incorrectly.
    Change to $count=$count+1

  2. The following code calculates the average of 2 numbers
    (test data, enter 6, 4 should display 5)

    Code Output Explain and fix the error
    $average = $num1 + $num2 / 2;
    Echo “The average is”; $average”;
    8 Average=6 + 4/2
    Echo "The average is"; $average";   Average = 6 + 2
    2 values are not added before dividing by 2
    Add brackets
    $average = ($num1 + $num2) / 2;

  3. The following algorithms print out if someone is able to legally vote

    Test Data Output
    14 Not legal
    25 Legal
    18 Legal

    Code Explain and fix the error
    IF ($age > 18) {
          Echo “Legal”;
    }ELSE{
          Echo “Not legal”;
    }
    When age=18, Not legal is incorrectly displayed.

    Change condition to
    ($age >= 18)

Back to activity

Activity 3

Desk checking the original algorithm with a value like 4 (sum = 4+3+2+1 = 10) should have revealed the error. This code gives a sum of 6.

The hand execution results are shown below.

The problem is fixed by placing the line number = number - 1 after the sum = sum + number line.

Loop iteration number sum expected sum
0 4 0
1 3 3 4
2 2 5 7
3 1 6 9
4 0 6 10

Back to activity

Activity 4 PHP answer

Code Output Explain and fix the error
$count = 0;
WHILE ($count < 5) {
            Echo “ $count”;
}
0
0
0
0
...endless loop
The endless loop could cause an overflow error if $count is set as an integer where there is a limit ti the value it can hold.
Add $count = $count + 1 to increment the count.

Back to activity

Go To Top



Neals logo | Copyright | Disclaimer | Contact Us | Help