Midterm 1 Solution

CS 23

  1. Unix

    Answer each of the following questions (each is worth 3 pts.):

    1. What is the command to print the files rational.cc and rational.h on the printer in the Unix lab?
      lpr -Plp rational.cc rational.h

    2. What is the command to copy all files in the directory ~kelliher/pub/cs23/hw3/ to the directory ~tom/hw3/ ? Your current position within the file system is ~tom/ .
      cp ~kelliher/pub/cs23/hw3/* hw3
      (Not the only way.)

    3. What is the command to compile the files main.cc, memory.cc, and io.cc, all in the current directory, into the executable program doom so that gdb may be run on it?
      g++ -g -o doom main.cc memory.cc io.cc

    4. How do you suspend an editor?

    5. How do you un-suspend a suspended program?

    6. How do you list all the files in the current directory?
      ls   # Misses the dot files, but close enough.

  2. Pointers and Dynamic Memory Allocation

    Answer each of the following questions (each is worth 9 pts.):

    1. Write a function, makecopy(), which dynamically allocates space to make a copy of an original string, returning the address of the copy of the string. Here's the prototype:
      char *makecopy(char *orig);
      The function should return the NULL pointer if a copy cannot be made.

      char *makecopy(char *orig)
         char *copy = new char[strlen(orig) + 1];   // Don't forget a position
                                                    // for the '\0' character.
         if (copy == NULL)
            return NULL;
         strcpy(copy, orig);
         return copy;

    2. What is printed by the following function? (Assume that an int is 4 bytes.)
      #include <iostream.h>
      int main()
         int *p;
         int *q;
         int array[6] = {63, 98, 52, 67, 78, 34};
         p = q = array;
         cout << (void*) array << endl;   // Assume that 0x1000 is printed.
         p = array;
         *p = 6;
         q = p + 3;
         cout << (void*) q << endl;
         cout << *q << endl;
         *p = 9;
         cout << q - p << endl;
         for (p = array + 1; p < array + 5; ++p)
            cout << (void*) p << "\t" << *p << endl;
         return 0;

      This is printed:

      0x1004      9
      0x1008      52
      0x100c      67
      0x1010      78

  3. C++ Classes

    Answer each of the following questions:

    1. (6 pts.) Compare and contrast public and private.

      A public member may be accessed by any function. A private member may be accessed only by a member function.

    2. (12 pts.) Design and implement a Factorial class. The constructor should take one parameter, n, and compute and store the value of n! . The member function Yield should return the value of n! .

      class Factorial
         int fact;
         Factorial(int n);
         int Yield(void);
      Factorial::Factorial(int n)
         int i;
         assert(n >= 0);
         for (fact = 1, i = 2; i <= n; ++i)
            fact *= i;
      int Factorial::Yield(void)
         return fact;

  4. Recursion

    Write a recursive function which determines if a string is a palindrome. A palindrome is a string which reads the same way forwards as it does backwards (e.g., radar).

    int palindrome(char *s)
       return palinCheck(s, s + strlen(s) - 1);
    int palinCheck(char *b, char *e)
       if (b >= e)
          return 1;
       else if (*b == *e)
          return palinCheck(b + 1, e - 1);
          return 0;

  5. Using Lists

    Assume that you are given a List class with the standard list operations:

    Write two functions (each worth 9 pts.) for manipulating a queue:
    void QueueInsert(List &l, int i);
    int QueueRemove(List &l);
    QueueInsert() should insert i at the tail of the queue. QueueRemove() should remove and return the integer at the head of the queue. Your functions should properly handle error conditions.

    void QueueInsert(List &l, int i)
       l.insert(i, l.size() + 1);
    int QueueRemove(List &l)
       int val;
       val = l.retrieve(1);
       return val;

Thomas P. Kelliher
Mon Mar 17 15:25:36 EST 1997
Tom Kelliher