Final exam review!

I bet the final will lean a little more toward coding and code-reading than the midterm did, but you should still spend a large chunk of your study time going over the class notes/slides, since there'll probably still be lots of "C++ trivia" type questions: true/false, multiple choice, fill in the blank, and so on.

On this page I'm providing practice problems for code reading and writing.

Important note: On the test you're going to have pencil and paper only! You should work on these problems without using your computer at first, just by writing down your answer on a white sheet of unlined paper. Once you're done, THEN use a computer and test for errors. If you don't practice without the computer helping, the test is going to be real tough!

Code reading

See the list of problems here. Each "mystery" function is a separate problem. The goal is always to understand and explain what the function does. Each function's purpose is pretty simple once you "get it", so the answers should all be 1-2 sentences.

To approach these, you should first try to solve them by hand. It can help to pick some inputs and trace through what the code would do. For example, for problem 1 you can carefully check what would happen if we input a = "abcd" and b = "abdc". Then, if you still aren't completely sure, you should write code inside int main that calls the function with several different inputs and prints out the results. This way you can quickly run experiments and try to find the pattern.

Here are the answers, split into separate files so you can check as you go without spoiling the remaining problems: 1 2 3 4 5

Code writing

General advice

Problem 1

Write a function

int sumIfDistinct(const vector<int>& v)

If all the elements of v are distinct, then your function should return the sum of all elements. If the elements are NOT all distinct, then your function should instead return 0.

Examples: Input { 1 , 3 , 8 , 2 } gives output 14. Input { 2 , 9 , 3 , 2 , 2 } gives output 0. Input { 3 , 8 , -15 , 4 } gives output 0.


Problem 2

Here is a file with a bunch of names: names.txt. These names are in random-ish order, and they're stored in the format


However, I actually want the names to be in the format


and also sorted in alphabetical order by last name. You should write a full program (NOT just a function this time!) that loads in the names from names.txt and creates a new file names_modified.txt which has the names in the desired new format.

Hint: Try breaking this into steps. For example, first you could write a program that just inputs the names and then dumps them to the output file without modifying them. You can test and debug that code by itself. Then, you can add the reformatting step, and test/debug as necessary. Finally, you can figure out how to add the sorting step.


Problem 3

Write a void function that takes a vector<int> as input and prints all the even elements from back to front.

Examples: Input { 1 , 3 , 8 , 2 } will print 2 8. Input { 2 , 9 , 3 , 2 , 2 } will print 2 2 2. Input { 2 , 9 , 4 , -60 , 2 } will print 2 -60 4 2.

Remember to use size_t instead of int as your index! (I'm choosing this problem specifically because you have to be extra careful to avoid bugs when you use size_t here.)


Problem 4

Write a Temperature class that makes the following code work:

int main()
    Temperature temp( 50 , 'F' );

    // this should print "The temperature is 50 degrees Fahrenheit."


    // this should print "The temperature is 10 degrees Celsius."

    // this should print "The temperature is 50 degrees Fahrenheit."

    Temperature temp2( 13 , 'C' );
    // this should print "The second temperature is hotter (or equal)."
    if ( firstIsLarger( temp , temp2 ) )
        cout << "The first temperature is hotter." << endl;
        cout << "The second temperature is hotter (or equal)." << endl;

    return 0;


Additional problems


I bet there'll be a "fix this code" question on your test, but I don't have debugging-specific questions for you this time around. You should make sure you remember the tricks/errors from the midterm debugging practice problems here. If you're done with those, you should work on making sure your code for the writing exercises above is clean and bug-free, since some of those questions are deliberately designed so it's easy to make certain mistakes. Also, make sure you remember the list of "gotchas" under General Advice above!