CIS 22 - EXAM I
Spring 1998

  1. Convert the following infix expressions to BOTH prefix and postfix:
    1. ((A+B) - (C+D) * E) / F
    2. (A+B) * (C/D * E + F ) / (G-H)

  2. This question refers to the number of ways one can form a committee of k people out of a group of n people. (This is the binomial coefficient "n choose k".) Note that n>k, n>=1, k>=1. The binomial coefficient is defined below:
    		binom(n,1) = n
    		binom(n,n) = 1
    		binom(n,k) = binom(n-1,k) + binom(n-1,k-1)
    
    1. Write a recursive function to evaluate binom(n,k). Remember to check for invalid parameters.
    2. For each of the following, trace the function calls and show what value is returned: (Note that the call may contain invalid data, in which case, you should show what the function would do.)
      1) binom(5,4) 2) binom(4,2) 3) binom(6,3) 4)binom(2,5) 5)binom(3,0)

  3. This question refers to the array implementation of a stack.
    1. Give the declaration for the Stack class of integers using the array implementation.
    2. Write the function push for the stack of part a. (You may assume that the functions isempty and isfull are available).
    3. Write the function pop for the stack of part a. (You may assume that the functions isempty and isfull are available).
    4. Explain how you could modify your Stack class so that it could be used for a Stack of any data type. (For example, your goal may be to allow the same program to include both a Stack of integers and a Stack of doubles.)

  4. This question refers to the node implementation of a Stack of integers.
    1. What changes have to be made to the stack.h file of question #3 if you would want to change the implementation to use the node implementation of stacks? (Note that you are NOT being asked about the stack.cpp file.)
    2. Name one advantage of the node implementation of a stack over the array implementation of a stack.

  5. Write a C++ class for a Rectangle object. You must provide both the rect.h and the rect.cpp files. The private implementation should consist of fields to contain the Rectangle's length and width. The public member functions should include the following:
    1. a constructor Rectangle(l,w) which should initialize a Rectangle with length l and width w.
    2. a print function, which should print out the length and the width.
    3. a function to compute the area of the Rectangle (The area of a rectangle equals its length multiplied by its width.)
    4. a function to compute the perimeter of the Rectangle (The perimeter of a rectangle equals the sum of the lengths of all four sides, or in other terms, 2*(length+width) ).
    5. a function is_square() which will return 1 if the Rectangle is a square, 0 if it is not. (A square is a rectangle whose length and width are equal.)
    6. There is no need to write a destructor for the Rectangle class. Explain why.