numbers = {1, 2, 3, 4, 5};
{{endgroup}}
{{group}}
for (size_t i = 0; i < numbers.size(); i++) {
{{endgroup}}
{{group}}
numbers[i] = numbers[i] * 2;
{{endgroup}}
{{group}}
}
{{endgroup}}
{{distractor}}
{{group}}
vector numbers = {1, 2, 3, 4, 5}; #distractor
{{endgroup}}
{{distractor}}
{{group}}
for (size_t i = 1; i <= numbers.size(); ++i) { #distractor
{{endgroup}}
{{distractor}}
{{group}}
for (int i = 0; i < numbers.size(); i++) { #distractor
{{endgroup}}
{{distractor}}
{{group}}
numbers[i] * 2; #distractor
{{endgroup}}
.. tb-parsons::
:name: vectors_p5
:no-indent:
Suppose you have the vector
vector words = {"car", "cat", "switch", "princess"};
Construct a block of code that transforms the vector to
vector words = {"c_ar", "c_at", "switch", "m_ario"}
.. code-block:: cpp
{{group}}
words.pop_back();
{{endgroup}}
{{group}}
words.push_back("mario");
{{endgroup}}
{{group}}
for (size_t i = 0; i < words.size(); ++i) {
{{endgroup}}
{{group}}
for (size_t c = 0; c < words[i].size(); ++c) {
{{endgroup}}
{{group}}
if (words[i][c] == 'a') {
{{endgroup}}
{{group}}
words[i][c] = 'A';
{{endgroup}}
{{group}}
}
}
}
{{endgroup}}
{{distractor}}
{{group}}
words.pop_back("mario"); #distractor
{{endgroup}}
{{distractor}}
{{group}}
for (int i = 0; i < words.size(); ++i) { #distractor
{{endgroup}}
{{distractor}}
{{group}}
for (int c = 0; c < words[i].size(); ++c) { #distractor
{{endgroup}}
{{distractor}}
{{group}}
words[i][c] == 'A'; #distractor
{{endgroup}}
.. tb-parsons::
:name: vectors_p7
:no-indent:
Suppose album has already been defined as
vector 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 album start with b.
.. code-block:: cpp
{{group}}
count = 0
{{endgroup}}
{{group}}
for (size_t i = 0; i < album.size(); i++) {
{{endgroup}}
{{group}}
if (album[i][0] == 'b') {
{{endgroup}}
{{group}}
++count;
{{endgroup}}
{{group}}
}
}
{{endgroup}}
{{distractor}}
{{group}}
for (int i = 0; i < album.size(); i++) { #distractor
{{endgroup}}
{{distractor}}
{{group}}
if (album[i] == 'b') { #distractor
{{endgroup}}
{{distractor}}
{{group}}
if (album[i][1] == 'b') { #distractor
{{endgroup}}
{{distractor}}
{{group}}
count++ #distractor
{{endgroup}}
.. tb-parsons::
:name: vectors_p8
:no-indent:
Suppose you have the following two vectors to describe the weekly forecast
vector temps = {82.0, 76.8, 74.3, 58.8, 79.2, 73.4, 80.1}
vector 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.
.. code-block:: cpp
{{group}}
count = 0;
{{endgroup}}
{{group}}
for (int i = 0; i < 7; ++i) {
{{endgroup}}
{{group}}
if (temps[i] >= 75.0 && precip[i] < 0.50) {
{{endgroup}}
{{group}}
++count;
{{endgroup}}
{{group}}
}
}
{{endgroup}}
{{distractor}}
{{group}}
for (size_t i = 1; i <= 7; ++i) { #distractor
{{endgroup}}
{{distractor}}
{{group}}
if (temps[i] > 75.0 && precip[i] <= 0.50) { #distractor
{{endgroup}}
{{distractor}}
{{group}}
count++ #distractor
{{endgroup}}
.. tb-parsons::
:name: vectors_p9
:no-indent:
Suppose you have the following vector nouns
vector 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.
.. code-block:: cpp
{{group}}
vector proper = {};
{{endgroup}}
{{group}}
for (size_t i = 0; i < nouns.size(); ++i) {
{{endgroup}}
{{group}}
if (isupper(nouns[i][0])) {
{{endgroup}}
{{group}}
proper.push_back(nouns[i]);
{{endgroup}}
{{group}}
}
}
{{endgroup}}
{{distractor}}
{{group}}
if (isupper(nouns[i][1])) { #distractor
{{endgroup}}
{{distractor}}
{{group}}
proper.push_back(nouns[i][0]); #distractor
{{endgroup}}
{{distractor}}
{{group}}
proper.pop_back(nouns[i]); #distractor
{{endgroup}}
{{distractor}}
{{group}}
vector proper = {}; #distractor
{{endgroup}}
.. admonition:: Parsons exercise
Suppose you have the following function how_many and vector exclamations
::
int how_many (const vector& 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 excl = {"what?!", "how???", "fine!", "STOP.", "yay!!!!!", "ugh...!"};
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.
.. code-block:: text
{{group}}
vector punc = {'.', '!', '?'};
vector counts = {};
{{endgroup}}
{{group}}
for (int i = 0; i < punc.size(); ++i) {
{{endgroup}}
{{group}}
counts.push_back(how_many(excl, punc[i]));
{{endgroup}}
{{group}}
}
{{endgroup}}
{{distractor}}
{{group}}
vector punc = {".", "!", "?"}; #distractor
vector counts = {};
{{endgroup}}
{{distractor}}
{{group}}
for (int i = 0; i < excl.size(); ++i) { #distractor
{{endgroup}}
{{distractor}}
{{group}}
counts.push_back(how_many(excl, i)); #distractor
{{endgroup}}