Lab 08: Recursion

This week's lab session will primarily be on Session 8 in the Lab Book, Recursion.


Exercises: Session 8 in the Lab Book

Do these exercises in the lab and turn them in. Remember to type your answers into a file and e-mail the file to your TA (not to the faculty coordinator). Do all steps in each Experiment unless noted.

Remember, when the lab-book mentions a program, say, CP08E03, don't type it in! Copy it to your current directory:
cp ~cot3002l/src/CP08E03.cpp .
This means the C++ files are in the subdirectory named 'src' in the directory of the user-name 'cot3002l' (note that's an 'ell' at the end not a one!) Don't forget the final '.' after the name of the C file. (This means the target of the copy command is the current directory, and just use the same name as in the source directory.)


Notes before starting:

You'll need to remember some things about printing addresses and using the debugger from previous labs in order to do this lab. (Don't forget you can refer back to previous labs' Web pages. Or ask your TA if after reviewing last weeks work you are still unsure. Or, read the next section!)

Printing addresses in the debugger:

You may not have learned how to print a variable's address with the debugger. Remember that every variable is located at a position in a computer's memory, which we refer to as the variable's address. This is different than the variable's value, which is the data stored at the address. Remember that C and C++ let us put the operator & in front of any variable to get the variable's address (instead of it's value). Different variables always have different addresses, even if they happen to have the same value.

When using gdb or xxgdb, you can print the address of a scalar variable like num by typing the debugger command
    print/u &num
in the debugger's command-line interface. To print the address of an array variable like array_A, type the debugger command
    print/u &array_A[0]
which prints the address of the first value in the array, which is the starting address of the array. You can put the /u option at the end of the display command to display an address in the display-area of the debugger's windows. (Note that the /u prints a number in "unsigned" format, and addresses are always non-negative integers.)

Quick review of recursion:

Be sure you read the information on page 112 in the Lab Manual. It's important that you understand the fundamentals of recursion. Remember, if a function is simply a "chunk" of code that operates on one or more arguments passed to it and returns a value, then there is no reason a program can't call that function in more than one place, with different arguments. And thus why can't a function call itself to calculate a partial result, then use that result plus something else to get the complete result it needs to return? This is very powerful, as long as we remember that there may be multiple "versions" of the function "in progress" at any one time. When one function-call finishes, it will return to whatever called it. And this might be the same function, if a recursive call was made.


The Exercises:

Do the following experiments:

Remember the command to compile your program is:
g++ -g -Wall prog_name.cpp
Always use the -Wall option with g++ to show all warnings, some of which might be errors. The -g is needed if you want to run the debugger. You could also use the -o option to rename the output file to be something other than a.out


Last modified on 11/27/00.