CS140 Midterm 2

Spring 2018

Instructions

  1. Write your name clearly at the top of the exam
  2. You must answer all the questions on the exam. The exam has 9 problems. Make sure that you have all 9 problems.
  3. Write your answers on the exam pages and hand in the exam when you are finished.
  4. You have until the end of the class period to finish the exam. When time is called you must immediately drop your pen/pencil and hand in your exam.
  5. Ensure that you write clearly and legibly and that your diagrams are easy to read. Failure to do so may result in the deduction of points.
  6. The exam is closed note, closed book, closed neighbor.
  7. The total points available for this exam (lab coding questions plus this paper exam) is 150 points. There are 90 points on this exam and there will be 60 points on the coding portion of the exam.
  8. Good luck!

    
    
  1. (14 points) For each of the following questions circle the best answer from the above list. Sometimes it may seem as though two or more choices would be equally good. In those cases think about the operations that the data structures support and what I said about them in class. Then choose the data structure whose operations are best suited for the problem. You may have to use the same answer for more than one question.
     
         a. vector
         b. list
         c. deque
         d. map
         e. set
         f. multimap
         g. multiset
         h. hash table
    

  2. (8 points) Suppose I have the following declarations and code:
         int a, b;
         int *x, *y;
    
    Also suppose that the above variables are assigned the following memory addresses:
         a: 0x1000
         b: 0x1004
         x: 0x1008
         y: 0x100c
     
    After the following code sequence executes, what are the values of a, b, x, and y?
           x = &b;
           y = x;
           a = 10;
           b = 40;
           *x = 30;
           *y = *x * 3;
    
         a:
    
    
         b:
    
    
         x:
    
    
         y:
    
    
    
    
    
  3. (6 points) _____________________________________ What term is used to describe the problem with the following code? If you can't think of the term, then for 3 points give a 2 sentence or less answer that describes the problem.
      int *x;
      x = new int;
      *x = 40;
      x = new int;
      *x = 60;
    

  4. (10 points) Suppose I have the following declarations:
      class PersonNode {
      public:
        string name;
        PersonNode *next;
        PersonNode(string n) : name(n), next(NULL) {}
      }
    
      PersonNode *x;
      PersonNode *y;
      PersonNode *newnode;
    
    1. Draw the diagram that results when the following code is executed. Your diagram should look like the ones for linked structures in the class notes and should include the three pointer variables x, y, and newnode, plus any PersonNodes the code creates. If a variable has an unknown value, then put a ? next to it.
        newnode = new PersonNode("Frank");
        x = newnode;
        newnode = new PersonNode("Jackie");
        newnode->next = x;
        x = newnode;
          
      
      
      
      
      
      
      
      
            
    2. Execute the following, additional code and draw the resulting diagram using the same instructions for drawing the diagram that were given in part a. Do not start from scratch. This code builds on the code that was executed in part a:
        newnode = new PersonNode("Sue");
        y = x->next;
        newnode->next = y;
        x->next = newnode;
      
  5. (10 points) Explain what is wrong with the following code segment. The intent of the code is to maintain a map keyed by name. For each name we keep track of that person's friends. The code is supposed to add friend2 to friend1's set of friends. You should assume that the input is error-free. Use no more than 3 sentences to describe the problem. You do not have to explain how to fix the code, only what is wrong with it.
        map<string, set<string> > friendsMap;
        map<string, set<string> >::iterator friends_it;
        set<string> friendsSet;
        string friend1, friend2;
        while (cin >> friend1 >> friend2) {
            friends_it = friendsMap.find(friend1);
            if (friends_it == friendsMap.end()) {
                friendsMap.insert(make_pair(friend1, set<string>()));
                friends_it = friendsMap.find(friend1);
            }
            friendsSet = friends_it->second;
            friendsSet.insert(friend2);
        }
    
    
    
    
    
    
    
    
    
    
      
    
    
    
    

  6. (10 points) You are given the following code fragment:
    	class Person {
    	  public:
    	    string name;
    	    int age;
    	};
    
    	Person *p;
    
    	p->name = "Nancy";
    	p->age = 25;
          
    Answer the following questions about the code fragment:

    1. Using 3 sentences or less, describe what is wrong with this code fragment. The problem is not with the class declaration--it is provided only for descriptive purposes.
      
      
      
      
      
      
      
      
      
      	    
    2. What C++ statement(s) need to be added to this code fragment to correct it and where should they be added. I want to see syntactically correct C++ code.
      
      
      
      
      
      
      
      	    

  7. (12 points) Your boss has asked you to choose between two algorithms. The two algorithms have the following running times:
    	  Algorithm 1: T(n) = 60n3 + 5n*log n + 50000n + 20
    	  Algorithm 2: T(n) = 0.10 * 2n + n + 8
            

    1. What is the Big-O running time of Algorithm 1?

    2. What is the Big-O running time of Algorithm 2?

    3. The size of the input will be typically on the order of n > 50. Which of the two algorithms should you choose and why?
    
    
        
  8. (10 points) You have been asked to print out all names in a multimap whose salaries lie between $15,000 and $20,000. The declaration for the multimap is:
    	multimap<int, string> salaries;
          
    The key is an integer denoting the person's salary and the value is a string representing the person's name. Below are two code fragments which will correctly print out the names and salaries. lower_bound(15000) returns an iterator that points to the first element in the multimap whose salary is greater than or equal to 15,000. lower_bound() has the same Big-O running time as the find function.

    (a)(b)
       multimap<int, string>::iterator mit;
    
       for (mit = m.begin(); mit != m.end(); mit++) {
          if (mit->first >= 15000 && mit->first <= 20000) {
             cout << mit->second << " " << mit->first << endl;
          }
       }
    						     
       multimap<int, string>::iterator mit;
    
       mit = m.lower_bound(15000);
       for (; mit != m.end(); mit++) {
          if (mit->first > 20000)
              return;
          else
              cout << mit->second << " " << mit->first << endl;
       }
    			  

    Make the following assumptions:

    Answer the following questions:

    1. Which code segment is more efficient (a or b)?

    2. Explain why the code segment you selected is more efficient using Big-O notation. You will need to compute the Big-O running time of each of the two functions in order to justify your answer.
      
      
      
      
      
      
      
      
      
         
  9. (10 points) Draw a list diagram that shows what the following list looks like pictorially after the following code executes. Make sure that you include sentinel nodes and that you show where each list iterator points. Your diagram should look like the ones in the class notes.
    list<string> names;
    list<string>::iterator nit, nit1;
    list<string>::reverse_iterator rnit;
    names.push_back("joe");
    names.push_back("mary");
    names.push_front("nancy");
    names.push_front("tom");
    nit = names.begin();
    nit++;
    rnit = names.rbegin();
    nit1 = names.end();