10.9. Activecode ExercisesΒΆ
Answer the following Activecode questions to assess what you have learned in this chapter.
Q1
Question
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.
1int make_vector () {
2 vector<int> nums (5) = 1;
3 return nums;
4}
Answer
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'.
1#include <iostream>
2#include <vector>
3
4std::vector<int> make_vector () {
5 std::vector<int> nums (5,1);
6 nums[3] = 2;
7 return nums;
8}
Q2
Fix the function below so that it returns how many even numbers are in nums.
1#include <vector>
2
3int count_even (const vector<int>& nums) {
4 for (int i = 0; i < nums.size(); i++) {
5 if (i % 2 == 0) {
6 count = count + 1;
7 }
8 }
9 return count;
10}
Q3
Question
Fix the function below so that it creates a vector of all of the
words in words that end with the passed character.
1#include <iostream>
2#include <string>
3#include <vector>
4
5int ends_with (const std::vector<std::string>& words, char c) {
6 int count;
7 for (size_t i = 0; i <= words.size(); i++) {
8 last = words.size() - 1;
9 if (words[last] == c) {
10 ++count;
11 }
12 }
13 return count;
14}
OK Answer
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.
1#include <string>
2#include <vector>
3
4int ends_with (const std::vector<std::string>& words, char c) {
5 int count = 0;
6 for (size_t i = 0; i < words.size(); i++) {
7 int last = words[i].size() - 1;
8 if (words[i][last] == c) {
9 ++count;
10 }
11 }
12 return count;
13}
Better Answer
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.
1#include <string>
2#include <vector>
3
4int ends_with (const std::vector<std::string>& words, char c) {
5 int count = 0;
6 for (const std::string& word: words) {
7 if (word.back() == c) {
8 ++count;
9 }
10 }
11 return count;
12}
Q4
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.
1int count_covid (const std::vector<double>& temps) {
2}
Q5
Question
Finish the code below so that it creates removes elements from the end of the vector until
it ends with "stop".
1int main () {
2 std::vector<string> words = {"roses", "are", "red", "violets", "stop", "are", "blue"}
3
4 while( ) {
5
6 }
7
8}
Answer
Below is one way to finish the program. We just use the pop_back function until the
last element of the vector is "stop".
1#include <string>
2#include <vector>
3
4int main () {
5 std::vector<std::string> words = {"roses", "are", "red", "violets", "stop", "are", "blue"};
6 while (words.back() != "stop"){
7 words.pop_back();
8 }
9}
Q6
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.
1#include <vector>
Q7
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.
1#include <vector>
Answer
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.
1#include <cstddef>
2#include <vector>
3
4
5bool all_have_char (const vector<string>& data, char c) {
6 size_t count = 0;
7 for (const string& tokens: data) {
8 for (const char tok: tokens) {
9 if (tok == c) {
10 count++;
11 }
12 }
13 }
14 return count == data.size();
15}
Q8
Question
Write a function mean which returns the average of a vector of numbers.
No tests are provided for this question.
1// student code goes here.
Answer
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?
1#include <vector>
2
3double mean (const vector<double> nums) {
4 double sum = 0;
5 for (const auto& value: nums) {
6 sum += value;
7 }
8 return sum/nums.size();
9}
Q9
Write the function hundy_bundy that returns a count of all numbers
in a vector that are divisible by 100.
No tests are provided for this question.
1#include <vector>
Q10
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! )
1#include <vector>
Answer
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.
1#include <vector>
2
3bool is_even (int value) {
4 return value % 2 == 0;
5}
6
7void make_odd ( std::vector<int>& nums) {
8 for (int& value: nums) {
9 if (is_even(value) && value > 0) {
10 --value;
11 }
12 }
13}
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.
1// student code goes here.