Skip to main content

Exercises 13.11 Exercises

1.

Create a vector of strings called words that contains (in order) "Oregon" and "Washington". Then read in three more words (using cin >> stringVariable) and add them to the vector.

2.

Remove the first element of vec and then insert the value 6 at index 2.

3.

Create a vector of strings called words that contains (in order) "Oregon" and "Washington". Then read in three more words (using cin >> stringVariable) and add them to the vector.

4.

Write the function vector<int>;swapEnds(???) that copies the given vector and then swaps the first and last items of the copy before returning the copy. You get to decide on the parameter type to use (whether to take the input by value or reference).
For example:
Input: 1 2 3 4 5
Returns: 5 2 3 4 1

Input: 10 20 30
Output: 30 20 10

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.
You will need a loop based on 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 minimumVal that returns the smallest value in vec
Hint.
You will need a loop based on 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.

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.

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 result in 54 0 82 100 100.
You have attempted of activities on this page.