Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [20pts] Read today's lecture notes. Honestly declare how your read the notes. You will get the full credit only if you read the notes carefully.
    1. [20pts] I read the notes very carefully. I have a complete understanding of the notes.
    2. [20pts] I read the notes, but a couple of things don't make sense. (Describe biefly below what confuses you.)
      
      
        
    3. [12pts] I just skimmed the notes.
    4. [0pts] I didn't read the notes.
  2. [20pts] Assuming the following function prototypes and variable definitions, fill in the table. Note: each expression should be taken as independent. i.e. if one expression modifies some variable values, those modifications do not carry over to the next expression.
    
    // Function prototypes - each function what the name says it does
    int abs(int j);            // returns "absolute value" of j.  defined in library cstdlib
    double round(double x);    // returns x rounded to nearest integer. defined in library cmath
    string to_string(int val); // returns string representing val. defined in library string
                               // Note: to_string works only if you run g++ with option -std=c++11 
    // Variable definitions
    int k = 4;
    double x = 3.8;
    string s = "The number of cookies I need: ";
    
    expressiontypevalue
    k + abs(-4)
    x + abs(-4)
    round(x) + k
    k = round(x)
    s + to_string(k + 5)
    to_string(int(round(x)))
    (3 + 10)/round(x - 1)
    k++ < x
    ++k < x
    k++ < x && k > x
  3. [10pts] istream objects have a function "get()" that can be useful. We have not yet needed it, but you call it like cin.get() or fin.get() when fin is an ifstream. The function returns the next character in the stream, even if it is a whitespace character. Remember that our istreams have always helped us by skipping whitespace. But sometimes you might need to know what spaces are there! It has the following prototype:
    int get();
    Note the function returns an int. Given this, write below the output of the following programs when the users inputs the letter U.
    
    #include <iostream>
    using namespace std;
    int main() {
    
       cout << cin.get() << endl;
       return 0;
    }
    
    
    
    #include <iostream>
    using namespace std;
    int main() {
       char c = cin.get();
       cout << c << endl;
       return 0;
    }
    
    [Assume the users inputs U]
    Output: 
    [Assume the users inputs U]
    Output: 
  4. [50pts] Consider the following program. The program reads an integer from the user and prints out its factorization into prime numbers. Unfortunately, I've been unable to figure out how to implement the function firstfactor:
    • It takes as input an integer n, where n > 1
    • It returns the smallest factor of n, i.e. the smallest integer greater than 1 that divides n evenly.
    
    #include <iostream>
    
    // PROTOTYPE: IMPLEMENT THIS FUNCTION BELOW main!!
    int firstfactor(int);
    
    int main() {
      // Get integer n, n > 1, from user.
      int n;
      cout << "Enter an integer larger than 1: ";
      cin >> n;
    
      // Print out the factorization.
      cout << "The factorization of " << n << " is ";
      while( n > 1 ) {
        // Get & print the next prime factor.
        int f = firstfactor(n);
        cout << '(' << f << ')';
        n = n / f;
      }
      cout << endl;
    
      return 0;
    }
    
    As you see, the program is missing the definition of the function firstfactor. Complete the program by defining the function (a description of what the function is supposed to do is given in the source code's comments). When your program is working correctly, a typical run might look like this:
    Enter an integer larger than 1: 60
    The factorization of 60 is (2)(2)(3)(5)
    Turn in a printout of this cover sheet with your answers to the questions, your source code, and a screen capture of your program running the input 21978.

    Challenge (optional) modify the program so that it prints multiple factors with an exponent, rather than repeated, so that the factorization of 40 would be given as (2)^3(5) rather than (2)(2)(2)(5).