CS140 Midterm 1

Spring 2016

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 hour 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 3:00pm.
  7. Remember to pass all object parameters by reference!
  8. Good luck!

  1. (15 points--CS140Fa16-Mid1-Moonglow) Moonglow continues to experiment with grade files in an effort to save paper. His grade files now include a single line for each student. Each line starts with a one word student name and then all of the student's scores. For example:
    BabyDaisy 89 75 93 53 76
    Smiley 86 46 100
    Nancy 97
    
    Write a complete program that reads student scores from stdin, and that outputs the student's name and the student's average score. For example, for the above file your program should output:
    BabyDaisy             77.20
    Smiley                77.33
    Nancy                 97.00
    
    Constraints
    1. You are guaranteed that there is at least one score for each student and that all scores are integers.
    2. You must use printf to print your output. The output should be formatted as follows:
      • The name should be left justified in a field of size 20
      • There should be one space between the name and the average score.
      • The average score should be right justified in a field six spaces wide with 2 digits of precision.

  2. (15 points--CS140Fa16-Mid1-VoteCount) Write an int function named countBallots that takes a two dimensional vector of ints (i.e., a vector of vector of ints) denoting a vector of ballots and a one dimentional vector of ints representing candidates. Each row of the 2D vector represents a voter's ballot. The voter has ranked the candidates from 1-n where n is the number of candidates, and 1 indicates the voter's most preferred candidate and n represents the voter's least preferred candidate. For example:
    4 3 1 2 5
        
    indicates that candidate 2 (using 0-based indexing) is the voter's preferred candidate. For each candidate, your function should count the number of ballots for which that candidate is the preferred candidate and assign the tallies to the candidates vector. Your function should return the number of the winning candidate. If there is a tie, return the candidate with the lowest index. When the function returns, the calling function should be able to examine the tallies in the candidates vector.

    As an example, suppose the 2D candidates vector is:

    	3 1 2
    	1 2 3
    	3 2 1
    	1 2 3
    	2 3 1
    
    Then the candidates vector on return would be:
      2
      1
      2
    
    and the winning candidate would be 0, because there is a tie and 0 is the lower index.

    Constraints:

    1. There will be at least one row
    2. There will be exactly one 1 in each row
    3. Your code does not have to do any error checking.