CS140 Midterm 1

Fall 2018

Instructions

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

  2. This paper exam has 10 problems worth 120 points and an extra credit problem worth 12 points. The coding exam had 2 problems worth 80 points, so the total exam is worth 200 points but you can score as high as 212 points. If you score 150/200, then your score will be 75%.

  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. No electronic devices are allowed and all electronic devices that you have with you must be turned off, including calculators.

  6. You may not use any notes for this exam.

  7. Good luck!

  1. (15 points) Declare a class named PgmImage that you could place in a .h file. Do not write the method definitions as these would not go in a .h file. PgmImage should have the following variables and methods:

    1. picture: a 2D vector of ints
    2. photographer: a string containing the name of the photographer who took this picture.
    3. printPicture: a void method that takes a string parameter named filename and writes the picture to that file.
    4. getPhotographer: a method that takes no parameters and that returns the photographer's name.
    5. crop: a void method that crops the picture. This method takes the following parameters:
      • croppedImage: a 2D vector of ints. The cropped image will be placed in this 2D vector
      • row, col, numRows, numCols: 4 ints specifying the cropped rectangle. The cropped rectangle starts at (row, col) in the picture and includes numRows rows and numCols columns.
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
        
      

  2. (12 points) Suppose I want to declare a class named PgmImage and use it in a program named ImageManipulator.cpp. I also want to implement the methods for PgmImage in a separate file from ImageManipulator.cpp. Answer the following questions:

    1. If the class declaration for PgmImage is placed in a file named PgmImage.h, then what statement would you write to include this header file in .cpp files?
      
      
      
    2. In what file should the method definitions for PgmImage be placed? Give the name of the file, following the naming conventions discussed in class.
      
      
      
      
    3. Using a single g++ statement, how could I compile all of these files into an executable named ImageManipulator?
      
      
      
    4. Suppose instead that I wanted to separately compile these files and then link together their object files into an executable named ImageManipulator. Write the set of statements that would be required to perform this separate compilation and then linking into the executable.
      
      
      
      
      
    5. Suppose that I execute ImageManipulator from the command line by writing the following command:
      ./ImageManipulator < bvz.pgm
            
      What does the < operator cause the C++ program to do?
      
      
      
      
      
            

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

  4. (10 points) Answer the following two questions about typedefs:

    1. Write a typedef statement that declares a new type named Pixels to be a vector of ints.
      
      
      
      
      
      
      
      	    
    2. Declare picture to be a vector of Pixels.
      
      
      
      
      
      
      	    
  5. (24 points)

    1. Insert the keys 22, 28, 39, 48, 41, and 61 into the following two hash tables using a) separate chaining, and b) quadratic probing. For separate chaining, use commas to separate the keys that would go into an entry. For example, if the keys 22, 28, and 39 all went into the same entry, you would write (22, 28, 39) in that entry. You should use the hash function:
      h(key) = key % 13
      
       Separate ChainingQuadratic Probing
      0  
      1  
      2  
      3  
      4  
      5  
      6  
      7  
      8  
      9  
      10  
      11  
      12  

    2. For quadratic probing please also complete the following table showing the comma, separated list of entries you would probe in order to insert each key (e.g., if you examined entries 6, 7, and 11 in order to insert 48, then you would write (6, 7, 11) next to 48 in the below table--this is only an example--it is not the actual set of entries you will probe for 48). This will allow us to assign you partial credit if you do not get the entries correct in the quadratic probing hash table.

      keyquadratic probing
      22 
      28 
      39 
      48 
      41 
      61 

  6. (12 points) The intent of the following code fragment is to read a series of name/salary pairs from stdin and to print the average salary to stdout.
    1   string name;
    2   int salary;
    3   int salarySum = 0;
    
    4   while (!cin.eof()) {
    5       cin >> name >> salary;
    6       salarySum += salary;
    7   }
    8   cout << "total salary = " << salarySum << endl;   
    							
    1. Show what the code fragment prints if the input is:
      	  Suzy 100
      	  Frank 80
      	  Ebber 50
      	
      
      
      
        
    2. What is wrong with the code that causes the output to be incorrect? You may use no more than four sentences. A sample explanation might be:
      The code fragment is incorrect because it is missing a read statement before the beginning of the while loop and hence the loop variable is uninitialized when the while statement is reached. This could cause the while loop to never execute. (this is an example explanation that does not correspond to the code fragments below!).
      
      
      							
      
      
      
      
      
       
    3. Modify the incorrect portion of the code to make it correct. Please do not re-gurgitate the entire code. Only modify the incorrect portion.
      
      
      
      
      
      
      
      
      
        
        

  7. (8 points) The following code fragment is supposed to read names into a vector.
    1    vector<string> names;
    2    int i = 0;
    3    string name;
    
    4    while (cin >> name) {
    5	names[i] = name;
    6       i++;				   
    7    }
    				   
    1. What is wrong with the code? (you may use no more than 4 sentences)
      
      
      
      
      
      
      
      
      
      
    2. Change the incorrect portion of the code to make it correct. Do not regurgitate the entire code. Only modify the incorrect code fragment.
      
      
      
      
      
      
      
      
      
      
        
      
  8. (12 points) Please read the following instructions before attempting this problem:

    1. 0xB5 = ____________________________
      
            
    2. (0101 1101) ^ (1110 1100) = _____________________________
      
      
      
      
      
      
            
    3. (0010 0101) | (1010 1100) = _____________________________
      
      
      
      
      
      
            
    4. (1010 1100) << 3 = _______________________________
      
      
      
      
      
      
      		  

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

  10. (14 points) Suppose you are given the following variable declarations:
    string runner;
    int minutes, seconds;
    
    Write a printf statement that:

    1. outputs runner in a left-justified field of 15 characters, and time in the following two formats: a) as a right-justified floating point number in a field that is minimally 6 characters wide with 1 decimal digit where the whole number is the minutes and the fractional number is the seconds divided by 60, and b) as a right-justified string in the format mm:ss (i.e., both minutes and seconds should be in fields that are 2 characters wide).
    2. If the number of seconds is less than 10, pads the seconds output with a single 0, and
    3. Puts a single space between each of the three fields (but no space at the end of the output).
    For example, if the runner's name is "Bradley", minutes is 5, and seconds is 40, then the output would be:
    Bradley            5.7  5:40
    
    (40/60 = .67 which when rounded to one digit is .7) and if seconds were instead 4, then the output would be:
    Bradley            5.1  5:04
    
    
    
    
    
    
    
    

  11. (Extra Credit-12 points) You have written a program that reads the exam scores for a student and for each student prints the student's name and exam average. Your program reads lines of input where the first and second words on the line are a student's first and last name and the remaining portion of the line are scores for the student. For example:
    Donald Duck 20 40 50
        
    All exam scores must be integers between 0 and 100 and every student must have at least one exam score. A line of input must therefore have at least 3 inputs--a firstname, lastname, and test score.

    When you test your program, you need to come up with a series of test cases for correct input, incorrect input, and boundary cases. List all the test cases you should provide in order to thoroughly test your program (this is what you are supposed to do in each of your labs). I will get you started with a couple of example test cases:

    Non-erroneous input: This will include all "normal" data plus all boundary cases:

    1. Multiple lines of data that have a first name, last name, and several exam scores between 0 and 100.
    2. A single line of data that has a first name, last name, and several exam scores between 0 and 100 (this is called a boundary case because it contains the minimum possible number of lines of input)
    Hint: The remaining test cases all have to do with boundary cases involving test scores. I found 3 such cases but feel free to list more if you think of more cases.
    
    
    
    
    
    
    
    
    
    
            
    Erroneous input: A test case with:
    1. A line that has fewer than 3 inputs and is therefore missing either a name or an exam score.

    Hint: The remaining test cases all have to do with erroneous exam scores as it is not really possible to test for erroneous names. I found three such cases but feel free to list more if you think of more cases.