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); 
      } 
    }

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
        *
       **
      ***
     ****
    *****
   ******
  *******
 ********
  1. Following is a program that uses iteration within the function pow() to calculate the power of a number.
#include <iostream>
using namespace std;

double pow(double, int);

int main() {
  double x;
  int n;
  cout << "Enter x: ";
  cin >> x;
  cout << "Enter n: ";
  cin >> n;
  double xn = pow(x,n);
  cout << "x^n is " << xn << endl;
  return 0;
}

double pow(double x, int n) {
  product = 1;
  for (int i = 0; i < n; i++)
    product *= x;
  return product;
} 

Rewrite pow so it performs the same task recursively. The prototype of pow and the main function should be untouched. No loops!

Turn In: This page, along with a screenshot of your program with the inputs x=4.2, n=3, and the codeprint output of your program.