CS140 Midterm 1

Fall 2018

Instructions

  1. Use the Code Assessor development website, cs102dev.eecs.utk.edu, to answer each of the coding questions. We have provided some scaffolding code to test your solutions so you should ensure that you only provide code that the problem asks for.
  2. Leave this worksheet next to your computer for the next lab section.
  3. You may have a one page cheat sheet, with writing on front and back.
  4. The name of the coding question on Code Assessor is shown next to each question.
  5. You may not be able to finish all the problems. If so, please do not worry. We will examine all answers (even correct ones) and award partial credit. Your strategy should be to first put down something reasonable for each problem and save your work. Then if you have time, go back and compile your code and try to make it work on the test cases.
  6. You have 1:30 to complete the exam. Once you finish, you should start working on this week's lab. Please do not discuss the exam with anyone from another lab section until after 5:30pm.
  7. Remember to pass all object parameters by reference!
  8. Good luck!

  1. (40 points--CS140Fa18-Mid1-CmdLine) Write a complete program, including include statements, that outputs the average of all user-provided command line arguments that are numbers (do not print argv[0] since it is not a user-provided command line argument). For example, given the command line arguments:
    brad 10.55 30.6 nels X30
    
    your program will output:
    20.58
    
    Constraints
    1. You must use printf to output your result.
    2. You must include cstdlib in order to use printf.
    3. The output should be printed to two decimal digits in a field 6 digits wide.
    4. You must use stringstreams to convert the command line arguments to numbers. You will lose almost all of the points in this problem if you try to use atof.
    5. There will be at least one valid number so division by 0 cannot happen.

  2. (40 points--CS140Fa18-Mid1-DotProduct) In this problem you are going to write two functions--one function to read a matrix from a file and one function to compute the dot product of one row from one matrix with one column from another matrix. The dot product is the sum of the pairwise product of each pair of numbers in the row of one matrix and the column of another matrix. For example, if your two matrices are:
                         *
     3 6 8 9 1       -3  4  8 -2  2 -7
     1 5 3 4 5       -8  6 -7  1  6 -5
    *3 8 7 6 5        4  2  4  5  0  4
                     -1  6 -2  0 -3 -2
                      4 -2  6  8  3  4
    
    and the row parameter is 2 and the column parameter is 1, then the selected row in matrix1 is [3 8 7 6 5] and the selected column in matrix2 is [4 6 2 6 -2] (I have denoted the selected row and column with asterisks) and the dot product is:
    3*4 + 8*6 + 7*2 + 6*6 + 5*-2
    = 12 + 48 + 14 + 36 - 10
    = 100
    
    Here are the details on the two functions you will write:

    1. readMatrix: a void function that takes a matrix which is a 2D vector of ints and a filename which is a C++ string and reads the matrix in filename into the 2D vector of ints. The first two numbers in the file are the number of rows and columns in the matrix and the remaining numbers in the file are the numbers in the matrix. For example, the file for the first matrix shown above would be:
      3 5
      3 6 8 9 1
      1 5 3 4 5
      3 8 7 6 5
      	  
      A sample invocation of this function would be:
      vector<vector<int> > matrix;
      string filename;
      
      ... code to initialize the variables ...
      
      readMatrix(matrix, filename);
      
    2. dotProduct: takes two 2 matrices represented as 2D vectors of ints and two integers denoting a row and column respectively. It returns the integer dot product of the row from matrix1 and the column from matrix2. A sample invocation of this function would be:
      vector<vector<int> > matrix1, matrix2;
      int row, column;
      	
      ... code to initialize the variables ...
      	
      cout << dotProduct(matrix1, matrix2, row, column) << endl;
          
    Constraints:
    1. Your code does not have to do any error checking. Thus you are guaranteed that:
      • The file names exist and you have permission to read the files.
      • The number of columns in matrix1 is the same as the number of rows in matrix2 (this constraint guarantees that the two vectors you use for the pairwise product are exactly the same size).
      • The row index for matrix1 and the column index for matrix2 are valid indices.
    2. You do not need to use stringstreams to solve this problem--they will only complicate things for you!