Mixed-Up Code Exercises¶
Answer the following Mixed-Up Code questions to assess what you have learned in this chapter.
Construct a block of code that changes the first element of vec
to a 6,
multiplies the third element of vec
by 2, and increments the last element
of vec
by 1 (in that order). This should work no matter what vec
is.
Construct a block of code that creates a vector called digs
whose elements are
7, 8, 7, 8. Then access elements to change the digs
to contain the elements
7, 4, 7, 4. Important: Change the 8’s to 4’s in order of
increasing index.
Construct a block of code that creates a vector called nums
whose elements are five 1’s.
Then make a copy of this vector called digits
, and use vector operations to change
digits to {1, 2, 3}
.
Construct a block of code that loops over a vector called numbers
and transforms the vector so each element is doubled.
Suppose you have the vector vector<string> words = {"car", "cat", "switch", "princess"}
Construct a block of code that transforms the vector to
vector<string> words = {"cAr", "cAt", "switch", "mArio"}
Suppose you run Club Keno, and you are in charge of picking the 20
random numbered balls between 1 and 80. Construct a block of code that
chooses these random numbers, then saves them to a vector called keno
.
Suppose <code>album</code> has already been defined as
vector<string> album = {"imagine", "needy", "NASA", "bloodline", "fake smile", "bad idea", "make up", "ghostin", "in my head", "7 rings", "thank u, next", "break up with your girlfriend, i'm bored"}
Construct a block of code that counts how many songs in <code>album</code> start with b.
Suppose you have the following two vectors to describe the weekly forecast
vector<double> temps = {82.0, 76.8, 74.3, 58.8, 79.2, 73.4, 80.1}
vector<double> precip = {0.00, 0.30, 0.60, 0.90, 0.10, 0.20, 0.80}
Your family will go to the beach if the temperature at least 75 degrees and the chance
of precipitation is less than 50%. Construct a block of code that counts how many days
your family can hit the beach on your vacation.
Suppose you have the following vector nouns
,
vector<string> nouns = {"cereal", "Cocoa Puffs", "Mario", "luigi", "Aerosmith"}
Construct a block of code that creates a vector of the proper nouns in nouns
.
Use the isupper
function to check if a letter is uppercase.
Suppose you have the following function howMany
and vector exclamations
Construct a block of code that counts how many times “.”, “!”, and “?” occur in exclamations
.
Save the counts to a vector with “.” count as the first element, “!” count as the second, and “?” count as the third.
Put the necessary blocks of code in the correct order.
int howMany (const vector<string>& vec, char let) {
int count = 0;
for (size_t i = 0; i < vec.size(); i++) {
for (size_t c = 0; c < vec[i].size(); c++) {
if (vec[i][c] == let) {
count++;
}
}
}
return count;
}
vector<string> excl = {"what?!", "how???", "fine!", "STOP.", "yay!!!!!", "ugh...!"};