Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [30pts] Consider the code below. The program is supposed to read in the first 10 words of the file tmp.txt, change any leading lower-case letters to upper case, and then print the words out. However, the program is buggy. Fix the code! Note the your program should output what's on the below right.
    ~/$ ./.a.out
    Brainy
    Level
    Shrug
    Unwritten
    Ready
    Disgusted
    Squealing
    Changeable
    Earthquake
    Male
    
    
    #include <iostream>
    #include <fstream>
    #include <cmath>
    using namespace std;
    
    string* getWords(string filename, int k);
    
    int main() {
       string* A = getWords("tmp.txt",10);
    
       for( int i = 0; i <= 10; i++ ) {
         if( "a" <= A[i][0] && A[i][0] <= "z" )
           A[i][0] -= 32;
       }
    
       for( int i = 0; i <= 10; i++ )
         cout << A[i] << endl;
    
       return 0;
    }
    
    string* getWords(string filename, int k) {
       ifstream fin(filename);
       string* W = new string*[k];
       while(--k >= 0 && fin >> W[k]);
       return W;
    }
    

    Turn in a screenshot showing your program running, and a codeprint printout of your code.

  2. [70 pts] The following is a program that uses iteration within the function pow() to calculate the power of a number. Rewrite pow so it performs the same task recursively. The prototype of pow and the main function should be untouched. No loops!
    ~/$ ./.a.out
    Enter x: 3.6
    Enter n: 7
    x^n is 7836.42
    
    
    #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) {
      double product = 1;
      for( int i = 0; i < n; i++ )
          product *= x;
      
      return product;
    } 
    

    Turn in a screenshot showing your program running, and a codeprint printout of your code.