CS24, Fall 2016

Lab08:
Using recursion


Goals for this lab

By the time you have completed this lab, you should be able to

Step by Step Instructions

Step 1: Create a lab08 directory (in the first pilot's account)

First get together with your lab partner. If your regular partner is more than 5 minutes late, ask the TA to pair you with someone else for this week.

Select a pilot, log in, create a ~/cs24/lab08 directory, and make it your current directory.

Step 2: Get a copy of the necessary program files

There are just two required files to copy from the class account this week. You can copy them both at once as follows:

-bash-4.3$ cp ~cs24/labs/lab08/*.cpp ~/cs24/lab08/
-bash-4.3$ ls
alph.cpp  triangles.cpp

Step 3: Learn the features of a recursive function

Every recursive function must (a) have a base case; (b) have recursive calls that converge on the base case; and (c) actually solve the given problem. In this lab step, you will learn about a recursive printing function with two base cases; later you will write similar functions.

Examine the completed function, alph, in alph.cpp. Then answer the following questions about this function, jotting down your answers on paper (or at least being sure to remember them).

  1. What are the two base cases?
  2. How can you be sure that every recursive call gets closer to the base case(s)?
  3. Does this function actually solve the problem - for example, will alph('d') print "abcd" as it should?

Compile and run it to test (as usual, user input is bold):

-bash-4.3$ make alph
g++     alph.cpp   -o alph
-bash-4.3$ ./alph
enter letter:
k
abcdefghijk
Before continuing, test boundary and null cases such as alph('a') and alph('B'), respectively. Any others?

Step 4: Implement three similar recursive functions

Now - one at a time - implement the other three functions in alph.cpp. All must be done using recursion (you may not use any loop structures).

Here is a complete run from our solution - functions are called in this order: alph, ralph, zalph, zralph
-bash-4.3$ ./alph
enter letter:
r
abcdefghijklmnopqr
rqponmlkjihgfedcba
rstuvwxyz
zyxwvutsr

Step 5: More recursive functions, and using one to help another one

First: switch roles between pilot and navigator if you did not already do that.

You must use recursion for all of the following tasks - no loop structures at all. Check the results of each part before going on to the next part.

Turn your attention now to the other program skeleton, triangles.cpp. When you finish implementing all three functions, your results should match the following (if user enters 3):

-bash-4.3$ ./triangles
enter size:
3
***
**
*
*
**
***
It is a crude print drawing of two triangles, each of the size specified by the user. Here is a run with size 20.

Implement function stars first. This function should print a star (actually an asterisk, '*'), and call itself as many times as necessary to print exactly the number of stars specified in the initial call. If you don't change main yet, then compile triangles.cpp and run it, your output should match the following runs:

-bash-4.3$ make triangles
g++     triangles.cpp   -o triangles
-bash-4.3$ ./triangles
enter size:
7
*******-bash-4.3$ ./triangles
enter size:
4
****-bash-4.3$
Don't worry about the lack of a newline, as that will be handled by the other functions.

Now implement triangle to print n rows of stars, the first row has n stars, the second has n-1, and the last row has one star. Use the stars function to print each row, and then print a newline at the end of the row.

To test it, edit the main function of triangles.cpp by deleting (or commenting out) the call to stars. Then remove the // from in front of the call to triangle, and recompile. Your results should match the top half of the complete results above.

Finally, implement rtriangle to print the rows of stars in reverse order, one star first, and n stars last. Match the bottom half of the complete results. To test, remove the // from in front of the call to rtriangle in main and compile again.

Step 6: Submit your revised alph.cpp and triangles.cpp

Submit Lab08 at https://submit.cs.ucsb.edu/, or use the following command from a CS terminal:

~submit/submit -p 599 alph.cpp triangles.cpp

If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code files, and be sure to properly form a group for this project in the submit.cs system.

50/50 is a perfect score.


Evaluation and Grading

Each student must accomplish the following to earn full credit [50 total points] for this lab:


Optional Extra Challenge

Don't change triangles.cpp, but copy it to another file (e.g., tri2.cpp), and make some changes to the copy, such as the following:


Prepared by Michael Costanzo.