10.9. Activecode Exercises¶
Answer the following Activecode questions to assess what you have learned in this chapter.
Fix the code below so that it creates a vector with 5 elements all initialized to 1, and changes the third element of that vector to a 2.
Below is one way to fix the program.
Always include the
<vector>
header when using vectors.To initialize all vector elements to a certain value, you must include that as a second argument after the size.
Finally, vectors are zero-indexed. The third element is at index ‘2’.
Fix the function below so that it returns how many even numbers are in nums
.
Fix the function below so that it creates a vector of all of the
words in words
that end with the passed character.
Below is one way to fix the function. However it is complicated and error prone.
We must initialize count
to zero and
declare last
as an integer.
To access a string inside of vec
, we use vec[i]
.
To get the last character, we must index the
string to the last index, which is one less than the length of the string.
Below is a simpler way to fix the function.
We still must initialize count
to zero.
However, it is better to avoid array indexing completely.
In general, it is always better to avoid array indexing
if it is not needed.
Someone could have COVID 19 if their temperature is above 99.9 degrees Fahrenheit. Finish the code below so that it counts how many students in the class may have been exposed.
Finish the code below so that it creates removes elements from the end of the vector until
it ends with "stop"
.
Below is one way to finish the program. We just use the pop_back
function until the
last element of the vector is "stop"
.
Write the function ends_even
that takes a vector and removes
elements from the end of the vector until
it ends with an even number.
No tests are provided for this question.
Write a function called all_have_char
that returns a bool if
every string in the provided vector contains the test character.
It should return true only if all strings contain the character.
No tests are provided for this question.
Below is one way to finish the program.
We loop through the vector, and search each string.
If the string has the character, then it is added to count
.
We then check whether count
is equal to the number of elements in vec
and return a boolean.
Write a function mean
which returns the average of a vector of numbers.
No tests are provided for this question.
Below is one way to finish the program.
First we take the sum, then divide the sum by the number
of elements in nums
.
In the solution below, an empty vector results in a divide by zero error.
How could we guard against that?
Write the function hundyBundy
that returns a count of all numbers
in a vector that are divisible by 100.
No tests are provided for this question.
Write the function make_odd
which subtracts 1 from every
even number in a vector of integers.
We don’t want any negative values so don’t subtract 1 from 0.
( remember to take in the vector by reference to make changes to the actual vector! )
Below is one way to finish the program. We us the modulus operator to check for even numbers and decrement them. we keep an extra check for 0 to make sure we are not decrementing 0.
Write the function weird_print
that prints the first half of a
vector of integers in reverse order
and then prints the second half in the order present in the vector.
If we start with {1,2,3,4,5,6}
we would print 3 2 1 4 5 6
.