Name: ____________________________________________________ Alpha: _____________ Section: ________

Describe help received: _________________________________________________________________

See the bottom for how to submit your work.

Due: Before 0800 on next class day

  1. (10 pts) Read today's lecture notes. Honestly declare how you read the notes.
    1. (10 pts) I read the notes carefully. I have a good understanding of the topics therein.
    2. (10 pts) I read the notes carefully, but I don't really understand the following points well:
      
      
      
    3. (0 pts) I didn't read the notes carefully.
  2. [10pts] Give the output of the following code:
    
    char fun(int x);
    
    int main() 
    {
      fun(3);
      cout << "KITTENS" << endl;
      fun(5);
      return 0;
    }
    
    char fun(int x) 
    {
      if (x <= 2) 
      {
        cout << "meow" << endl;
        return 'A';
      } 
      else 
      {
        char letter = fun(x-1);
        cout << letter << " " << x << endl;
        return (letter + 1);
      }
    }
    
    Output:
    
    
  3. [10pts] Give a recursive definition of a function double harm(int n) which computes the sum of the first n terms in the harmonic series: \[\frac{1}{1} + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + \cdots + \frac{1}{n}\] For example, harm(1) should return 1, and harm(2) should return 1.5.

  4. [70pts] Give a recursive function pow(x,n) that calculates x^n (i.e., x to the power of n). For example pow(2, 5) should return 32, and pow(3, 4) should return 81. No loops!
    ~/$ ./hw
    Enter x and n: 3.6  7
    x^n is 7836.42
    
    
    #include <iostream>
    using namespace std;
    
    double pow(double, int);
    
    int main() 
    {
      double x;
      int n;
      cout << "Enter x and n: ";
      cin >> x >> n;
      cout << "x^n is " << pow(x,n) << endl;
      return 0;
    }
    
    // TODO: Define pow: it must be recursive!
    
    
    

Submit to the submission server (due: 0800 on the next class day)

~/bin/submit -c=IC210 -p=hw35 hw.cpp

Bring to class