Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [10pts] Fill out the following form: Honor quiz. You must obtain a 100%; retake the quiz until you get a 100%.
  2. [10pts] We would like you to self-test how much you retained from the lecture. Write the correct solution to the Mandatory Practice Problems. Debug your code until it runs correctly. Circle below how you wrote your code.
    1. (10 pts) I was able to write the code without referring to anything.
    2. (10 pts) I had to look at the notes, but still I was able to write the code without looking at the solution.
    3. (10 pts) I had to look at solutions to finish to code.
    4. (0 pts) I didn't do this.
    If you had to look at the solution, briefly describe what you missed but understand now.
    
    
    
    
  3. [10pts] Consider the following code:
    
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    bool readRow(int len);
    
    int main()
    {
      int rows, cols;
      char t;
      cin >> rows >> t >> cols;
    
      int count = 0;
      for(int r = 0; r < rows; r++)
        if (readRow(cols))
          count++;
    
      cout << count << endl;
      return 0;
    }
    
    bool readRow(int len)
    {
      int n = 0;
      for(int i = 0; i < len; i++)
      {
        char c;
        cin >> c;
        if (c == 'Y')
          n++;
      }
      return n > 0;
    }
    
    Identify the following components for function readRow by circling and labeling them in the code.
    1. Prototype
    2. Definition
    3. Parameter
    4. Argument
    5. Function call
  4. [10pts] Consider the following program. What will be the output? Try to solve it manually instead of compiling and running the code.
    
    #include<iostream>
    using namespace std;
    
    int f(int, int);
    
    int main()
    {
      int x = 2, y = 3;
    
      x = f(x,y);
      cout << "main: x=" << x << ", y=" << y << endl;
      
      y = f(y,x);
      cout << "main: x=" << x << ", y=" << y << endl;
    }
     
    int f(int x, int y)
    {
      x++;
      y += 2;
      cout << "f: x=" << x << ", y=" << y << endl;
    
      return x+y;
    }
    
    output:

  5. [60pts] Consider the following program hw.cpp (download it).
    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.
    As you see, the program is missing the prototype and 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)
    
    #include <iostream>
    using namespace std;
    
    //==========================================
    // Give the prototype of firstfactor below
    
    //==========================================
    // main function
    int main()
    {
      // Get integer n, n > 1, from user
      int n;
      cout << "Enter an integer larger than 1: ";
      cin >> n;
    
      // Print out factorization
      cout << "The factorization of " << n << " is ";
      while(n > 1)
      {
        // get & print next prime factor
        int f = firstfactor(n);
        cout << '(' << f << ')';
        n = n / f;
      }
      cout << endl;
    
      return 0;
    }
    
    //==========================================
    // Define firstfactor below
    
    

Turn in