CS 46A Lab 6, Debugging

Problems

Name:
CS46A Instructor:

The Pascal project in the Lab 6 workspace is supposed to print part of Pascal's triangle. (Row n, column m of Pascal's triangle = choose(n, m) = the number of ways to choose m items from n items. For example, choose(52, 5) = the number of five-card poker hands.)

Pascal's Triangle
Enter number of rows to display -> 10
You entered 10
Enter number of columns to display -> 10
You entered 10

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Again? (y/n) ->

Build Phase

Unfortunately, the program contains a number of compile time and runtime errors. Attempting to build the program produces two compiler errors and two warnings. (Your program should compile without warnings, so treat them as errors.) Double click the left mouse button on the first error message. A tiny blue arrow should point to the offending line of source code.

 

What's wrong? More is a variable of type bool used to control the number of times the user loops through main's while statement. More has been initialized to TRUE to guarantee the loop is executed at least once, but the compiler is complaining that TRUE is an undeclared identifier. In other words, the compiler believes TRUE is the name of a variable or function we forgot to define, instead of a built-in C++ value. In older versions of VC++ this message would have been understandable, because bool was not a standard C++ type; programmers had to define bool for themselves:

enum bool { FALSE, TRUE };

But now bool is part of the ANSI specification of C++, hence should certainly be predefined in version 5.0 of VC++. Search the IV index for bool. It claims that the C++ values of type bool are true and false. But wait, C++ is case sensitive. Try replacing TRUE by true, then rebuild to see if the message goes away.

Fix the remaining compiler errors and warnings. When you're finished, the output window should look like this:

Compiling...
pascal.cpp
Linking...
Lab3b.exe - 0 error(s), 0 warning(s)

Test Phase

Now it's time to test the program. Something is wrong, though. Instead of displaying Pascal's triangle, we are asked a second time for the number of rows to display. In fact, we're stuck. The program perpetually asks us for the number of rows and columns. Close the command window to terminate the program.

Debug Phase

Let's trace what happened by pressing F10. Notice the yellow arrow points to the statement being executed. Keep pressing F10 until the statement

rows = getNumber("Enter number of rows to display");

is executed. At this point you will have to find the DOS command window on your desktop that is connected to cout and cin. It should be waiting for you to enter the number of rows. Type in a number, then return to VC++. Notice the yellow arrow is pointing at the while statement instead of the statement that asks for the number of columns. (Did you notice that when we tested the program it wasn't perpetually asking for the number of rows and columns; it was just asking for the number or rows over and over again.)

What's the problem? The body of the while loop appears to contain all of the indented statements that follow the while statement, but the programmer forgot to group these statements together with curly braces. Do this now. Be careful where you place the closing brace. It's a good idea to comment closing braces with the type of statement they terminate:

while(more)
{
// ...

for(int i = 0; i < rows; i++)
{
// ...

} // for

// ...

} // while

 

Finish debugging the program. Submit a log of all the bugs you found. Also subit a printout of the program's output.