Question 1: 8 Points

The program is in q1.cpp if you want to run it yourself.

Grading

2 points for A and B. Three points for C. One point for D.


Question 2: 12 points

The programs are in q2a.cpp, q2b.cpp and q2c.cpp, and the input files are in a.txt, b.txt, c.txt and d.txt, so you can run them yourselves:
UNIX> q2a < a.txt
7 8
UNIX> q2a < b.txt
10 11                - Cin reads words -- it doesn't care about lines.
UNIX> q2a < c.txt
1 6                  - The second cin failed, so j was unchanged.
UNIX> q2a < d.txt
5 6                  - The first cin failed, so both i and j were unchanged.
UNIX> q2b < a.txt
7.0 8.7
UNIX> q2b < b.txt
10.0 11.0
UNIX> q2b < c.txt
1.2 4.0                 
UNIX> q2b < d.txt
5.9 6.0              - printf() does rounding
UNIX> q2c < a.txt
7 8.7 9.7
UNIX> q2c < b.txt
10 11 21
UNIX> q2c < c.txt
1.2 4 5
UNIX> q2c < d.txt
Tyler 3 4
UNIX> 

Grading

You lost a point overall with multiple lines, commas, or extra decimal spaces.


Question 3: 8 points

This is a nuts-and-bolts stringstream problem. Obviously, the logic of your program can be slightly different from mine, but only slightly. In q3.cpp, I've included a main() that tests it by reading lines from standard input and passing them to stoivec(), then printing the resulting vector. The code below just implements stiovec:

vector <int> stoivec(string &s)
{
  vector <int> v;
  istringstream ss;
  int i;
  string dummy;

  ss.clear();   // This line is not necessary.
  ss.str(s);
  while (1) {
    if (ss >> i) {
      v.push_back(i);
    } else {
      ss.clear();   // This one is necessary.
      if (!(ss >> dummy)) return v;
    }
  }
}

Given the wording of the question, returning a vector of strings would have been fine too, as long as you return only the words that convert successfully to integers.

Grading

In particular, if you simply did "ss >> i" without any checking, you lost 3-4 points.


Question 4: 8 points

This tests your string arithmetic, and character arithmetic. q4.cpp has a main() with it that reads lines from standard input, creates a vector from the words, and prints out the return value of svectos.

string svectos(vector <string> &v)
{
  string s;
  int i;

  for (i = 0; i < v.size(); i++) {
    if (i != 0) s += " ";
    s += v[i];
  }

  for (i = 0; i < s.size(); i++) {
    if (s[i] >= 'A' && s[i] <= 'Z') s[i] += ('a' - 'A');
  }
  return s;
}

I was appalled by the number of students who used numbers like 97 for 'a'. Evidently I need to do a better job of teaching that you should use 'a' -- it is more readable and keeps you from making mistakes, which happened almost inevitably when you used numerical constants.

Grading

Same as question 3.


Question 5: 8 points

You should all be seasoned veterans at averaging numbers. Even though you are averaging integers, you should use doubles to compute the averages. q5.cpp uses the answer from question three to create the vector of integers from each line of standard input. When it is done reading, it calls avg():

typedef vector <int> IVec;

void avg(vector <IVec> &v)
{
  double n;
  double t;
  int i, j;

  for (i = 0; i < v.size(); i++) {
    if (v[i].size() == 0) {
      printf("Bad\n");
    } else {
      t = 0;
      n = v[i].size();
      for (j = 0; j < v[i].size(); j++) t += v[i][j];
      printf("%.3lf\n", t/n);
    }
  }
}

Grading


Question 6: 8 points (2 per part)


Question 7: 12 points

Grading

Each statement was .75 points with the exeception of C, E, F, G, K, and L which were .5 points.


Question 8: 8 points

This is from SRM 297, Division 2, 250-pointer. A solution is in q8.cpp. Obviously, yours can differ somewhat, but most solutions will look something like the one below:

#include "q8.h"

int PackingParts::pack(vector <int> partSizes, vector <int> boxSizes)
{
  int p, b, max;

  max = 0;
  b = 0;
  for (p = 0; p < partSizes.size(); p++) {
    while (b < boxSizes.size() && boxSizes[b] < partSizes[p]) b++;
    if (b == boxSizes.size()) return max;
    b++;
    max++;
  }
  return max;
}

I've put the class definition into q8.h, and I've written a main() routine that takes the example number as a command line arguments and prints the return value of pack(). That is in q8-Main.cpp.

Here's how they compile and run:

UNIX> g++ -o q8 q8.cpp q8-Main.cpp
UNIX> q8 0
3
UNIX> q8 1
2
UNIX> q8 2
3
UNIX> q8 3
4
UNIX> q8 4
3
UNIX> q8 5
6
UNIX> 
Many of you gave a solution like the following:

int PackingParts::pack(vector <int> partSizes, vector <int> boxSizes)
{
  int p, b, max;

  max = 0;
  for (p = 0; p < partSizes.size(); p++) {
    for (b = 0; b < boxSizes.size(); b++) {
      if (boxSizes[b] >= partSizes[p]) {
        boxSizes[b] = 0;
        max++;
        b = boxSizes.size(); // Or "break"
      }
    }
  }
  return max;
}

I gave full credit to this, because the size of the vectors is small (< 50 each). However, this solution is not the best, because the running time will be roughly partSizes.size()*boxSizes.size(). My solution will be max(partSizes.size(),boxSizes.size()), which is much faster.

This is an example of code that will work with topcoder, but isn't "good" code. Hence why I bring it up.

Grading

4 points for logic. 4 points for details.