CS302 Midterm, Fall, 2013

James S. Plank

Answers and Grading


Question 1: 10 points

This is permutation code, straight from the lecture notes, except I'm permuting integers rather than strings. Also, I'm starting the permutation with index 1, so the 10 will never be moved from slot 0. Since we are permuting 5 elements (the 10 never moves), there will be 5! lines printed.
UNIX> a.out | head -n 6
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 
UNIX> a.out | wc
     120     720    2280
UNIX> 
The answers are g and a

Grading

Part 1: 5 points for g. Three points for e or h.

Part 2: 5 points for a. Three points for b.


Question 2: 12 points

These are all straight from the lecture notes. Grading is 1.5 points each.


Question 3: 10 points

This is pretty straightforward bit arithmetic. Since the integer is less than 108, it is easily less than 230. In the code below, I calculate the largest digit that is set, and then go from that digit down to the smallest digit, adding either a '0' or a '1' to the string:

string bits(int n)
{
  int md, i;
  string rv;

  md = 0;
  for (i = 1; i < 31; i++) {
    if ( (1 << i) & n ) md = i;
  }
  for (i = md; i >= 0; i--) {
    if ( (1 << i) & n ) rv.push_back('1'); else rv.push_back('0');
  }
  return rv;
}

In q3.cpp, I've added main(), which reads from standard input, and prints the result of bits().

Grading

10 points. I grade these pretty much on how well I think your solution addressed the problem. I did deduct for the following problems:


Question 4: 12 Points

Grading

10 points. I grade these pretty much on how well I think your solution addressed the problem. I did deduct for the following problems:


Question 5: 10 Points


Question 6: 12 Points

This one was nearly identical to the Topcoder problem that I assigned in lab: TCCC 2005, 250-Pointer (Fairness). I had hints for that program in http://web.eecs.utk.edu/~jplank/plank/classes/cs302/Topcoder/Fairness.html, although since this program has no constraints, you can't do the second solution. Instead, you need to have a map keyed on the averages, whose second's are vectors of names. When you insert an average into the map, you call push_back() to put the name onto the vector.

The answer is in q6.cpp:

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

main()
{
  string s;
  map <double, vector <string> > m;
  map <double, vector <string> >::iterator mit;
  double t, n, avg;
  int i;

  while (cin >> s) {
    t = 0;
    n = 0;
    for (i = 0; i < s.size(); i++) {
      n++;
      t += s[i];
    }
    avg = t/n;
    m[avg].push_back(s);
  }
  for (mit = m.begin(); mit != m.end(); mit++) {
    for (i = 0; i < mit->second.size(); i++) {
      cout << mit->second[i] << endl;
    }
  }
  exit(0);
}

Grading

As always, grading on these programs is dictated by how well I feel your answer conveys that you know what's going on, and how well your code works. Roughly: