CS140 Midterm 2

Fall 2018

Instructions

  1. Use the Code Assessor development website, cs102dev.eecs.utk.edu, to answer each of the coding questions.
  2. Leave this worksheet next to your computer for the next lab section.
  3. You may have a one page cheat sheet, with writing on front and back.
  4. The name of the coding question on Code Assessor is shown next to each question. Both questions are in the CS140 category.
  5. You may not be able to finish all the problems. If so, please do not worry. We will examine all answers (even correct ones) and award partial credit. Make sure you budget your time so that you have time to write a complete solution to both problems. You can still obtain almost 100% credit even if your program is not running, so if you think your program is mostly correct but it is not running, move on to the other problem.
  6. You have 2 hours for this exam. Once you finish you should start working on this week's lab assignment. Please do not discuss the exam with anyone from another lab section until after 5:30pm.
  7. Remember to pass all object parameters by reference!
  8. Be careful not to make unnecessary copies of vectors or lists. You will lose points if you make such unnecessary copies.
  9. Good luck!

  1. (30 points--CS140Fa18-Mid2-Codes): Write a void function named deleteCodes that will delete used "Coke" codes from a list of outstanding codes. deleteCodes takes two arguments. The first argument is an STL list of strings which represent used codes. The second argument is an STL list of strings which represent all outstanding codes. deleteCodes should delete all codes in the "used codes" list from the "outstanding codes" list. For example, if the "used codes" list is {grb967, ppy175} and the "outstanding codes" list is {gej837, grb967, lrb818, osd630, ppy175}, then the "outstanding codes" list should be {gej837, lrb818, osd630} when deleteCodes is finished.

    Constraints:

    1. The used codes list is guaranteed to be a subset of the outstanding codes list (i.e., every code that appears on the used codes list will also appear on the outstanding codes list).
    2. Both lists are in sorted order. You can take advantage of this fact to traverse each list only once. If your function makes multiple passes through each list, thus turning what should be an O(n) algorithm into an O(n2) algorithm, you will lose 5 points.
    3. Do not print anything. The main that has been provided prints the result, which is the outstanding codes list once all used codes have been deleted.
    4. Do not read anything. The main that has been provided reads all the input and sets up the lists for you.
    5. Delete directly from the outstanding codes list. Do not make a new copy of this list.

  2. (30 points--CS140Fa18-Mid2-Clubs): You are going to write two parts of a C++ program that maintains information about students and the clubs to which they belong. Input to the program consists of two types of commands:

    For example, if your input is:

    AddStudent 48 Smiley
    AddStudent 35 Brad
    AddStudent 55 Nancy
    AddStudent 17 Nancy
    JoinClub 55 tennis
    JoinClub 55 bridge
    JoinClub 48 tennis
    JoinClub 17 tennis
    JoinClub 35 chess
    JoinClub 48 chess
          
    then the tennis club would have {Nancy, Smiley, Nancy} as members, the bridge club would have {Nancy} as a member, and the chess club would have {Brad, Smiley} as members.

    The data structures for the program are as follows:
    class Person {
    public:
      int id;
      string name;
    
      // initializes the id and name fields
      Person(int personId, string personName); 
    };
    
    class Club {
    public:
      string name;
      list<Person *> members; // the list of pointers to students' Person objects
    
      // initializes the name field and makes members an empty list
      Club(string n);
    };
    
    typedef vector<Club *> Bucket;
    
    class HashTable { // uses separate chaining
    public:
      HashTable(int tableSize);  // sets the table to the correct size
      void Insert(string clubName, Person *student);  // described below
      void PrintClubs();   // prints the bucket index for each club, the club's
                           // name, and the club's members
    protected:
      vector<Bucket> table;  // stores pointers to Club objects
    
      unsigned int Hash(string &key); // returns a random number
    };
    
    // These two variables are declared in main()
    HashTable *clubs;  // a hash table of clubs
    map<int, Person *> students;  // a map of students
          

    The two parts of the program you need to write are as follows:

    1. The HashTable's Insert function: This function should use the clubName as the key to find the pointer to the club's object and should then add the pointer to the student's Person object to the club's members list. If the club does not yet exist, then you should create a new club object using the one argument Club constructor we provided you and add this new club object to the hash table, as well as adding the student's Person object to its members list. The hash table is implemented using separate chaining so use the insert algorithm for separate chaining. You should add new clubs at the end of their buckets and you will need to convert the random number returned by the Hash function to a number between 0 and the tablesize.
    2. void AddStudentsAndClubs(HashTable *clubs, map<int, Person *> &students): This function reads the AddStudent and JoinClub commands from stdin and performs the appropriate action with them (this function is a standalone function--it does not belong to the HashTable class):

      • AddStudent: Create a new Person object for the student using the two argument constructor we provided you and add the pointer to this new Person object to the students map. You may assume that there are no duplicate student ids.
      • JoinClub: Find a pointer to the student's Person object using their id and then add the student to the club by calling the hash table's insert function. If you do not find the student's id, then skip this command. Please do not print anything out when you skip the command.

    Constraints:

    1. The input is error free. You cannot assume that all AddStudent commands come before JoinClub commands.
    2. It is guaranteed that a student will not try to join the same club twice.
    3. Do not print anything. There should not be a single cout statement in your code. We have written a function named HashTable::printClubs which we call at the end of main to print the buckets in which each club is stored along with the names of the clubs and the names of their members.
    4. The first line of the input contains the tablesize but we have already read that line and resized the table to the correct size. Your cin statements are reading AddStudent and JoinClub commands only.
    5. We have written all the supporting functions for you. Please do not waste your time reading them--they are not necessary for you to write the program. They are only necessary for us to test the program. I would hide them from you if I could because you will be wasting precious time if you attempt to read/understand them.
Handy Methods From The STL's List API

Handy Methods From The STL's Map API