Final Exam Practice Questions¶
Vectors and Strings¶
Write a boolean function called
all_same(v)
, wherev
is avector<string>
, that returnstrue
if all the elements inv
are the same, andfalse
otherwise. Ifv
is empty, then returntrue
.For example:
all_same({"cat", "cat", "cat"})
returnstrue
all_same({"mustard"})
returnstrue
all_same({"cat", "cat", "dog"})
returnsfalse
all_same({})
returnstrue
Write a
void
function calledconstrain(v)
, wherev
is avector<int>
, that modifies the elements ofv
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}
Write a function
just_digits(s)
that returns a string that is the same ass
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.
Write the definition for a C++
struct
that 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 returnstrue
if it is valid, andfalse
otherwise.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 andfalse
otherwise. If one, or both, of the passed-in colors are invalid, then your function should call theerror
function 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 positiveint
calledRAND_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 calledDay
that 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 returnstrue
ifd
(which is of typeDay
) is Saturday or Sunday, andfalse
otherwise.Write a boolean function called
is_weekday(d)
that returnstrue
ifd
(which is of typeDay
) is one of the five weekdays from Monday to Friday, andfalse
otherwise.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_day
that takes a singleDay
object as input and returns the day that comes after it. For example,next_day(Day::Sunday)
should returnDay::Monday
. Implement it using aswitch
statement.
Summing Numbers in a File¶
Suppose that the file
data.txt
contains 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.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 insidemain
.
Strings and Characters¶
Write a function called
is_upper(c)
that returnstrue
if the characterc
is one of the uppercase letters'A'
to'Z'
, andfalse
otherwise.Write a function called
to_lower(c)
that returns the lowercase version ofc
if it’s an uppercase letter, e.g.to_lower('E')
returns'e'
. Ifc
is not an uppercase letter, thenc
is returned unchanged.Write a function called
to_lower(s)
that returns a new string that is the same ass
, except all lowercase letters ins
have been converted to uppercase.s
itself should not be changed.Write a function called
is_vowel(c)
that returnstrue
if the characterc
is a vowel, andfalse
otherwise. 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
. Ifs
is 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 ofs
andt
have been swapped. If eithers
, ort
, or both, start with a vowel (or are empty), then returns
andt
with a space between them.For example:
swap_heads("Donald", "Trump")
returnsTronald Dump
swap_heads("Bill", "Gates")
returnsGill Bates
swap_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
srand
function,rand()
will always return the same sequence of numbers when you call it.- Show how to use
srand
to initialize the random number generator using the current time as the seed. - Why might it sometimes be useful to give
srand
a 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 randomint
from 0 up to, but including, theint
n
. Ifn
is less than 1, then throw an error usingcmpt::error
.Write a new function called
rand(lo, hi)
that returns a randomint
that is greater than, or equal to,lo
, and less than, or equal to,hi
. Iflo
is greater thanhi
, then throw an error usingcmpt::error
.Be careful with the arithmetic!