CSE 109 Test 1  Friday 22 February 2008
>>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<
1.  Write the output that occurs when the following program is run.
#include <iostream>
using namespace std;
void x(int a, int &b, int &c){
  int temp;
  temp=a; a=b; b=temp;
  b+=2;
  c+=a;
}

void y(int *a, int *&b){
 *a+=2;
 b=a;
 *b+=3;
 }

void z(int *a, int *&b, int *&c){
 int *temp;
 temp=a;
 a=b;
 b=temp;
 c=a;
}

int main(){
  int u=2, v=3, w=4, *r, *s, *t;
  x(u,v,w);
  cout<<u<<" "<<v<<" "<<w<<endl;
                                       <<<<<<<< 2 4 7
  u=2;
  x(u,u,u);
  cout<<u<<endl;
                                       <<<<<<<< 6
  s=&u; t=&v;
  u=2; v=3;
  y(s,t);
  cout<<u<<" "<<v<<" "<<*s<<" "<<*t<<endl;
                                       <<<<<<<< 7 3 7 7
  s=&u;
  u=2;
  y(s,s);
  cout<<u<<" "<<*s<<endl;
                                       <<<<<<<<  7 7
  u=2; v=3; w=4; r=&u; s=&v; t=&w;
  z(r,s,t);
  cout<<*r<<" "<<*s<<" "<<*t<<endl;
                                       <<<<<<<<  2 2 3

}
2.  Write a function select() that reads text from an input stream and writes
to the console only those characters that are in a given string. For example,
the code
    istream f("input.dat");
    select(f,"12345.!");
would display on the screen only those characters in the file 'input.dat' that
match one of '1', '2', '3', '4', '5', '.',  and '!'.  You should include
documentation of pre- and post-conditions for this function.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/*Purpose:  display on the screen only those characters in the file "in"
            that are among the characters stored in the string "goodstuff"
  Precondition: in is open for reading and and we are at the beginning
                of the file.
                goodStuff is not NULL and points to a string of characters
                (possibly empty) that terminates in '\0'.
  Postcondition:  all characters in "in" among those in goodStuff have
                 been displayed to the screen.
                 The chars in goodStuff have not been changed.
                 in.good() is false
*/
void select(istream &in,char *goodStuff){
  char ch;
  int ct;
  in.get(ch);
  while(in.good()){
    ct=0;
    while(goodStuff[ct]!='\0' && goodStuff[ct]!=ch)
      ct++;
    if(goodStuff[ct]==ch)
      cout<<ch;
    in.get(ch);
  }
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3.  Write the declaration and definitionss (code) for the class Rectangle
that represents a rectangle.  You need only implement those methods that are
necessary for the code below to compile and produce the indicated output.
No documentation is needed.
  Rectangle a(3.5,8);
  cout<<a<<" has dimensions "<<a[0]<<" and "<<a[1]<<" and has area "
    <<a.getArea()<<endl;
  // Rect[3.5,8] has dimensions 3.5 and 8 and has area 28
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <iostream>
#include <stdlib>
class Rectangle{
public:
 Rectangle(double a=0,double b=0);
 double getArea()const;
 double operator[](int j)const;
 friend ostream & operator<<(ostream &out,const Rectangle & r);
private:
 double x,y;
 static void check(bool b,char *mess);
};

 Rectangle::Rectangle(double a,double b):x(a),y(b){
   check(a>=0 && b>=0,"(Rectangle()) Bad dimensions");
 }

 double Rectangle::getArea()const{return x*y;}

 double Rectangle::operator[](int j)const{
   check(j==0 || j==1,"([]) Bad index");
   if(j==0)
     return x;
   return y;
 }

 void Rectangle::check(bool b,char *mess){
   if(!b){
     cerr<<"ERROR[Rectangle]: "<<mess<<endl;
     exit(1);
   }
 }

 ostream & operator<<(ostream &out,const Rectangle & r){
   out<<"Rect["<<r.x<<","<<r.y<<"]";
   return out;
 }
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
4. Assume the declaration for Link below, which is the class we developed
in class. Write a function inc() that traverses a linked list and increments
by one the value of 'data' in each link of the list. For example, given
Link *hd; the call would be inc(hd).  State the pre-conditions for the
function.
  class Link{
    public:
      Link(int a,double d=0,Link *nx=NULL);
      int key;
      double data;
      Link *next;
  };
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
/*Preconditions:  nx is defined and either points to NULL or to a
                  sequence of Links, the last of which points to NULL
*/
 void inc(Link *nx){
   while(nx!=NULL){
     nx->data++;
     nx=nx->next;
   }

 }
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
