## CS140 Midterm 2

### 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.
5. The total points available for this exam (the lab coding questions plus the paper exam) is 120 points. You will be graded out of 100 however, so in effect you have 20 extra credit points to work with.
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.
7. You have the entire lab period. 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. (10 points--CS140Sp15-Mid2-Vector): Write a function named avgWeight that takes a single argument, which is a vector 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 vector is guaranteed to contain at least one user.
2. Do not print anything. The main that has been provided prints the result.

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

```name weight
```
For example:
```brad 160
nels 145
smiley 45
```

2. (20 points--CS140Sp15-Mid2-List): Write a void function named CreateList. CreateList takes three arguments. The first two arguments are an STL list L of strings and a vector V of integers containing integer indices sorted in ascending order. The third argument is an empty STL list NewL of strings. createList(L, V, NewL) will copy into newL the strings from L that correspond to the positions specified by V. For instance, if V = {1, 3, 4, 6} and L is:

{"charlie", "sherry", "morse", "clark", "gerald", "suzy", "sandy", "sarah", "joe"}

then when your function returns NewL should contain

{"sherry", "clark", "gerald", "sandy"}

which, using zero-based indexing, correspond to the elements at locations 1, 3, 4, and 6 in L.

Constraints:

1. L and V should be unchanged by the function.
2. L and V each contain at least 1 element
3. V does not contain any integers whose value exceeds the number of strings in the list (i.e., you need not worry about an integer in V referencing a non-existent element in the list).
4. Do not print anything. The main that is provided prints the list that createList populates with strings.

Test Input: You can provide test input by providing a list of strings followed by a list of integers. For example:

```charlie sherry morse clark gerald
suzy sandy sarah joe
1 3 4 6
```

3. (20 points--CS140Sp15-Mid2-Vote) Write a program that counts the ballots cast for different candidates and then prints out the candidates in alphabetical order and the votes cast for each candidate. The input will be read from stdin. Each line of input will consist of the name of a voter and the candidate for whom the voter is voting. Both the voter and candidate names will be a single word. Your program should verify that the voter has not already voted. If the voter has already voted, then you should ignore the voter's vote. If the voter has not voted, then you should set the candidate's votes to 1 if the candidate does not yet exist or else add 1 to the candidate's number of votes if the candidate does exist. Once the input is exhausted, you should print the candidates and their votes in alphabetical order by candidate. The output should be formatted as follows:
1. The name of each candidate should be left justified in a field of 20 characters
2. Then there should be one space
3. Then the number of votes should be right justified in a field of 4 characters.

Constraints:

1. You must write main and ensure that you provide all the required include statements.
2. The only data structures you may use are maps and sets. Vectors, arrays, lists, etc. are all prohibited.
3. You must use printf to print your output
Even if you get your answer correct, there will be point deductions if you fail to follow these requirements.

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

```voter candidate
```
For example:
```
```Kobe                    2