10.8. Mixed-Up Code Exercises

Answer the following Mixed-Up Code questions to assess what you have learned in this chapter.

Q1

Construct a block of code that changes the first element of <code>vec</code> to a 6, multiplies the third element of <code>vec</code> by 2, and increments the last element of <code>vec</code> by 1 (in that order). This should work no matter what <code>vec</code> is.

  1. last = vec.size() - 1;
    vec[last]++;
  2. last = vec.size(); #distractor
    vec[last]++;
  3. vec[0] = 6;
  4. vec[0] == 6; #distractor
  5. vec[1] = 6; #distractor
  6. vec[2] = vec[2] * 2;
  7. vec[3] = vec[3] * 2; #distractor

Construct a block of code that creates a vector called <code>digs</code> whose elements are 7, 8, 7, 8. Then access elements to change the <code>digs</code> to contain the elements 7, 4, 7, 4. <b>Important</b>: Change the <code>8</code>'s to <code>4</code>'s in order of increasing index.

  1. digs.pop_back();
  2. digs.pop_back(4); #distractor
  3. digs.push_back(4);
  4. digs[1] = 4;
  5. digs[2] = 4; #distractor
  6. vector digs = {7, 8, 7, 8}; #distractor
  7. vector<int> digs = [7, 8, 7, 8]; #distractor
  8. vector<int> digs = {7, 8, 7, 8};

Construct a block of code that creates a vector called <code>nums</code> whose elements are five <code>1</code>'s. Then make a copy of this vector called <code>digits</code>, and use vector operations to change digits to <code>{1, 2, 3}</code>.

  1. digits.pop_back();
    digits.pop_back();
  2. digits.push_back(); #distractor
    digits.push_back();
  3. digits[1]++;
    digits[2] = digits[2] * 3;
  4. digits[2]++; #distractor
    digits[3] = digits[3] * 3;
  5. vector nums = {1, 1, 1, 1, 1}; #distractor
  6. vector<int> digits = nums;
  7. vector<int> nums (5, 1);
  8. vector<int> nums = digits; #distractor

Construct a block of code that loops over a vector called <code>numbers</code> and transforms the vector so each element is doubled.

  1. for (int i = 0; i < numbers.size(); i++) { #distractor
  2. for (size_t i = 0; i < numbers.size(); i++) {
  3. for (size_t i = 1; i <= numbers.size(); ++i) { #distractor
  4. numbers[i] * 2; #distractor
  5. numbers[i] = numbers[i] * 2;
  6. vector numbers = {1, 2, 3, 4, 5}; #distractor
  7. vector<int> numbers = {1, 2, 3, 4, 5};
  8. }

Suppose you have the vector

<pre> <code>

vector<string> words = {"car", "cat", "switch", "princess"};

</code> </pre>

Construct a block of code that transforms the vector to

<pre> <code>

vector<string> words = {"c_ar", "c_at", "switch", "m_ario"}

</code> </pre>

  1.   }
     }
    }
  2. for (int c = 0; c < words[i].size(); ++c) { #distractor
  3. for (int i = 0; i < words.size(); ++i) { #distractor
  4. for (size_t c = 0; c < words[i].size(); ++c) {
  5. for (size_t i = 0; i < words.size(); ++i) {
  6. if (words[i][c] == 'a') {
  7. words.pop_back("mario"); #distractor
  8. words.pop_back();
  9. words.push_back("mario");
  10. words[i][c] = 'A';
  11. words[i][c] == 'A'; #distractor

Suppose <code>album</code> has already been defined as

<pre> <code>

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"}

</code> </pre>

Construct a block of code that counts how many songs in <code>album</code> start with b.

  1.  }
    }
  2. ++count;
  3. count = 0
  4. count++ #distractor
  5. for (int i = 0; i < album.size(); i++) { #distractor
  6. for (size_t i = 0; i < album.size(); i++) {
  7. if (album[i] == 'b') { #distractor
  8. if (album[i][0] == 'b') {
  9. if (album[i][1] == 'b') { #distractor

Suppose you have the following two vectors to describe the weekly forecast

<pre> <code>

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}

</code> </pre>

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.

  1.  }
    }
  2. ++count;
  3. count = 0;
  4. count++ #distractor
  5. for (int i = 0; i < 7; ++i) {
  6. for (size_t i = 1; i <= 7; ++i) { #distractor
  7. if (temps[i] > 75.0 && precip[i] <= 0.50) { #distractor
  8. if (temps[i] >= 75.0 && precip[i] < 0.50) {

Suppose you have the following vector <code>nouns</code>

<pre> <code>

vector<string> nouns = {"cereal", "Cocoa Puffs", "Mario", "luigi", "Aerosmith"};

</code> </pre>

Construct a block of code that creates a vector of the <b>proper</b> nouns in <code>nouns</code>. Use the <code>isupper</code> function to check if a letter is uppercase.

  1.  }
    }
  2. for (size_t i = 0; i < nouns.size(); ++i) {
  3. if (isupper(nouns[i][0])) {
  4. if (isupper(nouns[i][1])) { #distractor
  5. proper.pop_back(nouns[i]); #distractor
  6. proper.push_back(nouns[i]);
  7. proper.push_back(nouns[i][0]); #distractor
  8. vector proper = {}; #distractor
  9. vector<string> proper = {};

Parsons exercise

Suppose you have the following function <code>how_many</code> and vector <code>exclamations</code>

int how_many (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...!"};

Construct a block of code that counts how many times ".", "!", and "?" occur in <code>exclamations</code>. Save the counts to a vector with "." count as the first element, "!" count as the second, and "?" count as the third.

{{group}}
vector<char> punc = {'.', '!', '?'};
vector<int> 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<string> punc = {".", "!", "?"}; #distractor
vector<int> 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}}