Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

  1. [14pts] Show how to rewrite the function prototype and definition below so that it could be used do its special printing of int's to either standard out (as it does now) or to a file. Note: the caller of the function will already have opened up the file prior to wanting to call print3.
    void print3(int d);
    
    
    
    void print3(int d)
    {
      if (d < 10)
        cout << "xx";
      else if (d < 100)
        cout << "x";
      cout << d;
    }
    	  
     
  2. [6pts] Given the following prototypes, mark each of the following function calls as "OK" or "ERROR". If the answer is "ERROR" explain what the problem is.
    void foo(int a, int& b);
    string bar(double k, string s);
    int i, j, k;
    double x, y, z;
    string u, v, w;
    

    1. foo(3,4)
    2. foo(i,j)
    3. foo(i,j+k)
    4. bar(3.0,4.0)
    5. bar(x,u)
    6. bar(x,u+v)

  3. [80pts] Write a program that will read in a fraction from the user and "pretty print" it, i.e. print it out in three lines with the numerator, horizontal line, then denominator. A typical run of your program might look like this:
    Enter fraction: -13/345877
    Pretty print is:
    
     -13
    ------
    345877
    In doing this, write a function writefrac that takes the numerator and denominator and prints out the fraction. Make the function so that I could pretty-print my fraction to a file or to the screen, even though your program might not make use of that flexibility. Now, I'm not going to tell you how to do your job, but writefrac would be a piece of cake to implement, if only you had a function length(int k) that would tell you how many characters would be required to print k, and maybe another function rep(char c, int k, ostream& OUT) that would print out k copies of c to the output stream OUT. Of course, that's just me.
    Turn In a printout of your program and a screen capture showing it running on the above input and, of course, this cover sheet. Your program must print out the horizontal bar no longer than the longer of the numerator and denominator, and also do some kind of centering so that 1/23457 doesn't look like this:
    1
    -----
    23457