Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [10 pts] 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.
    
    
    
    
  2. [20pts] Assuming the following function prototypes and variable definitions, fill in the table. Write ERROR if the expression cannot be compiled correctly.

    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
                               
    // Variable definitions
    int k = 4;
    double x = -3.8;
    string s = "The number of cookies I need: ";
    
    expressiontypevalue
    k + abs(x)
    x + abs(k)
    round(x) + k
    abs(x) = k
    k = round(x)
    s + to_string(k + 5)
    to_string(int(round(x)))
    (3 + 10)/round(x - 1)
    to_string(1) > to_string(2)
    to_string(1) + 3
  3. [70pts] 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, you need to define and use a function gcd that takes two positive integers and returns their greatest common divisor.

    Turn in