Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. Consider the program below:
    #include <iostream>
    using namespace std;
    
    void tri(int n, int level);
    void rep(char c, int k);
    
    int main()
    {
      tri(8,8);
      return 0;
    }
    
    void tri(int n, int level)
    {
      if (level == 0) 
        return;
      tri(n,level-1); 
      rep(' ',level);
      rep('*',1 + n - level);
      cout << endl;
    }
    
    void rep(char c, int k)
    { 
      if (k > 0) 
      { 
        cout << c; 
        rep(c,k-1); 
      } 
    }
    1. Compile and run this program. You should see that it prints out a sort of skewed triangle of *'s, point down. Question: Clearly the function rep works, but it is a recursive function that does not appear to have a base case. Without a base case we must have an infinite recursion, so the base case must be there after all, just hidden a bit. What is the base case for rep?
    2. Annotate this code to show how the recursive function tri() would have to be modified in order to print the triangle point up, like this:
      ~/$ ./hw0
              *
             **
            ***
           ****
          *****
         ******
        *******
       ********
  2. Consider the (incomplete) program hw1.cpp. It's the same as the program above, except that it replaces the call rep('*',1 + n - level); with the call repsep('*',1 + n - level); — a call to a different function "repsep()", that you'll have to define. The new "repsep()" function will do just what rep() does, i.e. print k copies of the character c, except that the c's will be separated by "-" characters. Thus, the call repsep('X',5) should print to standard out:
    X-X-X-X-X
    Note: you must define repsep without any loops; it must be recursive! I'd start with the rep() function and modify it. If you've defined it properly, running the program should produce the following output:
    ~/$ ./hw1
     *-*-*-*-*-*-*-*
      *-*-*-*-*-*-*
       *-*-*-*-*-*
        *-*-*-*-*
         *-*-*-*
          *-*-*
           *-*
            *
Turn In: This page, filled in, along with codeprint output of hw1.cpp with your definition of repsep(), and a screenshot of your program running.