Showing posts with label Debugging. Show all posts
Showing posts with label Debugging. Show all posts

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
    ]

Debugging

Debugging is the process of finding and fixing “bugs”(errors) in your program.

Bugs can be of different forms, Some bugs that you would find in the following questions include instructions that have been typed in wrong, wrong logic, punctuation errors, missing instructions, wrong data, etc.

(1)   Here is a program for tracing the number 2 as seen on a digital clock using Pro-Bot. Each side of the figure is 6 cm long. Try programming your Pro-Bot with this program and see if it makes the required pattern. If it does not, find the bugs in your program (the parts of the program that are not working correctly) and fix them. Circle the wrong instructions in the given program and write the correct ones instead.























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


(2)   Here is a program for tracing the following figure using Pro-Bot. Each side of the figure is 6 cm long. Try programming your Pro-Bot with this program and see if it makes the required pattern. If it does not, find the bugs in your program and fix them. Circle the wrong instructions in the given program and write the correct ones instead.





















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



(3)   Here is a program for tracing the following figure using Pro-Bot. Each side of the figure is 6 cm long. Try programming your Pro-Bot with this program and see if it makes the required pattern. If it does not, find the bugs in your program and fix them. See if you need to add any other instructions as well. Circle the wrong instructions in the given program and write the correct ones instead.






















Rpt 2 [
Rpt 5 [
Fd 6
Rt
]


(4)  Here is a program for tracing the figure below using Pro-Bot. Each side of the square is 6 cm long. The square is divided into two equal parts by the line in the middle. Try programming your Pro-Bot with this program and see if it makes the required pattern. If it does not, find the bugs in your program (the parts of the program that are not working correctly) and fix them. Circle the wrong instructions in the given program and write the correct ones instead.















Rpt 4 [
Fd 6
Rt
]
Fd 2
Rt
Bk 6


(5) Here is a program for tracing the following figure using Pro-Bot. There are three squares stacked up and each square has 6 cm long sides. Try programming your Pro-Bot with this program and see if it makes the required pattern. If it does not, find the bugs in your program and fix them. See if you need to add any other instructions as well. Circle the wrong instructions in the given program and write the correct ones instead.