Using CodeView to Isolate an Error

In addition to the typographical errors that you just corrected, ANNUITY1.C contains a logic error: when the program prints the annuity table, the year numbers start at 0 instead of 1. You can use CodeView to isolate the errors in program logic.

This program calculates the payment on a loan, so you can use the following test case:

Present Value: $14,500
Interest Rate: 14%
Period: 5 Years

The expected result is a monthly payment of $337.39.

·To start CodeView:

From the Run menu, choose Debug.

If anything in your program is out-of-date, PWB asks if you want to build or rebuild the current target. If you modified the source file to correct errors or change text, PWB considers it out-of-date relative to the executable file that you built earlier. If this happens, build the program and choose Debug from the Run menu.

CodeView starts, showing you the source line of the program's starting point. In this case, the starting point is the opening brace of the function main.

The first step in debugging a program is to verify input values. You will know what values have been supplied after the last scanf statement has executed, so run the program up to that point as follows:

1.If the Source window (the window displaying your program) is not the active window, press F6 until it is. You can tell that a window is active when the title bar is highlighted and it has scroll bars.

2.Move the cursor to line 32, RatePct = Rate / 1200.0

3.Press F7 (continue execution to cursor).

The program runs, asking for input. Supply the values you are using as a test case:

Present Value: 14500
Interest: 14
Number of Periods: 5

CodeView stops your program at line 32.

4.Resize the Local window until you can see all the variables:

A.Press F6 until the Local window is active. (The active window is the window with the scroll bar.)

B.From the Window menu, choose Size.

C.Use the DOWN ARROW key to enlarge the Local window.

When the window is the desired size, press ENTER to accept that size.

D.Press F6 to move back to the Source window.

Now you can verify that the initial data used by your program is correct by examining the values of Pv, Rate, and Nper. They should have the values 14500, 14, and 5, respectively.

Note :

For case-insensitive languages such as Basic or FORTRAN, CodeView displays all variables, subroutine names, and function names in uppercase.

The next step is to execute the program until the initial calculations are done. The calculations are complete prior to the for loop. If you let the program execute that far, the program produces some screen output that's useful for debugging.

·To execute to the for loop:

1.From the Search menu, choose Find.

2.Type for in the Find Text box.

3.Press ENTER to move the cursor to the for statement.

Although your cursor is now on line 62, the program has not executed the statements between where it stopped (on line 32) and the current cursor position.

4.Press F7 to execute all code up to but not including this location.

Your program has now displayed the summary information on the screen. To switch to the output screen, press F4. To switch back to the CodeView screen, press F4 again (or any key).

On the output screen, you should observe the following results:

Monthly Payment: 337.39
Total Payments: 20243.38
Total Interest: 5743.38

These results are correct. You know that the program works properly up to the beginning of the for loop. Therefore, you can ignore all code up to this point and focus on discovering why the year number is incorrect.

·To step through one cycle of the loop and examine your data:

1.Press F10 three times to step three lines.

This calculates values for PerInterest (interest for the current period) and PerPrin (contribution to principal for the current period).

2.Examine the values of PerInterest and Prin in the Local window to see if they are correct. The formula for simple interest is:

Interest = Outstanding Principal * Periodic Interest Rate

Similarly, the formula for the contribution principal is:

Contribution = Payment – Interest

In the test case, the correct values for PerInterest and PerPrin are 169.167 and 168.223, respectively. These values should appear on your screen.Press F10 again to step one more program statement—the printf statement.Press F4 to examine the screen output. The year number is still incorrect.

You have reduced the problem to the arithmetic in the printf call itself. The ar-gument list for printf contains the expression Period / 12. Integer truncation causes all values of Period that are less than 12 to yield the result 0.

Now that you have identified the apparent bug, you can test the solution. The proposed solution to this problem is to replace the expression Period / 12 with Period / 12 + 1. You can test this solution by using the CodeView expression evaluator.

·To test the new expression:

1.Activate the Command window.

2.Type the Display Expression (?) command with the test expression:

? Period / 12 + 1

CodeView evaluates the expression and prints 1, the correct result.