CS140 Midterm 1

Spring 2020

Instructions

  1. Go to the Modules section on Canvas, find the Midterm 1 Coding link, and click on it to start the exam. Once you start the exam you may not go to any other web-site. Doing so is considered cheating and if the TAs catch you doing it you will receive a 0 for this portion of the exam and the TAs will inform me so that I can decide if further action is warranted.
  2. The only window that may be open is your browser window. All other applications, including the terminal window, must be closed. I would maximize the size of your browser window so that it covers the entire screen.
  3. Type the answer to the two coding questions in the space provided and nicely format your code using indenting.
  4. When you are finished give this exam worksheet to the TA and start working on your lab (do not take this exam worksheet with you!).
  5. You may have a one page cheat sheet with writing on front and back. You need to put your name on this cheat sheet and hand it in to the TA when you complete the exam.
  6. You may not be able to finish all the problems. If so, please do not worry. We will 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 try to flesh out your answer or correct any syntax issues.
  7. You have 1 hour to complete the exam. Please do not discuss the exam with anyone from another lab section until after 3:00pm.
  8. Remember to pass all object parameters by reference and when they are not being modified, to also declare them as const!
  9. Good luck!

  1. (20 points) Write an integer function named countAndSwap that takes a 2D vector of strings called names. names is a square vector, meaning that it has the same number of rows and columns. countAndSwap counts and returns the number of symmetric entries. If you think of a 2D vector as a table, then a symmetric entry is one where the two entries that face one another across the table's diagonal have the same name. More technically, names[row][col] and names[col][row] are symmetric if they have the same name. If two entries are not symmetric, then countAndSwap swaps the names in the two entries. This swap should be reflected in the 2D vector associated with the function that calls countAndSwap. As an example, countAndSwap will transform the 2D vector shown below on the left to the 2D vector shown below on the right (the numbers are row and column indices and are not part of the vector). The three pairs of entries {[0,2], [2,0]}, {[1,2], [2,1]}, and {[2,3], [3,2]} have been swapped because they are not symmetric. countAndSwap would return 3 for the 2D vector on the left because the entries {[0,1], [1,0]}, {[0,3], [3,0]}, and {[1,3], [3,1]} are symmetric.
    0123
    0---lynxtigercat
    1lynx---doglion
    2bearlion---bear
    3catliondog---
    ===>
    0123
    1---lynxbearcat
    2lynx---lionlion
    3tigerdog---dog
    4catlionbear---
    Hint: Your code should only traverse the upper half of the 2D vector above the diagonal or the lower half of the 2D vector below the diagonal. If you traverse the entire 2D vector, you will double count the symmetric entries and perform 2 swaps on the asymmetric entries, thus leaving the asymmetric entries unchanged.

  2. (30 points) Write a program that reads commands from a file and performs the appropriate action. The filename will be provided as a command line argument. Your program will be using a vector where each entry keeps a sum of floating point numbers. There are two commands your program must process: