6.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 prints "THE TEAM" "THE TEAM" "THE TEAM" on three separate lines.
1#include <iostream>
2
3x = 8;
4y = 8;
5
6if (x % 2 == 0) {
7 std::cout << "THE TEAM";
8} else if (x >= y) {
9 std::cout << "THE TEAM";
10} else if (y >= x) {
11 std::cout << "THE TEAM";
12}
Answer
Below is one way to fix the program. Since we want "THE TEAM"
to print three times, we must check all three conditons. this
means changing the else if statements to if statements.
1#include <iostream>
2
3x = 8;
4y = 8;
5
6if (x % 2 == 0) {
7 std::cout << "THE TEAM\n";
8}
9if (x >= y) {
10 std::cout << "THE TEAM\n";
11}
12if (y >= x) {
13 std::cout << "THE TEAM\n";
14}
Q2
You are part of a class where everyone passes, but it's very hard to pass with an A. Fix the function so it prints your letter grade according to this scheme. [0, 50) = C, [50, 85) = B, and [85, 100] = A.
1#include <iostream>
2#include <string>
3
4std::string which_door (double grade) {
5 s = "";
6 if (grade < 50) {
7 s = "C";
8 }
9 if (grade < 85) {
10 s = "B";
11 }
12 if (grade >= 85) {
13 s = "A";
14 }
15 std::cout << s;
16}
Q3
Question
Fix the infinite recursion in the code below. The function should not count any numbers after 10 (the highest numbers that should print are 9 or 10). When it is done counting, the function should print that.
1#include <iostream>
2using std::cout;
3
4void count_by_2 (int num) {
5 if (num != 10) {
6 cout << num;
7 count_by_2 (num + 2);
8 }
9 else {
10 cout << num
11 << "\n_done counting!";
12 }
13}
14
15int main () {
16 count_by_2(6);
17}
Answer
Below is one way to fix the program. The infinite recursion
happens when we use an odd number as an argument. By checking
that a number is less than 99, the highest numbers to recurse
are 98 and 97. 98 + 2 == 100 and 97 + 2 == 99, so we
never count past 100.
1#include <iostream>
2using std::cout;
3
4void count_by_2 (int num) {
5 if (num < 9) {
6 cout << num;
7 count_by_2 (num + 2);
8 }
9 else {
10 cout << num
11 << "\n_done counting!";
12 }
13}
14
15int main () {
16 count_by_2(6);
17}
Q4
In the following question,
std::boolalpha is an I/O manipulator that
replaces 'falsy' values witht he word false and 'truthy'
expressions witht he word true.
Finish the code below so that it prints true if x is even
and false if x is odd.
1#include <iostream>
2
3void is_even (int num) {
4 if (num % 2 == 0) {
5 std::cout << std::boolalpha << true;
6 }
7}
Q5
Question
Finish the code below so that the function will continue to ask for input until the user guesses the word correctly.
1#include <iostream>
2#include <string>
3using namespace std;
4
5bool guess_word (string correct) {
6 cout << "Guess the word!";
7 string guess;
8 cin >> guess;
9 if (guess == correct) {
10 cout << "That's it!";
11 }
12}
Answer
Below is one way to complete the program.
1#include <iostream>
2#include <string>
3using namespace std;
4
5bool guess_word (string correct) {
6 cout << "Guess the word!";
7 string guess;
8 cin >> guess;
9 if (guess == correct) {
10 cout << "That's it!";
11 }
12 else {
13 guess_word(correct);
14 }
15}
Q6
Write the function greater that prints true
if the first double argument is greater than the
second double argument. Be sure to include any
necessary headers.
1void greater () {
2
3}
Q7
Question
Write the function good_vibes that prints "I'm having a mood day!"
depending on the value of mood. If mood is "bad", then the function
should not do anything since it's good vibes only. Be sure to
include any necessary headers.
1void good_vibes (string mood) {
2
3}
Answer
Below is one way to write the program. The return allows the function to exit if there are bad vibes in the room. Otherise, the function prints as directed.
1void good_vibes (string mood) {
2 if (mood == "bad") {
3 return;
4 }
5 cout << "I'm having a " << mood << " day";
6}
Q8
Write the function exclusive_or that prints true If
either a OR b is true, and prints false otherwise.
Be sure to include any necessary headers.
1void exclusive_or (bool a, bool b) {
2
3}
Q9
Question
Write the function countdown that takes a positive integer
and decrements it until eaching zero, printing the number at each
step of the way. Once it reaches zero, it should print "Blastoff!"
1void countdown (int num) {
2
3}
Answer
Below is one way to write the program.
1void countdown (int num) {
2 if (num != 0){
3 cout << num << '\n';
4 num -= 1;
5 countdown (num);
6 }
7 else {
8 cout << "Blastoff!";
9 }
10}
Q10
Write the function print_negative that asks the user
for a negative number. If the user does not provide a negative
number, it should contine asking until the user provides one.
It should then print the negative number.
1void print_negative () {
2
3}