CSE 109 Test 1 Friday 24 February 2012
==========================SUGGESTED ANSWERS========================
1. Consider the code below, where the header of the function vex() is
missing (commented out):
#include <iostream>
using namespace std;

//void vex(int a, int b, int *c){      //version a
//void vex(int &a, int &b, int *c){    //version b
//void vex(int &a, int &b, int *&c){    //version c
  *c+=2;
  c=&b;
  a+=2;
  b+=a;
  *c+=b;
}

int main(){
  int x,y,*z;
  z=&x;
  x=1;
  y=2;
  z=&x;
  vex(x,y,z);
  cout<<x<<", "<<y<<", "<<*z<<endl;
  x=1;
  y=2;
  z=&x;
  vex(x,x,z);
  cout<<x<<", "<<y<<", "<<*z<<endl;
}

a. Write the output when main() is executed when the header for
   version a is used.
b. Write the output when main() is executed when the header for
   version b is used.
c. Write the output when main() is executed when the header for
   version b is used.
=====================================================================
a. 3 2 3 \  3 2 3
b. 5 14 5 \  20 2 20
c. 5 14 14 \ 20 2 20
=====================================================================

2. Here is an excerpt from class Word that we developed in class.

  class Link{
  public:
    Link *next;
    char segment[11];
    Link();
  };
private:
  Link * head,*tail;
  int count;
  static void check(bool b, const char *mess);

Write the declaration and definition (code) of a method for Word that is
called getSeg() and that gives access the a given segment in the
linked list representing a string. Below is code that calls getSeg(),
along with the indicated output:

  Word c("x fairly long string");
  cout<<c.getSeg(1)<<endl;    //OUTPUT: ong string
  c.getSeg(1)[2]='X';
  cout<<c.getSeg(1)<<endl;     //OUTPUT: onX string
==========================================================================
  char * getSeg(int index);

char * Word::getSeg(int index){
  Link *temp;
  temp=head;
  check(index>=0 && index<=(count-1)/10,"(getSeg()) Bounds error");
  for(int j=0;j<index;j++)
    temp=temp->next;
  return temp->segment;
}
==========================================================================

3. Given the class Link below, write a function, total(), that returns
the sum of the keys in a linked list. BUT FIRST, WRITE THE PRECONDITIONS
for the function. Below the class Link is code that shows how total()
should be called.
class Link{
public:
  int key;
  Link *next;
  Link(int k,Link * nxt=NULL);
};

Link::Link(int k,Link*nxt):key(k),next(nxt){}

int main(){
  Link *hd;
  hd=new Link(2);
  hd=new Link(3, hd);
  hd= new Link(4,hd);
  cout<<"The total is "<<total(hd)<<endl; //OUTPUT: The total is 9
}
====================================================================
//Preconditions:  no link points to a link occuring earlier in the list
//                (else infinite loop)
int total(Link *hd){
  int sum;
  sum=0;
  while(hd!=NULL){
    sum+=hd->key;
    hd=hd->next;
  }
  return sum;
}

4. I am thinking of writing a class for a thesaurus, but first I need
a class for entries in a thesaurus.  Entries consist of a word and up to
five synonyms. Declare such a class, called Synonym, and write the code
for such a class. It need only have enough methods for the code below to
compile and produce the indicated output when run.  You can assume you
have class Word available.

int main(){
  Synonym syn("Fine");
  (syn+="good")+="okey dokey";
  cout<<syn<<"\nTo repeat, the key is '"<<syn.getKey()<<"'\n";
  //OUTPUT: Synonym[Key: 'Fine' { 'good' 'okey dokey'} ]
  //        To repeat, the key is 'Fine'
}
========================================================================
class Synonym{
private:
  int count;
  Word key,data[5];
public:
  Synonym(const Word &k);
  Synonym & operator+=(const Word&w);
  friend ostream &operator<<(ostream &out,const Synonym&s);
  Word getKey()const;
};

Synonym::Synonym(const Word &k):count(0),key(k){}

Synonym & Synonym::operator+=(const Word&w){
  if(count>=5){
    cerr<<"Error: data overflow"<<endl;
    exit(1);
  }
  data[count]=w;
  count++;
  return *this;
}

ostream &operator<<(ostream &out,const Synonym&s){
  out<<"Synonym[ Key: "<<s.key<<" {";
  for(int j=0; j<s.count;j++)
    out<<" '"<<s.data[j]<<"'";
  return out<<"} ]";
}

Word Synonym::getKey()const{
  return key;
}
========================================================================
