Skip to main content

Exercises 13.13 Exercises

1.

Create a vector of strings called words that contains "Oregon" and "Washington". Then read in three more words (using cin >> into a string variable you create) and add them to the end of the vector.

2.

Write the function void removeLastHalf(vector<int>& vec); that removes the last half of the vector. If the size is odd, round up the number of items to keep (so for a 5 item list you should keep the first 3 items and remove the last two).
Note that the function takes a reference, so you should make changes to vec and not try to return anything.
For example:
Input: {1, 2, 3, 4, 5, 6}
Result: {1, 2, 3}

Input: {10, 20, 30}
Result: {10, 20}
Hint.
You could use a loop, but it is much easier to use .size() to figure out the midpoint and then .erase() to delete the last half of the vector.

3.

Write the function void swapEnds(vector<int>& vec); that swaps the first and last items of the given vector.
For example:
Input: {1, 2, 3, 4, 5}
Returns: {5, 2, 3, 4, 1}

Input: {10, 20, 30}
Output: {30, 20, 10}
Hint 1.
Remember that the index of the last item is always vec.size() - 1
Hint 2.
You will need to use a temporary variable to hold a value while swapping the first and last elements.

4.

Write the function vector<int> swapEnds(const vector<int>& original); that copies the given vector and then swaps the first and last items of the copy before returning the copy.
Hint 1.
This vector is passed by const reference, so we can’t make changes to the original vector.
Hint 2.
You will need to use a temporary variable to hold a value while swapping the first and last elements.

5.

Complete the function trimZeros that removes values from the end of vec until the last value is not 0.
For example:
Input: {1, 4, 0, 0, 0}
Result: {1, 4}

Input: {1, 8, 3, 0, 2, 0}
Result: {1, 8, 3, 0, 2}
Hint 1.
This is not really a traversal problem. Yes, you will need a loop, but you do not need to visit every element. Start by just checking the last element and calling .pop_back() to remove it if it is 0. Then you can repeat that process until the last element is not 0.
Hint 2.
You will need a loop based on β€œwhat is the value of the last element”. Remember that the last element is at index .size() - 1 and changes each time you remove something from the back.

6.

Complete the function maximumValue that returns the largest value in vec

7.

Complete the function countOver that returns the number of values greater than value.

8.

Write a function isBlankFree that takes a vector of strings and returns true if there are no empty strings ("") in it. Return false if there is at least one empty string.
Tip: start by making a function that returns true to make sure you have the function prototype correct. Once the function compiles, you can worry about passing the tests.

9.

Write a function limit that takes a vector of integers and returns a new vector that is a copy of the original where every value less than 0 is set to 0 and every value greater than 100 is set to 100.
For example, passing in 54 -6 82 100 102 would return a new vector containing 54 0 82 100 100.
Tip: start by making a function that makes an empty vector of integers and returns it to make sure you have the function prototype correct. Once the function compiles, you can worry about passing the tests.
You have attempted of activities on this page.