CS140 Midterm 1

Spring 2015

Instructions

  1. Write your name clearly at the top of the exam.

  2. The exam has 10 problems worth 120 points. This is a long exam and you may have difficulty finishing it if you do not code rapidly. You should make sure that you solve the problems you find easiest.

  3. Write your answers on the exam.

  4. Ensure that you write clearly and legibly. Failure to do so may result in the deduction of points.

  5. The exam is open note, open book, closed neighbor.

  6. With the exception of students taking this exam on Blackboard, no electronic devices are allowed and all electronic devices that you have with you must be turned off. If you are taking the exam via Blackboard, then the browser window must take up your entire screen, you may use no other software during the exam, and you must not leave the blackboard exam page (i.e., you may not go to any other sites on the internet, nor to other blackboard pages).

  7. Good luck!


  1. (5 points) 0x1d78 + 0xb6 = _______________ (I want the hexadecimal result)
    
    
    
    

  2. (8 points) In a function, why should you use reference parameters for objects (e.g., strings, vectors, instances of classes), rather than value parameters? For example, if you want to pass a string object to a function, you should declare the string parameter as follows:
    void mystery(string &name) { ... }
    
    Use no more than 3 sentences for your answer.
    
    
    
    
    
    
    
    
    
    

  3. (18 points) The following hash table already has the values 22, 28, and 17 inserted. Suppose that 48, 39, and 62 are the next three keys inserted, in that order. Show where they would be inserted into the following two hash tables using linear probing and quadratic probing. You should use the hash function:
    h(key) = key % 11
    
    Please write the keys into the hash tables, and also, for each key, please complete the following table showing the list of entries you would probe in order to insert the key:
    keylinear probingquadratic probing
    2200
    2866
    176, 76, 7
    48  
    39  
    62  
                    Linear Probing                     Quadratic Probing
               -----------------------              -----------------------
          0    | 22                  |              | 22                  |
    	   |                     | 	        |                     | 
               -----------------------              -----------------------
          1	   |                     |              |                     |
    	   |                     |              |                     |
               -----------------------              -----------------------
          2    |                     |              |                     |
    	   |                     |              |                     |
               -----------------------              -----------------------
          3	   |                     |              |                     |
    	   |                     |              |                     |
               -----------------------              -----------------------
          4	   |                     |              |                     |
    	   |                     |              |                     |
               -----------------------              -----------------------
          5	   |                     |              |                     |
    	   |                     |              |                     |
               -----------------------              -----------------------
          6	   |                     |              |                     |
    	   | 28                  |              | 28                  |
               -----------------------              -----------------------
          7	   |                     |              |                     |
    	   | 17                  |              | 17                  |
               -----------------------              -----------------------
          8	   |                     |              |                     |
    	   |                     |              |                     |
               -----------------------              -----------------------
          9	   |                     |              |                     |
    	   |                     |              |                     |
               -----------------------              -----------------------
          10   |                     |              |                     |
    	   |                     |              |                     |
               -----------------------              -----------------------
    
  4. (8 points) Suppose you are given the following code fragment:
    string name = "am";
    unsigned char h = 19;
    int i;
    for (i = 0; i < name.size(); i++) {
        h = (h << 3) ^ name[i];
    }
    
    where 'a' = 0x61 and 'm' = 0x6d
    
    What is the hexadecimal value of h after each iteration of the above loop?

    iterationh
    0 
    1 

    If you cannot figure out the hexadecimal value, you can give the base-10 value for a 2 point deduction.

    
    
    
  5. (5 points) ___________________ What is the output of the following printf statement:
    
    
    char value = 'a' + 6;
    printf("%c", value);
    

  6. (12 points) Suppose you have the following three declarations:
    string licensePlate, state, count;
    
    licensePlate is a string variable that represents a license plate of the form "state-digits", such as "tennessee-941". Assume that you do not know how many letters precede the dash, nor how many digits are in the number. Write a fragment of C++ code to extract the strings representing the letters and the digits from license plate and assign these two strings to state and count respectively.
    
    
    
    
    
    
    
    
    
  7. (10 points) Given the following variable declarations, write a printf statement that outputs name in a left-justified field of 20 characters, salary in a right-justified field of 10 characters with 2 decimal digits, and age in a right-justified field of 4 characters. There should be a single space between each of the fields.
    string name;
    double salary;
    int age;
    
    
    
    
    
    
  8. (18 points) Write a void function named pad_left that takes a vector of ints and pads the left side of the vector with pad digits whose value is value. The three parameters are the vector of ints, an integer pad and an integer value. For example, if the original vector is:

    012345
    121725364348

    and your function is asked to pad by 3 digits with a value of 6, the resulting vector should be:

    012345678
    666121725364348

    Your code does not have to do any error checking.

  9. (18 points) Moonglow is back and he's less distracted. He's created a grade file that lists a student's scores and then the student's name. The grade file may have multiple students. For example:
    89
    75
    93
    53
    BabyDaisy
    86
    45
    100
    Smiley
    
    Write a main function that reads student scores from stdin, and that outputs the student's name and the student's maximum score. For example, for the above file your program should output:
    BabyDaisy 93
    Smiley 100
    
    You are guaranteed that there is at least one score for each student, that all scores are greater than 0, that all scores are integers, and that every set of scores ends with a one word name. You may use either printf or cout to create your output. Please do not write any include statements.

  10. (18 points) A friend comes to you with the following code which is not working. The code is supposed to take a vector that contains only 0's or 1's and move all the 1's to the front of the vector. For example:
    0 1 1 0 1 1 0 0
    
    should become
    1 1 1 1 0 0 0 0
    
    Your friend explains that the code is supposed to skip over 1's until it comes to a 0. When it comes to a 0, it searches ahead for a 1. If it finds a 1, then it swaps the 0 and the 1. It then repeats these steps until it fails to find a 1 when it searches ahead for a 1.

    Here is the code that your friend wrote (you do not have to know how BitVector's methods are implemented--it is enough to know what they do for you):

    class BitVector { 
      protected:
        vector bits;
      public:
        void swap(int x, int y) // swaps the integers at index locations x and y
        int getBit(int x);      // returns the integer at index location x
        int getSize();          // returns the size of the bits vector
    };
    
    void moveBits(BitVector &vec) {
      int i, j;
      int size = vec.getSize();
    
    1   i = 0;
    2   j = 0;
    3   while (j < size) {
    4     while (vec.getBit(i) == 1 && i < size) {
    5       i++;
    6     }
    7     j = i+1;
    8     while (vec.getBit(j) == 0 && j < size) {
    9       j++;
    10    }
    11    if (j < size)
    12      vec.swap(i, j);
    13    i = j;
    14  }
      }
    
    1. Trace the above code on the example vector and show 1) which swaps are performed, and 2) the final contents of the vector. As an example, if you write:
      swap(0, 6)
      swap(4, 7)
      
      you would be indicating that the code first swapped entries 0 and 6, and then swapped entries 4 and 7.
      Swaps                              Final Vector
                                    -------------------------------------------------
                                    |  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |
                                    -------------------------------------------------
                                    |     |     |     |     |     |     |     |     |
                                    |     |     |     |     |     |     |     |     |
                                    |     |     |     |     |     |     |     |     |
                                    -------------------------------------------------
      

    2. Based on your trace, figure out the logic error in the code and show how to fix it. You may not re-write the code whole-sale nor may you change the class declaration. The code can be fixed very simply. Tell me the line number(s) that need to be changed and what you would change them to.
      
      
      
      
      
      
      
      
      
      
      
      
      
    3. This algorithm is inefficient in that it moves most of the 1's that are in the vector. If there are n 1's in the vector, it will move roughly all n 1's. Suggest a more efficient algorithm that would not move all of the 1's. You may only use swaps to swap the 0's and 1's. Use no more than 5 sentences to describe your algorithm. Do not write code. If you think it would help, you can list the series of swaps that your algorithm would perform on the example vector.