CSE 109 Test1  Wednesday   4 October 2006
>>>>>>>>>>>>>>>>>>>>>>>>>>>SUGGESTED ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1. Susan Bytesenbits has designed classes A, B, C, and D, whose declarations
are below, storing the declarations in the corresponding files a.h, b.h, c.h,
and d.h. She stored the corresponding code, which I have indicated in comments,
in corresponding .cc files a.cc, b.cc, c.cc, and d.cc. She stored the main
program, also show below, in p.cc.  Write a makefile for Susan. Include a
command for "cleaning up" the directory where the files are loocated. The
executable file should be stored in the file "BytesenB".

int main(){D a; }  //not much of a program. She will add more later
class A{int j; void x(); /* void x{} also not much of a function */ };
class B{ A a; void t(); /* void t{a.x();} */ };
class C{ B b; void q();  /* void q{b.t();} */};
class D{ C c; void v();  /* void v{c.q();} */};
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
   OPT= -c -Wall -Werror
   BytesenB: p.o a.o b.o c.o d.o
        g++ -o BytesenB p.o a.o b.o c.o d.o
   p.o: p.cc a.h b.h c.h d.h
        g++ $(OPT) p.cc
   a.o: a.cc a.h
        g++ $(OPT) a.cc
   b.o: b.cc a.h b.h
        g++ $(OPT) b.cc
   c.o: c.cc a.h b.h c.h
        g++ $(OPT) c.cc
   d.o: d.cc a.h b.h c.h d.h
        g++ $(OPT) d.cc
   clean:
        rm -f *.o *~ BytesenB
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2. Write out the output that would appear on the screen when the code
fragments listed at (a), (b), and (c) are executed.
void x(int a, int &b, int &c, int d[])
{a++;
 b+=a;
 c+=b;
 d[2]+=3;
}
(a) int r=0, s=1, t=2, a[]={1,2,3};
    x(r,s,t,a);  cout<<r<<" "<<s<<" "<<t<<" "
    <<a[0]<<" "<<a[1]<<" "<<a[2];
(b) int r=0, s=1, t=2, a[]={1,2,3};
    x(a[0],a[1],a[2],a);
    cout <<a[0]<<" "<<a[1]<<" "<<a[2];
(c) int a[]={1,2,3};
    x(a[2],a[2],a[2],a);
    cout <<a[0]<<" "<<a[1]<<" "<<a[2];
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
(a)    0 2 4 1 2 6
(b)    1 4 10
(c)    1 2 17
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

3.  Consider the abstract data type (ADT) for a traffic light. It can be in
one of three conditions, RED, YELLOW, and GREEN.  Each time it "turns" it
changes color, in the order GREEN, YELLOW, RED, GREEN,...  Write a class
to implement this ADT.  The class need only have the functionality needed
for the program below to produce the output indicated in the comments. To
make the writing easier, I call the class TL (for Traffic Light).
TL a, b(TL::RED), c(b);
cout<<a<<" "<<b<<" "<<c<<endl;  //OUTPUT: [Green] [Red] [Red]
a.turn().turn();
cout<<a<<endl;                  //OUTPUT: [Red]
a.turn().turn();
cout<<a<<a.RED<<a.YELLOW<<a.GREEN<<endl; //OUTPUT: [Yellow]210
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <iostream.h>
using namespace std;
class TL
{public:
   static const int RED=2, YELLOW=1, GREEN=0;
   TL(int col=GREEN);
   TL(const TL &t);
   TL & turn();
   friend ostream & operator<<(ostream &out,const TL &t);
 private:
   int lightColor;
};

   TL::TL(int col):lightColor(col)
   {if(col<GREEN || col>RED)
   col=GREEN}

   TL::TL(const TL &t):lightColor(t.lightColor){}

   TL & TL::turn()
   {lightColor=(lightColor+1)%3;
    return *this;
    }

ostream & operator<<(ostream &out,const TL &t)
{out<<"[";
 switch(t.lightColor)
  {case TL::GREEN: out<<"Green"; break;
   case TL::YELLOW: out<<"Yellow"; break;
   case TL::RED: out<<"Red"; break;
  }
 out<<"]";
 return out;
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
4.  Write a function, sumSquares, which returns the sum of the squares
of the first n entries of an array.  Given the declaration double x[]={1,2,3},
the call sumSquares(x,3) would return 14, and the call sumSquares(x,2) would
return 5.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int sumSquares(int v[], int n)
{int sum=0;
 while(n>0)
    {n--;
     sum+=v[n]*v[n];
    }
 return sum;
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
P