IC210 Lab 5 -The Debugger


Pre-lab homework.None for this lab. 


Removing programmer errors with the Debugger, Or, Zen and the Art of Finding Errors and Understanding Your Code

This lab is extremely important for your programming development (and sanity) because learning how to use the debugger can make your life easier! If you can use the debugger effectively, you'll gain a greater understanding of the way your code works and be able to quickly identify and correct even subtle problems with your programís control flow logic.

Using the debugger to understand your code

For our first portion of the lab, we'll examine the operation of the Hankel Matrix program we developed in class.

  1. Create a new project named "debug_lab".
  2. Add hankel.cpp to the project, compile and run. Satisfy yourself that everything is working.
  3. In Visual C++, right-click in an area of the menu bars that has no buttons. This will pop up a window listing the available toolbars. If it is not selected already, select the Debug toolbar.
  4. Put your cursor on the line of code with int main(), and click on the Run to Cursor icon from the Debug toolbar. The program will build if necessary, then begin executing and stop at the line where your cursor is. A yellow arrow next to your source indicates the next line to execute. The Run to Cursor option is really handy if you want to stop in a certain spot, and don't want to wade through every line of code before it to get there.
  5. Notice that the Visual C++ screen has changed. Two new windows have been opened at the bottom of the screen, the Watch window and the Call Stack window. On the bottom of the left-hand window is a tab for locals; Click on it. (Note: If the Locals or Call Stack Windows disappear, you can bring them back by going up to the Debug menu and choosing Windows, which brings up a list of windows related to debugging. Just find the one you want in the list and click on it.) Click the Step Over button on the Debug toolbar until the yellow arrow goes past the variable declaration, int n. You will see the integer n displayed with its current value. If we had declared other variables, they would be shown here as well. Since we have not assigned anything to n, it will have a random value.
  6. Click the Step Over button to step through the cout and cin lines. Progress will stop on the cin line. The program is waiting for you to input n. Click on the window in which your program is running (). Enter a value for n in the Program window, and verify that it shows up in the Locals window.
  7. In addition to the automatic addition of variables to the Variables window, it is possible to manually add variables and expressions. Click on the Watch 1 tab next to the Locals tab, click in the blank cell on the left-hand side, and enter an expression like n*4. You should see 4 times the current value of n, whatever you entered.
  8. Proceed through your program using Step Over. Observe how the variables change as you proceed through the loops. Observe how they get created and destroyed as you go.
  9. Walking through you program in this manner is very slow. To move faster, we'll revisit the Run to Cursor button. Position the cursor to the line with the first curly brace following the first for. Click on the Run to Cursor button. What happened (check the Program Window)? Click on the Run to Cursor button again and check the output.
  10. Visual C++ provides two additional debugging buttons, Step Into and Step Out. These buttons will become of use when we cover functions next week. For now, just know they exist. Note, if you accidentally end up in a screen full of code from one of the libraries, you can get back to your code by using the Step Out button. Note, if you Step Out of your program, you'll end up in some "disassembled" code. This is the machine code the exits your program. If you close the window with the machine code, you should be back to your program.
  11. To stop debugging, simply click the aptly named Stop Debugging button.

Problem Solving

Use the debugger to find and fix the problems in the following programs:

  1. This program to calculate interest rates only works properly on every other year. Why?
  2. One of last year's homework assignments had the students write a loan calculator. This student's solution to the problem appears to work, but gives different answers than the instructor's solution. Is the instructor wrong? (Hint: The instructor is NEVER wrong! What's wrong with the student's solution!)


Going Further 

If you finish the lab assignment early you should try to tackle this problem: 

  1. This program is supposed to report when two vectors are perpendicular. But, it always reports the vectors as not perpendicular regardless of the vectors input. For example, try (2, 20) and (5, 110). Why?
  2. Write a program that prints rectangles (in text) on the screen. The outline of the rectangles should be in * characters and the inside should be empty. In particular, your program will read in a height, a width, and an offset, and print out (in text) a rectangle of the appropriate height and width, indented by the number of spaces specified in the offset. Typical runs of your program might look like this:
    Enter height (greater than 2): 5
    Enter width  (greater than 2): 8
    Enter offset: 3
       *      *
       *      *
       *      *
    Enter height (greater than 2): 5
    Enter width  (greater than 2): 4
    Enter offset: 10
              *  *
              *  *
              *  *
    As you write this program, try to make use of the process of stepwise refinement that we discussed in lecture. Run your program and show your instructor the output with user input height=10, width=25, and offset=5.