CS140 Coding Midterm 2

Fall 2016

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.
  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.
  6. You have the entire lab period. You may leave when you are finished. Please do not discuss the exam with anyone from another lab section until after 2:30pm.
  7. Remember to pass all object parameters by reference!
  8. Good luck!

  1. (15 points--CS140Fa16-Mid2-List): Write a function named findCaps that takes a list of words, wordList, and does two things: 1) returns a pointer to a new list of all words in wordList that start with a capital letter, and 2) deletes all words in wordList that start with a capital letter. Hence when the function returns, wordList will consist solely of words that start with lowercase letters. For example, if wordList starts as <"Brad", "Smiley", "summer", "Bill", "nancy"> then findCaps will return a pointer to a list containing the words <"Brad", "Smiley", "Bill"> and wordList will contain the words <"summer", "nancy"> when findCaps is finished.

    findCaps takes an STL list of strings as an argument and returns a pointer to an STL list of strings. You will need to allocate this new list in findCaps.

    It is okay for either the returned list or the edited wordList to be empty. For example, if wordList consists only of words that start with a capital letter, then wordList will be empty when findCaps returns.

    Constraints:

    1. Your algorithm must be O(n), which means that you cannot restart your search of wordList from the beginning each time that you delete a word from it. Instead you must remember where you were and pick up your search from that location.
    2. wordList could be empty
    3. Do not print anything. The main that is provided prints the lists that findCaps creates and manipulates.

    Handy Methods From The STL's List API

    Test Input and Driver Code: We have written a main program that reads test input, calls findCaps, and then prints the resulting lists.

    You can provide test input by providing a list of words. For example:

    	Brad
    	Smiley
    	summer
    	Bill
    	nancy
          

  2. (15 points--CS140Fa16-Mid2-Devices) You have gone to work for a telecommunications company named BTT that issues devices to customers. BTT needs to keep track of the devices it has issued to customers and for each customer it wants to keep track of the devices that have been issued to that customer.

    You are given the following data structures:

    class Customer {
       public:
          string name;
          string address;
          set<string> devices;
       public:
          Customer(string n, string a);
    };
    
    Customer::Customer(string n, string a): name(n), address(a) {}
    
    class Device {
       public:
          void addCustomer(string name, string address);
          bool addDevice(string deviceName, string customerName);
          void printCustomers();
       protected:
          set<string> devices;
          map<string, Customer *> customerMap;
    };
        
    You need to write the three member functions for Device:

    1. addCustomer: This function should create a new customer object and add the customer to the customerMap.
    2. addDevice: This function should verify that a device has not yet been issued. If it has been issued (i.e., it is in the devices map), then immediately return false. Otherwise add the device to the devices set and the appropriate customer's device set, and return true.
    3. printCustomers: This function should print the customers in alphabetical order and the list of their devices in alphabetical order. The format of the output should be as follows:
      1. One line for the customer's name and address. This line should be printed as follows:
        1. The customer's name left-justified in a field that is 15 characters wide
        2. A single space
        3. The customer's address left-justified in a field that is 30 characters wide.
      2. One line for each device name. This line should start with 4 blank spaces and then the device name.
      As an example:
      Brad             1678 Cardiff Rd.             
          BradLaptop
          BradPhone
          BradTablet
      	
    Constraints
    1. A new customer is guaranteed to be unique (i.e., you don't have to check to see whether or not it's already in the map).
    2. A new device is not guaranteed to be unique (i.e., you must verify whether or not it's in the set).
    3. The customer name associated with a device is guaranteed to already be in the map.

    Test Driver

    We have prepared a test driver that will test your member functions. The test driver accepts two commands:

    For example, the input:

    ADD_CUSTOMER Brad 1678 Cardiff Rd.
    ADD_CUSTOMER Smiley 4391 Redbud Dr.
    ADD_DEVICE BradLaptop Brad
    ADD_DEVICE BradPhone Brad
    ADD_DEVICE BradLaptop Brad
    ADD_DEVICE Laptop Smiley
    ADD_DEVICE Tablet Smiley
    ADD_DEVICE BradPhone Brad
    ADD_DEVICE BradTablet Brad
    ADD_CUSTOMER Nels 1838 Bluff Rd.
    
    should generate the output
    Brad             1678 Cardiff Rd.             
        BradLaptop
        BradPhone
        BradTablet
    Nels             1838 Bluff Rd.               
    Smiley           4391 Redbud Dr.              
        Laptop
        Tablet
        

    Handy Methods From The STL's Set API

    Handy Methods From The STL's Map API