Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [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
  2. [8pts] 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:
  3. [82pts] Write a program hw.cpp that works as follows:
    1. It reads in a list of fractions from the user, each separated by commas and the whole terminated by a semicolon.
    2. From the list, it prints out only the fractions that are reduced to lowest terms.
    For example, a typical run might look like this:
    ~/$ ./hw
    2/4, 6/9, 3/11, 21/5, 8/10;
    3/11 is in lowest terms!
    21/5 is in lowest terms!
    
    In writing this program, define and use a function gcd that takes two positive integers and returns their greatest common divisor. (Check out Class 10 to remind yourself about computing gcd's.)

    Turn In the codeprint of your program.

    Submit:

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