10.8. 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 <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.

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.

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>.

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

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 = {“cAr”, “cAt”, “switch”, “mArio”}

</code> </pre>

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.

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.

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.

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

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

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.

You have attempted of activities on this page