1. 0x1d78 + 0xb6 = 0x1e2e

    You can get this result as follows:

     1d78     8+6 = 14 = e with no carry
    +00b6     7+b = 7+11 = 18 = 2 with a carry of 1
              d+0+carry of 1 = 13 + 1 = 14 = e
    -----
     1e2e
    
  2. Straight from the procedures notes: You want the convenience of using a procedure, but you don't want to make copies, because that is expensive.

    Here is a slightly wordier explanation: A reference parameter only copies a pointer to an object, which is only 4-8 bytes, whereas a value parameter would copy the contents of the entire object. If the object is quite big, such as a lengthy string or a vector with a large number of elements, then copying the contents of the entire object could slow down the program considerably.

  3. keylinear probingquadratic probing
    2200
    2866
    176, 76, 7
    4844
    396, 7, 86, 7, 10
    627, 8, 97, 8

    Linear Probing:

    1. 48 hashes to location 4, which is empty.
    2. 39 hashes to location 6, which is full. We start a sequential search from location 6 to find an empty entry. The first empty entry is 8, so we probe entries 6, 7, and 8.
    3. 62 hashes to location 7, which is full. We start a sequential search from location 7. Location 8 has become occupied by 39, so the first empty entry is location 9. Hence we probe entries 7, 8, and 9.

    Quadratic Probing:

    1. 48 hashes to location 4, which is empty.
    2. 39 hashes to location 6 which is full. We next query the following locations:
      1. 7: hash(39) = (6 + 12) % 11 = 7
      2. 10: hash(39) = (6 + 22) % 11 = 10 & 11 = 10
    3. 62 hashes to location 7 which is full. We next query the following locations:
      1. 8: hash(62) = (7 + 12) % 11 = 8
                    Linear Probing                     Quadratic Probing
               -----------------------              -----------------------
          0    | 22                  |              | 22                  |
               -----------------------              -----------------------
          1	   |                     |              |                     |
               -----------------------              -----------------------
          2    |                     |              |                     |
               -----------------------              -----------------------
          3	   |                     |              |                     |
               -----------------------              -----------------------
          4	   |                     |              |                     |
               -----------------------              -----------------------
          5	   |                     |              |                     |
               -----------------------              -----------------------
          6	   | 28                  |              | 28                  |
               -----------------------              -----------------------
          7	   | 17                  |              | 17                  |
               -----------------------              -----------------------
          8	   | 39                  |              | 62                  |
               -----------------------              -----------------------
          9	   | 62                  |              |                     |
               -----------------------              -----------------------
          10   |                     |              | 39                  |
               -----------------------              -----------------------
    
  4. iterationh
    0f9
    1a5
    You can trace through the code as follows:
    1. h = 19: 19 has the bit representation 00010011
    2. When i = 0, we are looking at name[0], which is 'a'. 'a' has the hexadecimal code 0x61 which is 0110 0001. So iteration 0 becomes:
      h = (h << 3) ^ 0110 0001      # I am separating the bits into groups of 4 for clarity
      
      1. h << 3: 0001 0011 becomes 1001 1000 because the left most three bits get shifted into the "bit bucket" and 3 0's get shifted into the right most three locations.
      2. 1001 1000 ^ 0110 0001:
          1001 1000
        ^ 0110 0001
          ---------
          1111 1001 = f9  (the answer to iteration 0)
             
    3. Starting with iteration 1, i is 1 and name[1] is 'm', which has the hexadecimal code '0x6d'. '0x6d' has the bit representation 0110 1101. So iteration 1 becomes:
      h = (h << 3) ^ 0110 1101
      
      1. h << 3: 1111 1001 becomes 1100 1000 because the left most three bits get shifted into the "bit bucket" and 3 0's get shifted into the right most three locations.
      2. 1100 1000 ^ 0110 1101:
          1100 1000
        ^ 0110 1101
          ---------
          1010 0101 = a5  (the answer to iteration 1)
        
    See the hashing and bit notes if you are having difficulty with bit operations.

  5. g: Counting 6 characters from a gives you:
    1 2 3 4 5 6
    b c d e f g
    
    What is really happening is that 6 is being added to the ascii code for 'a', which is 97. The sum is 103, which is the ascii code for 'g'.
  6. int dash = licensePlate.find('-');
    state = licensePlate.substr(0, dash);
    count = licensePlate.substr(dash+1);
    
    You could also use a for loop to iterate through licensePlate until you find the dash. However, this is less efficient and less elegant than using the find method.

  7. printf("%-20s %10.2f %4d", name.c_str(), salary, age);
    
  8. Your code needs to perform 3 steps:
    1. Resize the vector to accommodate the additional pad digits.
    2. Move the original contents of the vector pad entries to the right. You should do this by starting from the back of the original vector. If you start from the beginning of the original vector, then you will clobber some of the original numbers in the vector.
    3. Fill the first pad entries with value.
    void pad_left(vector &vec, int pad, int value) {
       int i;
       int oldSize = vec.size();
    
       // resize the vector to accommodate the additional pad digits
       vec.resize(oldSize + pad);
    
       // shift the original numbers to the right by pad entries
       for (i = oldSize-1; i >= 0; i--)
          vec[i+pad] = vec[i];
    
       // fill the leftmost pad entries with value
       for (i = 0; i < pad; i++)
          vec[i] = value;
    }
    
  9. The overall strategy is to read the file entries one at a time and determine whether they are a score or a name. If the entry is a score then check whether it is greater than the current maximum for the current student. If the entry is a name, then output the student's name and maximum score. When you encounter the name, you must also ensure that you reset the variable that is keeping track of the maximum score. Since all scores are guaranteed to be greater than 0, you can reset max to -1 and that guarantees that the first score you consider for the next student will become the student's max.

    My approach was to read a score and assume it's a score (i.e., an integer). If the read fails, then we know we have either reached eof, in which case we need to quit, or that we have a student name, in which case we need to clear the cin stream and re-read the name as a string.

    int main() {
      int max = -1;
      int score;
      string name;
      while (true) {
        // if the next entry is an integer, then the read into score succeeds.
        // check to see if the new score is a max for the student
        if (cin >> score) {  
          if (score > max)
    	max = score;
        }
    
        // if the read fails, it could be because we're at eof, in which case
        // we need to get out of the loop and exit the program
        else if (cin.eof())
          break;
    
        // otherwise we're reading a name. We must first clear the cin stream
        // and then re-read the name. 
        else {
          cin.clear();
          cin >> name;
          cout << name << " " << max << endl;
          max = -1;
        }
      }
    }
    
    1. The final vector is 1 1 1 0 1 0 0 0. The swaps performed are:
      swap(0, 1)
      swap(1, 2)
      swap(2, 4)
      swap(4, 5)
      
    2. The problem is line 13. It should be i++, since you just want to repeat your search by starting at the next entry after the swapped entry.
    3. There are a couple approaches:

      1. Skip over 1's until it comes to a 0. When it comes to a 0, search from the back of the vector for a 1, and swap the 0 and the 1. Repeat these steps, but start the backward search from where you found the last 1. Stop when your index moving from the front of the vector crosses the index moving from the back of the vector.

      2. Make a "pre-pass" through the vector that counts the number of 1's in the vector. Suppose there are n 1's in the vector. Then only 1's starting at index location n or later need to move. Hence the algorithm becomes skip over 1's until it comes to a 0. When it comes to a 0, start the search for a 1 at location n. Repeat these steps until you reach location n and for each step pick up the search for a 1 from the location where it found a 1 on the last step.