CS140 Midterm 2

Spring 2018

Instructions

  1. Use the Code Assessor development website, cs102dev.eecs.utk.edu, to answer each of the coding questions.
  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. Both questions are in the CS140 category.
  5. The coding portion of this exam is worth 60 points and the paper exam is worth 90 points for a total of 150 points.
  6. 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. Make sure you budget your time so that you have time to write a complete solution to both problems. You can still obtain almost 100% credit even if your program is not running, so if you think your program is mostly correct but it is not running, move on to the other problem.
  7. You have the whole lab period for this exam. You may leave when you are finished. Please do not discuss the exam with anyone from another lab section until after 2:30pm.
  8. Remember to pass all object parameters by reference!
  9. Good luck!

  1. (30 points--CS140Sp18-Mid2-List): Write a function named avgWeight that takes a single argument, which is an STL list of pointers to User objects, and returns their average weight as a double. You are given the following class declaration for User:
    class User {
    public:
      string name;
      int weight;
    };
    
    Constraints:
    1. The list is guaranteed to contain at least one user.
    2. Do not print anything. The main that has been provided prints the result.
    3. Do not read anything. The main that has been provided reads all the input and sets up the list for you.

    Test Input: You can provide test input by creating lines of the form

    name weight
    
    For example:
    brad 160
    nels 145
    smiley 45
    

  2. (30 points--CS140Sp18-Mid2-Contest): Write a C++ program to score af contest. Standard input is composed of lines of text, where each line contains a person's first name and the name of a problem they have correctly solved. A person gets credit for solving the problem only if they are the first person to have solved the problem. The winner is the person or persons who correctly solves the most problems.

    Your job is to write a program that prints out the name of the winning person(s) and the number of problems they were credited with solving. Put a single space between the winner's name and the number of problems they solved. If there is a tie, then print the names of all of the winning persons in alphabetical order with each person printed on a new line. The only data structures you are allowed to use are sets and maps from the C++ Standard Template library. While your solution will have to keep track of which problems have been previously solved, it does not have to keep track of which person solved the problem since the problem description does not ask you to print the problems that the winner(s) solved.

    Here are two examples. If the sample input is:

    	  Kyle    SRM-394
    	  Banajee SRM-405
    	  Sue     SRM-591
    	  Nancy   P-486
    	  Joe     SRM-394
    	  Sue     SRM-394
    	  Lee     B-4839
    	  Sue     P-486
    	  Lee     SRM-394
    	  Lee     SRM-405
    	  Sue     P-383
    
    Then your program would print:
        Sue 2
      
    since Sue was the first person to solve problems SRM-591 and P-383. Notice that Sue does not get credit for either SRM-394 or P-486 because someone solved those problems before her.

    If the input included the additional line:

          Lee  SRM-001
        
    then your program would print:
          Lee 2
          Sue 2
        

    Constraints:

    1. The input is guaranteed to contain at least one line and is error free.
    2. You must use STL maps and sets to solve this problem.
    3. You cannot assume that every problem will include a number. The type of a problem is a string, not an integer.
    Handy Methods From The STL's Set API

    Handy Methods From The STL's Map API