CS302 Midterm Exam - October 8, 2013

Do your answers on the answer sheets provided. When you write code, you do not need to have "include" or "using" statements.

Question 1

Behold the program to the right, in q1.cpp.

Part 1: What are the first 7 lines of output of this program? Choose from the multiple choice answers below (use the answer sheet).

(a)
15 14 13 12 11 10 
14 15 13 12 11 10 
14 13 15 12 11 10 
13 15 14 12 11 10 
13 14 15 12 11 10 
12 13 14 15 11 10 
31 12 14 15 11 10 
(b)
10 15 14 13 12 11 
10 15 14 13 11 12 
10 15 14 12 13 11 
10 15 14 12 11 13 
10 15 14 11 12 13 
10 15 14 11 13 12 
10 15 13 14 12 11 
(c)
15 14 13 12 11 10 
15 14 13 12 10 11 
15 14 13 11 12 10 
15 14 13 11 10 12 
15 14 13 10 12 11 
15 14 13 10 11 12 
15 14 12 10 11 13 
(d)
10 11 13 12 14 15 
10 11 12 15 14 13 
10 11 12 15 13 14 
10 11 12 14 15 13 
10 11 12 14 13 15 
10 11 12 13 15 14 
10 11 12 13 14 15 
(e)
10 11 12 13 14 15 
10 11 12 13 15 14 
10 11 12 14 13 15 
10 11 12 14 15 13 
10 11 12 15 13 14 
10 11 12 15 14 13 
10 11 13 12 14 15 
(f)
10 15 11 12 13 14 
10 15 11 12 14 13 
10 15 11 13 14 12 
10 15 11 13 12 14 
10 15 11 14 13 12 
10 15 11 14 12 13 
10 15 12 13 14 11 
(g)
10 11 12 13 14 15 
10 11 12 13 15 14 
10 11 12 14 13 15 
10 11 12 14 15 13 
10 11 12 15 14 13 
10 11 12 15 13 14 
10 11 13 12 14 15 
(h)
10 11 12 13 14 15 
10 11 12 13 15 14 
10 11 12 14 15 13 
10 11 12 14 13 15 
10 11 12 15 14 13 
10 11 12 15 13 14 
10 11 13 12 14 15 
(i)
15 14 13 12 11 10 
15 14 13 12 10 11 
15 14 13 10 12 11 
15 14 13 10 11 12 
15 14 13 11 12 10 
15 14 13 11 10 12 
15 14 12 13 11 10 
(j)
10 15 14 13 12 11 
10 15 14 13 11 12 
10 15 14 11 12 13 
10 15 14 11 13 12 
10 15 14 12 13 11 
10 15 14 12 11 13 
10 15 11 12 13 14 

Part 2: How many lines of output will this program have? Again, choose from the multiple choice answers below. (use the answer sheet).

a: 5! b: 6! c: 10! d: 16!
e: 55 f: 56 g: 66 h: 65
i: 105 j: 106 k: 165 l: 166
m: (16!)/(10!6!) n: (16!)/(11!5!) o: (16!)/(6!) p: (16!)/(5!)
q: (10!)/(6!4!) r: (10!)/(5!5!) s: (10!)/(6!) t: (10!)/(5!)

#include <iostream>
#include <vector>
using namespace std;

class E {
  public:
    vector <int> v;
    void A(int index);
};

void E::A(int index)
{
  int i, j;

  if (index == v.size()) {
    for (i = 0; i < index; i++) {
      cout << v[i] << " ";
    }
    cout << endl;
    return;
  }

  for (i = index; i < v.size(); i++) {
    j = v[i];
    v[i] = v[index];
    v[index] = j;

    A(index+1);

    j = v[i];
    v[i] = v[index];
    v[index] = j;
  }
}

main()
{
  int i;
  E e;

  for (i = 10; i < 16; i++) e.v.push_back(i);
  e.A(1);
}

Question 2

For each of the following, tell me the running time. Use the answer sheet. For disjoint sets, assume that you are using union by rank with path compression.
  • A. Deleting an element from a map with n elements.
  • B. Constructing a heap from a vector with n elements.
  • C. Performing union() on a disjoint set instance with n elements.
  • D. Performing find() on a disjoint set instance with n elements.
  • E. Performing upper_bound() on a map with n elements.
  • F. Inserting an element into a map with n elements.
  • G. Performing pop() on a heap with n elements.
  • H. Performing push() on a heap with n elements.

Question 3

Write a procedure bits() which takes an integer as its only parameter and returns a string, which contains the binary representation of the integer. You may assume that the integer is non-negative, and less than 100,000,000. The result should not have any leading zeroes. I would prefer that you use bit arithmetic to solve this problem.

For example:

  • bits(1) should return "1".
  • bits(2) should return "10".
  • bits(3) should return "11".
  • bits(12) should return "1100".
  • bits(257) should return "100000001".