Final Exam Practice Questions

Sample solutions.

Vectors and Strings

  1. Write a boolean function called all_same(v), where v is a vector<string>, that returns true if all the elements in v are the same, and false otherwise. If v is empty, then return true.

    For example:

    all_same({"cat", "cat", "cat"}) returns true

    all_same({"mustard"}) returns true

    all_same({"cat", "cat", "dog"}) returns false

    all_same({}) returns true

  2. Write a void function called constrain(v), where v is a vector<int>, that modifies the elements of v as follows:

    • If the element is less than 0, replace it with 0.
    • If the element is greater than 100, replace it with 100.
    • Otherwise, don’t change the element.

    For example:

    vector<int> a = {6, -10, 0, 100, 44, 101};
    constrain(a);
    // now a == {6, 0, 0, 100, 44, 100}
    
  3. Write a function just_digits(s) that returns a string that is the same as s except all non-digit characters have been removed. For example:

    • just_digits("Route 7.2, highway56") returns "7256"
    • just_digits(" moose") returns ""

RGB Structures

RGB color is a popular way to represent colors. In RGB, a color is represented as a mixture of red (r), green (g), and blue (b). Each RGB color has the form (r, g, b), where r, g, and b are integers. We will refer to (r, g, b) as an RGB triple, or just a triple for short.

For example, the triple (255, 140, 0) is dark orange, and (147, 112, 219) is medium purple.

A triple (r, g, b) is valid if each of r, g, and b is greater than, or equal to 0, and less than, or equal to 255. Otherwise, the triple is invalid. So, for example, (65, 180, 102) is a valid RGB triple, but (200, 180, 256) is invalid.

  1. Write the definition for a C++ struct that represents an RGB triple. Give it a short and self-descriptive name.

  2. Write a boolean function that takes one RGB color struct (that you defined in the previous question) as input, and returns true if it is valid, and false otherwise.

  3. The RGB triples (a, b, c) and (r, s, t) are equal if a equals c, b equals s, and c equals t. Write a boolean function that takes two RGB color structs as input and returns true if they are equal and false otherwise. If one, or both, of the passed-in colors are invalid, then your function should call the error function instead of returning a value.

  4. The perceived brightness of the (r, g, b) triple can be calculated with this formula:

    brightness = (299 * r + 587 * g + 114 * b) / 1000
    

    Write a function that returns, as a double, the perceived brightness of the an RGB color struct.

  5. Recall that the standard function rand() returns a random integer that is greater than, or equal to 0, and less than, or equal to, some very large positive int called RAND_MAX. Assume that rand() has been included in your code so that it is available to be used.

    Write a function that takes no inputs and uses rand() to return a randomly chosen RGB color struct. For example, you could use it like this:

    RGBcolor fill = rand_color();   // fill is set to some random RGB color
    RGBcolor stroke = rand_color(); // stroke is set to some random RGB color
    

    All RGB colors should have the same chance of being returned, and an invalid color should never be returned.

Enumerated Types: Days of the Week

  1. Create a C++ enumerated type (use enum class, not the old-style enum) to create a new data type called Day that can be used like this:

    Day yesterday = Day::Thursday;
    Day today = Day::Friday;
    Day tomorrow = Day::Saturday;
    
  2. Write a boolean function called is_weekend(d) that returns true if d (which is of type Day) is Saturday or Sunday, and false otherwise.

  3. Write a boolean function called is_weekday(d) that returns true if d (which is of type Day) is one of the five weekdays from Monday to Friday, and false otherwise.

  4. Write a function called to_string(d) that returns a string representation of d. For example:

    cout << to_string(Day::Wednesday)  // prints "Wednesday"
         << to_string(Day::Friday);    // prints "Friday"
    
  5. Write a function called next_day that takes a single Day object as input and returns the day that comes after it. For example, next_day(Day::Sunday) should return Day::Monday. Implement it using a switch statement.

Summing Numbers in a File

  1. Suppose that the file data.txt contains 0, or more, doubles. Write a function that efficiently prints the sum of all the positive numbers in data.txt; negative numbers should be ignored. You can assume that data.txt only contains valid double numbers.

    For example, suppose data.txt contains these numbers:

    -8.1
    2.2
    1.0
    0.0
    -233.22
    -92.2201
    

Then your program should print 3.2.

Your program should read and process files in the same style as discussed in the lectures and textbook (i.e. C++-style file handling).

Write the necessary #include statements, and put your program entirely inside main.

Strings and Characters

  1. Write a function called is_upper(c) that returns true if the character c is one of the uppercase letters 'A' to 'Z', and false otherwise.

  2. Write a function called to_lower(c) that returns the lowercase version of c if it’s an uppercase letter, e.g. to_lower('E') returns 'e'. If c is not an uppercase letter, then c is returned unchanged.

  3. Write a function called to_lower(s) that returns a new string that is the same as s, except all lowercase letters in s have been converted to uppercase. s itself should not be changed.

  4. Write a function called is_vowel(c) that returns true if the character c is a vowel, and false otherwise. The vowels are a, e, i, o, and u, both uppercase and lowercase.

  5. Write a function called first_vowel_loc(s) that returns the index location of the first vowel in string s. If s is empty, or has no vowels, then return -1. For example first_vowel_loc("sprayer") returns 3, and first_vowel_loc("MBB") returns -1.

  6. Write a function called swap_heads(s, t) that returns a single string that consists of s, a space, and then t, except leading consonants of s and t have been swapped. If either s, or t, or both, start with a vowel (or are empty), then return s and t with a space between them.

    For example:

    • swap_heads("Donald", "Trump") returns Tronald Dump
    • swap_heads("Bill", "Gates") returns Gill Bates
    • swap_heads("Emily", "Carr") returns Emily Carr

Randomness

  1. Give a small but complete program that shows how to call and use the standard rand() function. What possible values could rand() return?

  2. If you don’t first call the srand function, rand() will always return the same sequence of numbers when you call it.

    1. Show how to use srand to initialize the random number generator using the current time as the seed.
    2. Why might it sometimes be useful to give srand a fixed value, such as 15?
    3. Suggest some other ways to get an initial random seed value for srand.
  3. Write a new function called rand(n) that returns a random int from 0 up to, but including, the int n. If n is less than 1, then throw an error using cmpt::error.

  4. Write a new function called rand(lo, hi) that returns a random int that is greater than, or equal to, lo, and less than, or equal to, hi. If lo is greater than hi, then throw an error using cmpt::error.

    Be careful with the arithmetic!