Data Structures - Chapter 9 - Programming Assignment
Implement and Test Four Short Recursive Functions

Data Structures and Other Objects Using C++
Second Edition
by Michael Main and Walter Savitch
ISBN 0-201-70297-5, Softcover, 816 pages

The Assignment:
You will implement and test four short recursive functions. With the proper use of recursion, none of these function should require more than a dozen lines of code. If you have an easy time with this assignment, then after you submit it you should consider doing the additional honors assignment.
Ensure that you can write and test small recursive functions.
Before Starting:
Read all of Chapter 9, especially Sections 9.1 and 9.2.
Due Date:
How to Submit Your Work:
Change to your subdirectory 2270/hw09, which should contain all of your work for this assignment. Then run the command dora, which is the CSCI 2270 gradekeeping program. Select the option to submit an assignment. If you get a poor grade, you CAN resubmit. Dora will compute a new grade, and if the new grade is better then she throws out your old grade.
File that you must write:
  1. rec_fun.cxx: This file should contain the implementations of the four functions described below. You might also want to put the functions prototypes in a separate file rec_fun.h and write a test program that includes rec_fun.h.

Implement and Test Four Small Recursive Functions

1. Triangle Pattern

  void triangle(ostream& outs, unsigned int m, unsigned int n)
  // Precondition: m <= n
  // Postcondition: The function has printed a pattern of 2*(n-m+1) lines
  // to the output stream outs. The first line contains m asterisks, the next 
  // line contains m+1 asterisks, and so on up to a line with n asterisks.
  // Then the pattern is repeated backwards, going n back down to m.
  /* Example output:
     triangle(cout, 3, 5) will print this to cout:
Hint: Only one of the arguments changes in the recursive call. Which one?

2. Section Numbers

Write a function with this prototype:
  #include <strclass.h>
  void numbers(ostream& outs, const String& prefix, unsigned int levels);
The function prints output to the ostream outs. The output consists of the String prefix followed by "section numbers" of the form 1.1., 1.2., 1.3., and so on. The levels argument determines how may levels the section numbers have. For example, if levels is 2, then the section numbers have the form x.y. If levels is 3, then section numbers have the form x.y.z. The digits permitted in each level are always '1' through '9'. As an example, if prefix is the string "THERBLIG" and levels is 2, then the function would start by printing:
and end by printing:
The stopping case occurs when levels reaches zero (in which case the prefix is printed once by itself followed by nothing else).

The String class from <strclass.h> has many manipulation functions, but you'll need only the ability to make a new string which consists of prefix followed by another character (such as '1') and a period ('.'). If s is the String that you want to create and c is the digit character (such as '1'), then the following statement will correctly form s:

  s = (prefix + c) + '.';
This new String s can be passed as a parameter to recursive calls of the function.

3. A Teddy Bear Picnic

This question involves a game with teddy bears. The game starts when I give you some bears. You can then give back some bears, but you must follow these rules (where n is the number of bears that you have):

  1. If n is even, then you may give back exactly n/2 bears.
  2. If n is divisible by 3 or 4, then you may multiply the last two digits of n and give back this many bears. (By the way, the last digit of n is n%10, and the next-to-last digit is ((n%100)/10).
  3. If n is divisible by 5, then you may give back exactly 42 bears.
The goal of the game is to end up with EXACTLY 42 bears.

For example, suppose that you start with 250 bears. Then you could make these moves:

  • --Start with 250 bears.
  • --Since 250 is divisible by 5, you may return 42 of the bears, leaving you with 208 bears.
  • --Since 208 is even, you may return half of the bears, leaving you with 104 bears.
  • --Since 104 is even, you may return half of the bears, leaving you with 52 bears.
  • --Since 52 is divisible by 4, you may multiply the last two digits (resulting in 10) and return these 10 bears. This leaves you with 42 bears.
  • --You have reached the goal!

    Write a recursive function to meet this specification:

       bool bears(int n)
       // Postcondition: A true return value means that it is possible to win
       // the bear game by starting with n bears. A false return value means that
       // it is not possible to win the bear game by starting with n bears.
       // Examples:
       //   bear(250) is true (as shown above)
       //   bear(42) is true
       //   bear(84) is true
       //   bear(53) is false
       //   bear(41) is false
    Hint: To test whether n is even, use the expression ((n % 2) == 0).

    4. A Fractal Pattern

    Examine this pattern of asterisks and blanks, and write a recursive function that can generate patterns such as this:
    * * 
    * * * *
        * *
    * * * * * * * *
            * *
            * * * *
                * *
    With recursive thinking, the function needs only seven or eight lines of code (including two recursive calls). Your prototype should look like this:
      void pattern(ostream& outs, unsigned int n, unsigned int i);
      // Precondition: n is a power of 2 greater than zero.
      // Postcondition: A pattern based on the above example has been
      // printed to the ostream outs. The longest line of the pattern has
      // n stars beginning in column i of the output. For example,
      // The above pattern is produced by the call pattern(cout, 8, 0).
    Hints: You do not need to check the precondition. Think about how the pattern is a fractal. Can you find two smaller versions of the pattern within the large pattern? Here is some code that may be useful within your function:
      // A loop to print exactly i spaces:
      for (k = 0; k < i; k++) outs << ' ';
      // A loop to print n asterisks, each one followed by a space:
      for (k = 0; k < n; k++) outs << "* ";

    Michael Main (