
    CSc 109   Summer      Mid-Semester Test   15 June 2000
    ...........SUGGESTED ANSWERS.................
    1. (25 pts)  (a) Write a New Simpletron program which reads in two
      integers, which are assumed to be positive, and writes out the remainder
      obtained when the second number is divided by the first.
      (b) Write a New SimPL program which reads in two integers, which are
      assumed to be positive, and writes out the remainder obtained when the
      second number is divided by the first.
        New Simpletron Program         NewSimPL Program
         2000  read                     read x
         13    store x                  read y
         2000  read                     z=y-y/x*x
         14    store y                  write z
         7013  div by x                 halt
         6013  mul by x
         15    store in z
         1014  load y
         5015  sub z
         3000  write z
         5000
         8000  quit

    2.  (25 pts)  Write a program whose sole purpose is to write out either
     "Yes" or "No".  It writes out "Yes" if and only if the program is
     called with the same word appearing at least twice after the call on the
     command line.  That is, if the executable code for the program is stored
     in the file "sayes",  "Yes" is written when the program is run with the
     command "sayes tra  a b tra", but "No" is written when the program
     is run with the command "sayes" or with the command "sayes a b c".

     #include <fstream.h>
     void main(int argCt,char *args[]){
       for(int j=1; j<argCt; j++)
        for(int k=j+1; k<argCt; k++)
         if(strcmp(args[j],args[k])==0){
          cout<<"Yes\n";
          return;
         }
       cout<<"No\n";
       return;
     }


    3.  (25 pts)  Write a function "rightJustify" which takes a string and an
     integer, n, as arguments and returns a new string consisting of the old
     string, right justified: if the string has fewer than n characters, it
     appears in the new string, preceded by blanks, so that the last character
     from the old string is in location n-1.  If the string has more than n
     characters, only the first n characters appear in the new string.  For
     example, if we have   char s[]="hello there" then the call
       rightJustify(s,7) would return the string "hello t" and the call
       rightJustify(s,15) would return the string "    hello there"

     char * rightJustify(char *s,int n){
       char *temp;
       int oldLoc,loc,length;
       if(n<=0)
         return "\0";
       temp=new char[n+1];
       if(temp==NULL)
         return NULL;
       if(s==NULL){
         for(int j=0; j<n; j++)
           temp[j]=' ';
         temp[n+1]='\0';
         return temp;
       }
       loc=0;
       length=strlen(s);
       for(int j=0; j<n-length; j++){
         temp[loc]=' ';
         loc++;
       }
       oldLoc=0;
       while(loc<n){
         temp[loc]=s[oldLoc];
         oldLoc++;
         loc++;
       }
       temp[n]='\0';
       return temp;
     }

    4.  (25 pts)  Below is the interface for a version of the class MemCell
     which is for storing New Simpletron code.  It fails to protect against
     storing or retrieving values outside the range -9999....9999 and fails
     to return the opcode or the address of an instance of MemCell.  Write
     the definition of and the code for SubMemCell, a subclass of MemCell
     which cures these failings.
        class MemCell{
          public:
            MemCell();
            MemCell(int);
            void setValue(int);
            int getValue();
          protected:
            bool initialized;  //has a value been stored yet?
                               //if not getValue() should fail
            void check(bool ok,char*mess); //if !ok send the message mess and
                                      //quit the program
          private:
            int value;
        };

     class SubMemCell:public MemCell{
      public:
        SubMemCell();
        SubMemCell(int);
        int getOp();
        int getAddr();
        void setValue(int x);
     };

     SubMemCell::SubMemCell(){}

     SubMemCell::SubMemCell(int x):MemCell(x){
       check(x>=-9999 && x<9999,"Value out of range");
     }

     int SubMemCell::getOp(){
       check(getValue()>=0,"Illegal getOp()");
       return getValue()/1000;
     }

     int SubMemCell::getAddr(){
       check(getValue()>=0,"Illegal getAddr()");
       return getValue()%1000;
     }

     void SubMemCell::setValue(int x){
       check(x>=-9999 && x<=9999,"Value out of range");
       MemCell::setValue(x);
     }
