Final Exam Practice Questions¶
Vectors and Strings¶
Write a boolean function called
all_same(v), wherevis avector<string>, that returnstrueif all the elements invare the same, andfalseotherwise. Ifvis empty, then returntrue.For example:
all_same({"cat", "cat", "cat"})returnstrueall_same({"mustard"})returnstrueall_same({"cat", "cat", "dog"})returnsfalseall_same({})returnstrueWrite a
voidfunction calledconstrain(v), wherevis avector<int>, that modifies the elements ofvas 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}
Write a function
just_digits(s)that returns a string that is the same assexcept 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.
Write the definition for a C++
structthat represents an RGB triple. Give it a short and self-descriptive name.Write a boolean function that takes one RGB color
struct(that you defined in the previous question) as input, and returnstrueif it is valid, andfalseotherwise.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
trueif they are equal andfalseotherwise. If one, or both, of the passed-in colors are invalid, then your function should call theerrorfunction instead of returning a value.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.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 positiveintcalledRAND_MAX. Assume thatrand()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¶
Create a C++ enumerated type (use
enum class, not the old-styleenum) to create a new data type calledDaythat can be used like this:Day yesterday = Day::Thursday; Day today = Day::Friday; Day tomorrow = Day::Saturday;
Write a boolean function called
is_weekend(d)that returnstrueifd(which is of typeDay) is Saturday or Sunday, andfalseotherwise.Write a boolean function called
is_weekday(d)that returnstrueifd(which is of typeDay) is one of the five weekdays from Monday to Friday, andfalseotherwise.Write a function called
to_string(d)that returns a string representation ofd. For example:cout << to_string(Day::Wednesday) // prints "Wednesday" << to_string(Day::Friday); // prints "Friday"
Write a function called
next_daythat takes a singleDayobject as input and returns the day that comes after it. For example,next_day(Day::Sunday)should returnDay::Monday. Implement it using aswitchstatement.
Summing Numbers in a File¶
Suppose that the file
data.txtcontains 0, or more, doubles. Write a function that efficiently prints the sum of all the positive numbers indata.txt; negative numbers should be ignored. You can assume thatdata.txtonly contains valid double numbers.For example, suppose
data.txtcontains 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
#includestatements, and put your program entirely insidemain.
Strings and Characters¶
Write a function called
is_upper(c)that returnstrueif the charactercis one of the uppercase letters'A'to'Z', andfalseotherwise.Write a function called
to_lower(c)that returns the lowercase version ofcif it’s an uppercase letter, e.g.to_lower('E')returns'e'. Ifcis not an uppercase letter, thencis returned unchanged.Write a function called
to_lower(s)that returns a new string that is the same ass, except all lowercase letters inshave been converted to uppercase.sitself should not be changed.Write a function called
is_vowel(c)that returnstrueif the charactercis a vowel, andfalseotherwise. The vowels are a, e, i, o, and u, both uppercase and lowercase.Write a function called
first_vowel_loc(s)that returns the index location of the first vowel in strings. Ifsis empty, or has no vowels, then return -1. For examplefirst_vowel_loc("sprayer")returns 3, andfirst_vowel_loc("MBB")returns -1.Write a function called
swap_heads(s, t)that returns a single string that consists ofs, a space, and thent, except leading consonants ofsandthave been swapped. If eithers, ort, or both, start with a vowel (or are empty), then returnsandtwith a space between them.For example:
swap_heads("Donald", "Trump")returnsTronald Dumpswap_heads("Bill", "Gates")returnsGill Batesswap_heads("Emily", "Carr")returnsEmily Carr
Randomness¶
Give a small but complete program that shows how to call and use the standard
rand()function. What possible values couldrand()return?If you don’t first call the
srandfunction,rand()will always return the same sequence of numbers when you call it.- Show how to use
srandto initialize the random number generator using the current time as the seed. - Why might it sometimes be useful to give
sranda fixed value, such as 15? - Suggest some other ways to get an initial random seed value for
srand.
- Show how to use
Write a new function called
rand(n)that returns a randomintfrom 0 up to, but including, theintn. Ifnis less than 1, then throw an error usingcmpt::error.Write a new function called
rand(lo, hi)that returns a randomintthat is greater than, or equal to,lo, and less than, or equal to,hi. Iflois greater thanhi, then throw an error usingcmpt::error.Be careful with the arithmetic!