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] 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
  3. [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:

  4. [70pts] 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)

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

     ~/bin/submit -c=IC210 -p=hw15 hw.cpp 
    Bring to class
    • A printout of this homework sheet annotated with your answers
    • The codeprint of hw.cpp.
    
    #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