CS302 Midterm Exam: October 16, 2008


Question 1

What is wrong with the procedure on the right, and how would you fix it?

void clear_vector(vector <double> v)
{
  int i;

  for (i = 0; i < v.size(); i++) v[i] = 0;
}


Question 2

What is the output of the program to the right:

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

main()
{
  const char *s1;
  const char *s2 = "Wilma";
  const char *s3;

  string st1;
  string st2;

  st1 = s2;
  st2 = st1;
  st1[2] = 'Y';
  st2[1] = 'X';

  s1 = st1.c_str();
  s3 = s1+1;

  cout << "s1: " << s1 << endl;
  cout << "s2: " << s2 << endl;
  cout << "s3: " << s3 << endl;
  cout << "st1: " << st1 << endl;
  cout << "st2: " << st2 << endl;
}


Question 3

The following is an array representation of a heap.

Draw the array representation of the heap after the value 10 is inserted.

Question 4

The following is an array representation of a heap.

Draw the array representation of the heap after a Delete_Min() operation.


Question 5

In the Tequila plant simulation, we ended up with this definition of a Distillation Unit:

Explain why we need to have failure_ptr as part of the Distillation Unit. Moreover, explain every place in the simulation that it is either set or used. You may want to draw the simulation's state diagram and use that for reference.

typedef multimap <double, class Event *> EQ;

class Dist_Unit {
  public:
    double lasteventtime;
    double uptime;
    double downtime;
    DU_State state;
    int id;
    EQ::iterator failure_ptr;
};


Question 6 - Note, this has three parts


Barney

Betty

Barney and Betty want to generate random numbers, and they show you the CDF plots above. Clearly, they both have problems. First, explain to me what CDF(x) is in English. Second, explain what a CDF plot shows. Finally, explain to me what the problems are with Barney and Betty's CDF plots.

Question 7

This question concerns the CDF to the right.
  • Part 1: Which range of numbers is more likely to be generated with this CDF:

    • 0.2 to 0.4
    • 0.6 to 0.8
    • 0.8 to 1.0

  • Part 2: Write a procedure called cdf_genrand() that uses drand48() to return a random number according to this CDF.


Question 8

You have an input file whose lines are in the following format:

Technique(one-word)Time(double)

Each line is a performance test of the given technique.

An example is the following file:

cauchy_good    0.225604
cauchy_good    2.804108
evenodd        1.247746
liberation     0.978000
cauchy_good    6.156661
rdp            1.005819
liberation     1.978000

Write a program that reads a file in the above format on standard input. After reading the file, it should print out the Technique which has the smallest average time. For example, suppose I call the program on the file above. It will print rdp, because it has the smallest average time (liberation's average is 1.478 and cauchy_good's average is 3.062124).

To help you out, I have put skeleton of the program on the answer sheet. You do not need to add any more variable declarations to this program. You may also assume that standard input is in the correct format, and there is at least one test on standard input.