CS140 Midterm 1 Solutions

Spring 2020


  1. (10 points)
    1. Picture.hpp
    2. #include "Picture.hpp"
    3. g++ -o Photoshop Photoshop.cpp Picture.cpp
    4.   g++ -c Photoshop.cpp
        g++ -c Picture.cpp
        g++ -o Photoshop Photoshop.o Picture.o
        
    5. ./Photoshop < bvz.pgm > edited_bvz.pgm

  2. (6 points) The problem is that the program reads its input from stdin but the user is providing the input as command line arguments. The program has stalled waiting for input from stdin. In order to get the program to finish its execution, the user needs to type some input onto the console. In this case the input would be 3 and 6.

  3. class Checkerboard {
    protected:
      vector<vector<char> > board;
      bool playerTurn;
    public:
      void initializeCheckerboard();
      void initializeCheckerboard(const Checkerboard &initialBoard);
      void printBoard() const;
      bool move(const string &move);
      int winner() const;
    };	    
    	  
  4. (12 points)
    1. 0123456789101112
      0001020301020301020

    2. It is starting to shift the entries from the left side of the vector rather than from the right side of the vector and as a result it is clobbering entries in the vector before it has a chance to move them.
    3. You need to modify fragment 2 so that it starts shifting entries from the right side of the original vector (not the resized vector--that's why my code starts shifting from originalSize-1 rather than v.size()-1):
          for (i = originalSize-1; i >= 0; i--) {
              v[i+3] = v[i];
          }
      	  
  5. (24 points)

    1.  Linear ProbingQuadratic Probing
      030 
      11212
      2  
      35858
      44848
      525 
      6 30
      7 25
      81919
      95252
      103232

    2. keylinear probing
      3210
      121
      484
      198
      528, 9
      583
      308, 9, 10, 0
      253, 4, 5
      keyquadratic probing
      3210
      121
      484
      198
      528, 9
      583
      308, 9, 1, 6
      253, 4, 7
  6. (12 points)
    1. 0x6D = 0110 1101

    2. 1001 0101 ^ 1010 1100 = 0x39
            ^ is bitwise exclusive or:
      	  
      	1001 0101
            ^ 1010 1100
              ---------
              0011 1001 = 0x39
            
    3. 1001 0101 | 1010 1100 = 0xBD
            | is bitwise or
      	  
      	1001 0101
            | 1010 1100
              ---------
              1011 1101 = 0xBD
      
      	
    4. 1010 1100 >> 3 = 0x15
          >> is the right shift operator:
      
          1010 1100 >> 3 = 0001 0101 = 0x15
      		  

  7. a: 10
    b: 90
    x: 0x1004
    y: 0x1004
          
  8. The function mystery does the following things with the input:
    1. It keeps a sum of the integers that it has encountered and each time it reads an integer it prints the integer and the sum computed thus far.
    2. It prints floating point numbers in a catch statement.
    3. It throws an exception to main if the input is a string. main() prints the exception string and then exits the program without reading any additional input.
    The output from the program is:
        
    3.6 is a double
    input = 16
    intermediate sum = 16
    14.5 is a double
    input = -5
    intermediate sum = 11
    Oops-that was not a number!
    input was not a number
    exiting program with an error
    


Coding Solutions

  1. countAndSwap
    int countAndSwap(vector<vector<string> > &names) {
       int row, col;
       int count = 0;
       string saveName;
       for (row = 0; row < names.size(); row++) {
          for (col = 0; col < row; col++) {
             if (names[row][col] == names[col][row])
                count++;
             else {
                saveName = names[row][col];
                names[row][col] = names[col][row];
                names[col][row] = saveName;
             }
          }
       }
       return count;
    }
          
  2. Vector summing program
    int main(int argc, char *argv[]) {
       ifstream inputfile;
       string command;
       vector<double> accumulator;
       int i;
       double num;
       int index;
    
       try {
          if (argc != 2) {
             throw (string) "usage: ./sum filename";
          }
          inputfile.open(argv[1]);
          while (inputfile >> command) {
             if (command == "ADD") {
                inputfile >> index;
                inputfile >> num;
                if (index >= accumulator.size()) {
                   accumulator.resize(index+1, 0);
                }
                accumulator.at(index) += num;
             }
             else if (command == "PRINT") {
                printf("Vector Contents\n");
                for (i = 0; i < accumulator.size(); i++) {
                   if (accumulator.at(i) == 0)
                      printf("%3d: Empty\n", i);
                   else
                      printf("%3d: %6.2f\n", i, accumulator.at(i));
                }
             }
          }
          inputfile.close();
       }
       catch (const string &msg) {
          printf("%s\n", msg.c_str());
       }
    }