2022 COSC 302 Midterm: Questions, Answers and Grading Guide

March 8, 2022.
James S. Plank

Question 1 : 15 points

(For some students, the order of Q1 and Q2 was flipped. Regardless, for your grading file, this is called Question 1):

These came from a bank. You can see an example in Exam.pdf. Here are answers for those questions:

Part A: There are 265 ways to have 5 lower-case letters, and 104 ways to have four single numeric digits. Therefore, the answer is: (265)(104).

Part B: This is equal to the number of 6 digit numbers in base 17: (176).

Part C: 17 choose 5, plain and simple: C(17,5) or C(17,12).

Part D: This is equal to the number of permutations of the colors: (11!).

Part E: This is the power set of the juices: (212).

Grading: Three points per. You lost 0.25 points if you didn't follow the instructions, and gave me, for example (17!)/(5!12!) instead of C(17,12). I gave some partial credit, but not a huge amount.


Question 2 : 15 points

(For some students, the order of Q1 and Q2 was flipped. Regardless, for your grading file, this is called Question 2):

These came from a bank. You can see an example in Exam.pdf. Here are answers for those questions:

Part A: Since node 5's links is -1, it is the root of a set. Nodes 6, 12 and 18 link to node 5, so they are in the set. Node 8 links to 18, and node 19 links to 6, so those are also in the set. That's it. The answer is: 5 6 12 18 8 19 (They can be specified in any order).

Part B:

Part C: When we call Find(1), the links go 5, 17, 8, -1. Therefore, we change the links for 1 and 5, and their values change to 8. 17's value doesn't change.

Grading

Each correct answer in Part A was worth 1 point. Incorrect answers were worth zero. You were deducted if you gave more than 6 answers.

Part B was worth 1.5 points per question.

Part C -- each node was worth 1 point and the final question was worth 1 point. You were deducted 0.5 points if you gave three nodes, and 1 point if you gave four nodes. You were deducted 2 points if you gave more than four nodes.

In the grading, if you see something like "2-19", it means that your answer was 19, and you were test #2 in the bank. Typically, you only see that on incorrect answers.


Questions 3-12

These were in shuffled order. Each was worth 1.5 points with some partial credit. You lost a full point for answering something like O(n+2) or O(3n/4) when the answer is O(n). Here are answers, ordered as they are in the grading file -- you identify the question by the name of the procedure (although two were named "p_c3b36" -- you can figure out which is which): There's a little more explanation in your Canvas exam file, after your answers to each question.


Question 13

Straightfoward recursive enumeration:

void binstrings(int n, int index, string &sofar)
{
  if (index == n) {
    cout << sofar << endl;
    return;
  }
  sofar[index] = 'A';
  binstrings(n, index+1, sofar);

  sofar[index] = 'B';
  binstrings(n, index+1, sofar);
}

Since I already resized sofar, you can simply set the characters, rather than doing push_back() and pop_back(). Doing push_back() and pop_back() is wrong because of the resizing in main().

Grading

The basic grading here was 4 points for a decent answer that had a base case, recursion on 'A' and recursion on 'B'. You got 1 to 2 points more if your base case was ok or correct, and 1 to 2 points more if your setting of 'A'/'B' recursions were correct. If you did push_back() and pop_back() correctly, you got 1 point for this (and a lot of you forgot the pop_back() at the end, so you got no points for that).

If your grade says "See The Answer", then your program was not structured correctly.


Question 14

You could do this in one of two ways -- as a Div/Mod recursion, or as a power-set recursion. Both are straightforward, and pretty much exactly as in the lecture notes. Here's the power-set:

void enum_cd(int n)
{
  int i, j, k;

  for (i = 0; i < (1 << n); i++) {
    for (j = 0; j < n; j++) {
      if (i & (1 << j)) printf("C"); else printf("D");
    }
    printf("\n");
  }
}

Here's the Div/Mod:

void enum_cd(int n)
{
  int top, i, j, k;

  top = (1 << n);

  for (i = 0; i < top; i++) {
    j = i;
    for (k = 0; k < n; k++) {
      printf("%c", 'C' + j%2);
      j /= 2;
    }
    printf("\n");
  }
}

Grading was 8 points, and I took off deductions. The most common deductions were on the div/mod enumerations, where you would div and mod by n rather than two. If your grading file says "Please see the answer", then your code didn't have the right structure for either enumeration.


Question 15

Collection is an interface, and MyImp implements the interface. That means you need to specify that in the definiton of MyImp, and that MyImp needs to implement the methods. MyImp also needs to specify a protected data structure to store the data. Here's the best answer, which would be in myimp.hpp:

#include <string>
#include <map>
#include <vector>
#include "collection.hpp"
using namespace std;

class MyImp : public Collection {          // This says it is a subclass of Collection
  public:
    void Add_Item(int id, const string &item);    // And these implement the virtual methods
    void Print();
  protected:
    map < int, vector <string> > Clxn;   // Here's the protected data
};

You also need to implement Add_Data() and Print(). Add_Data() simply adds the thing to the vector in the id's map entry:

void MyImp::Add_Item(int id, const string &item)
{
  Clxn[id].push_back(item);   // You can be more complex, but this does everything you need.
}

Print is a simple nested for loop:

void MyImp::Print()
{
  map <int, vector <string> >::iterator cit;
  size_t i;

  for (cit = Clxn.begin(); cit != Clxn.end(); cit++) {
    cout << cit->first << ":";
    for (i = 0; i < cit->second.size(); i++) {
      cout << " " << cit->second[i];
    }
    cout << endl;
  }
}

Grading

I broke up the grading into the following parts: If you wrote a destructor that had memory errors, you lost points (no destructor is needed) with most data structures. And if your data structure sorted the things, then you lost a half a point, because you are now using O(log(n)) opearations, when you could be using O(1) with a vector.