                CSc 17  Test 1   Wednesday  17 October 2001
===================SUGGESTED ANSWERS MARKED WITH "<<<<" ====================
1. (16 pts) Assume you have the function v() below (which I forgot to format):
      char v(int x, int y)
      {char ch;
       ch='C';
       if(x<y) if(x>0) if(x<5) ch='D';
       else {if(x>77) ch='A';} else ch='B';
       return ch;
      }

    For each of the four lines of code below state the output.
    a) cout << v(3,8);        <<<<< D
    b) cout << v(6,8);        <<<<< C
    c) cout << v(80,90);      <<<<< A
    d) cout << v(-6,7);       <<<<< B

2. (16 pts)  State the output of each of the following code fragments.
    a) cout<<(8/7+12*3/5%5);         <<<<< 3
    b) cout<<(8.0/6+12.0*(3/5%5));   <<<<< 1.3333
    c) if((6>7||8<9)==(6>7&&9<8))    <<<<< 2
         cout<<1;
       else if(6>7==false==true)
         cout<<2;
       else
         cout<<3;
    d)  cout<<('E'-'A')<<char('F'-'A'+'a'+1);  <<<<< 4g

3. (18 pts) State the output of the following main program.

    bool v(int &a,int &b,int c)
     {a++;
      b=b+a;
      return a+b>3*c;
     }
    void out(bool t,int a, int b)
    {if(t)
      cout<<a<<"  "<<b<<endl;
     else
      cout<<b<<"  "<<a<<endl;
    }
    int main()
     { int a,b;
       bool t;
       a=2;  b=3;
       t=v(a,b,5)
       out(t, a, b);                 <<<<< 6  3
       a=2;   b=3;
       t=v(b,a,8) && v(b,a,8);       <<<<< 4  6
       out(t, a, b);
       a=2;
       t=v(a,a,3*a);                 <<<<< 6  6
       out(t, a, a);
     }


      4. (18 pts) I forgot to document the following function. Provide it.
   //Purpose: <<< change the input values to positive numbers and then remove
   //         <<< all factors common to the two numbers, except that when one
   //         <<< of the numbers is 0, simply change the other number to a
   //         <<< positive number if it is negative
   //Pre-conditions: <<<none
   //Post-conditions: <<<a and b are positive.  If x=greatest common divisor
   //                 <<<(gcd) of original a and b, the new a and b are
   //                 <<<positive and (new a) = (old a)/x
   //                 <<<             (new b) = (old b)/x
   void xxx(int &a, int &b)
   {int c;
    c=2;
    if(a<0)
     a=-a;
    if(b<0)
     b=-b;
    while(c<=a && c<=b)
     {while(a%c==0 && b%c==0)
       {a=a/c;
        b=b/c;
       }
      c++;
     }
   }

5. (32 pts) Add to the class A below declarations of a constructor, a member
function getX(), and overloaded operators "==" and "<<", and provide the
corresponding definitions of the structures you have declared so that the
main program below the class declaration will compile and produce the output
shown in the comment below the main program.

  State the declarations, indicate where they appear in the declaration of
  A below, and then provide the definitions below main().

  class A{
    public:
       A();
       void set(int a,char b);
       A(int a, char b);                         //<<<<<<Part of answer
       int getX()const;                          //<<<<<<Part of answer
       bool operator==(const A &anA);            //<<<<<Part of answer
       friend ostream & operator<<(ostream &out,
                 const A &anA);                  //<<<<<<Part of answer
    private:
       int x;
       char y;

  };



  void main(){
     A r(2,'a'), q(3,'a'),s(3,'a');
     cout<<"'"<<r<<" == "<<q<<"' is ";
     if(r==q)
      cout<<"true\n";
     else
      cout<<"false\n";
     cout<<"'"<<q<<" == "<<s<<"' is ";
     if(q==s)
      cout<<"true\n";
     else
      cout<<"false\n";
     cout<<"The x-component of "<<r<<" is "<<r.getX()<<endl;
  }

//'A[2, 'a'] == A[3, 'a']' is false
//'A[3, 'a'] == A[3, 'a']' is true
//The x-component of A[2, 'a'] is 2

   A::A()
   {x=0;
    y='\0';
   }

   A::A(int a, char b)//<<<<<<Part of answer
   {set(a,b);}

   int A::getX()const //<<<<<<Part of answer
   {return x;}

   bool A::operator==(const A &anA)//<<<<<<Part of answer
   {return x==anA.x && y==anA.y;}

   ostream & operator<<(ostream &out,
               const A &anA)//<<<<<<Part of answer
   {out<<"A["<<anA.x<<", '"<<anA.y<<"']";
    return out;
   }

   void A::set(int a,char b)
   {x=a;
    y=b;
   }
