Wednesday, July 30, 2014

Solutions: Debugging

1.  The program to trace the number 2 has bugs in instructions & data.

   Fd 6
   Rt
   Fd 6
   Rt
   Fd 6
   Rt          //Wrong instruction, Rt instead of Lt
   Fd 6
   Lt
   Fd 12    //Wrong data, 12 cm instead of 6 cm

Here is the program with the bugs fixed:

   Fd 6
   Rt
   Fd 6
   Rt
   Fd 6
   Lt      
   Fd 6
   Lt
   Fd 6  




2.  The missing square bracket for the Rpt instruction is a very common bug.

    Rpt 2 [
    Fd 6
    Lt
    Rpt 3 [
    Fd 6
    Rt
    ]
    Rt
    Rt
    // The closing square bracket is missing as the last instruction here.
    // Without it, the outer loop executes only once, not twice as intended.


Here is the program with the bugs fixed:

    Rpt 2 [
    Fd 6
    Lt
    Rpt 3 [
    Fd 6
    Rt
    ]
    Rt
    Rt
    ]



3.  A stack of squares, using a Nested Loop:  The given program has bugs in data, punctuation (missing square bracket) and missing instructions.

    Rpt 2 [     // Wrong data, need 3 squares not 2
    Rpt 5 [
    Fd 6
    Rt
    ]
    // Missing a few instructions here, as well as a closing square bracket for the outer loop.


Here is the program with the bugs fixed:

    Rpt 3 [  
    Rpt 5 [
    Fd 6
    Rt
    ]
    Fd 6
    Lt
    ]



4.  A square of sides 6 cm divided into two equal parts: The given program has wrong data and wrong instructions.
   

    Rpt 4 [
    Fd 6
    Rt
    ]
    Fd 2     // Wrong data, need to move 3 cm to get to the midpoint 
    Rt
    Bk 6    // Wrong instruction, need Fd 6

Here is the program with the bugs fixed:
    Rpt 4 [
    Fd 6
    Rt
    ]
    Fd 3     
    Rt
    Fd 6    



5.  Three squares stacked on top of each other. 

    Rpt 2 [     // Wrong data: need 3 squares, so repeat 3
    Rpt 5 [
    Fd 6
    Rt


    ]
    // Missing instructions here
    ]

Here is the program with the bugs fixed:
    Rpt 3 [     
    Rpt 5 [
    Fd 6
    Rt


    ]
    Lt
    ]

No comments:

Post a Comment