
      CSc 17 Test 1  Thursday 10 June 1999

      ###########ANSWERS####################
  1.  (10 pts)Given the function definitions below, state what output is
          generated by the code below the dashed line.

          void b(int & x, int &y)
           {y++;
            x+=y;
            cout <<x<<" "<<y<<" ";
           }

          void a(int & x, int& y,int  z)
          { x=y+z;
            y=y+z;
            b(x,z);
            cout << x << " " << y << " " << z<<endl; }

     -----------------------------------------------------------
      int u=1,v=2,w=3;                              //9 4 9 5 4
      a(u,v,w);                                     //9 5 3
      cout << u << " " << v << " " << w << endl;    //8 8
      w=3;
      b(w,w);

  2.  (10 pts) In the code below find 10 syntax errors and no more.  I will
  only grade the first 10 syntax errors you identify.  Note that I have not
  provide definitions for all the functions, but that will not generate a
  syntax error, rather, if the code compiles, it will generate a linker
  error, because the definition(s) cannot be found.
     #include <fstream.h>
     void func(int a, bool b)        // MISSING ;
     int b(ifstream in)              // MISSING ;  // & NEEDED BEFORE in
     void main {                     // MISSING () AFTER main
        ifstream inF;

        b(inF);
        if(func(2,true))             // func DOES NOT RETURN ANYTHING
          cout<<"good";
          cout<<" day";
         else                        // MISPLACED else
          cout<<"bad day";
        func(6);                     // TOO FEW ARGUMENTS TO func
        cout<<b(3);                  // BAD ARGUMENT TO b
     }
     int b(ifstream in){             // & NEEDED BEFORE in
        in>>a;                       // MISSING return
     }

  3.  (10 pts)  What is the output when the following code is executed?

         int x=4,y=5;
         if(x>y) if(y>4) cout<< 6; else cout<< 7;     //
         cout<<endl<<(y/x*x);                         //4
         cout<<endl<<(2+x/3/2.0);                     //2.5
         cout<<endl<<('d'-'a'+10);                    //13
         switch(x){
           case 4: cout<<"\nhello";                   //hello
           case 5: cout<<"\n Hi";                     //Hi
         }

  4.  (10 pts)  I forgot to document the purpose of the following function.
    Provide the documentation.
    //Purpose: Return the largest of the three ints x, y, and z
    int quizzical(int x, int y, int z){
       if(x>=y && x>=z)
         return x;
       if(y>=z)
         return y;
       return z;
    }

  5. (30 pts)  Write the prototype and definition for the function "ascend".
    When we have double x,y; then after the call ascend(x,y) x and y are in
    ascending order, that is, x<=y.

    void ascend(double & x, double & y);
    void ascend(double & x, double & y){
       double temp;
       if(x>y){
           temp=x;
           x=y;
           y=temp;
       }
    }

  6. (30 pts) Given the class below, I wish to add to the class a member
   function "total()".  If we have Junk a; then the call a.total() should
   return the sum of the values stored in a. Write the prototype and the
   definition of this member function.
      class Junk{
         private:
           int x,y,z;
         public:
           Junk();
           Junk(int a, int b, int c);
           int getA();
           int getB();
           int getC();
      }


          int total();

          int Junk::total(){
                return x+y+z;
          }
