CS140 Midterm 1

Spring 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. You need to put your name on this cheat sheet and hand it in to the TA before leaving the exam.
  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 2 hours to complete the exam. Please do not discuss the exam with anyone from another lab section until after 3:00pm.
  7. Remember to pass all object parameters by reference!
  8. Good luck!

  1. (20 points--CS140Sp18-Mid1-Tail) Write a program that reads input from stdin and prints the last n lines of the input, where n is given as a command line argument. For example suppose the input is:
          The quick
          brown fox
          jumped over
          the fence
          and eluded the
          pursuing
          hounds.
        
    If n is 3, then your program would print:
          and eluded the
          pursuing
          hounds.
        
    If n is 10, then your program would print all 7 lines of input.

    Constraints

    1. You are guaranteed that there is at least one line of input
    2. You are guaranteed that n is a positive integer. If n is greater than the number of lines in the input, then print the entire input.
    3. You must use a vector to store the input. Dr. Plank's notes talked about an efficient way to store only n lines of the input at any given time. Do not worry about only storing n lines. For this particular exam problem, I recommend that you read the entire input into a vector and then print the last n lines.
    4. You must use a stringstream to extract the integer n from the command line argument. You will lose points if you try to use atoi.
    5. Your code does not have to do any error checking.

  2. (25 points--CS140Sp18-Mid1-Teams) In this problem you will write two functions to complete a program that prints out members of a team. The program reads lines of input where the first word is the team leader name and the remaining words are the names of members of the team. For example, if you have the following line of input:
          smiley ebber wolf huck jessie lady
        
    then smiley is the team leader and ebber, wolf, huck, jessie, and lady are team members. We have provided you with a main function that reads lines of input. You are to write the following two functions: