Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Exercises 4.13 Activecode Exercises

Answer the following Activecode questions to assess what you have learned in this chapter.

1.

Fix the code below so that it prints “THE TEAM” “THE TEAM” “THE TEAM” on three separate lines.
Solution.
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.

2.

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, 95) = B, and [95, 100] = A.
Hint.
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, 95) = B, and [95, 100] = A. Use the lines to construct the code, then go back to complete the Activecode.

Activity 4.13.1.

3.

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.
Solution.
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.
#include <iostream>
using namespace std;

void countBy2(int num) {
    if (num < 9) {
        cout << num;
        countBy2(num + 2);
    }
    else {
        cout << num << endl;
        cout << "Done counting!";
    }
}

int main() {
    countBy2(5);
}

4.

Finish the is_evencode below so that it prints true if x is even and false if x is odd.
Hint.
Finish the code below so that it prints true if x is even and false if x is odd. Use the lines to construct the code, then go back to complete the Activecode.

Activity 4.13.2.

5.

Finish the code below so that the function will continue to ask for input until the user guesses the word correctly.
Solution.
Below is one way to complete the program.
#include <iostream>
using namespace std;

bool guessTheWord(string correct) {
    cout << "Guess the word!";
    string guess;
    cin >> guess;
    if (guess == correct) {
        cout << "That's it!";
    }
    else {
        guessTheWord(correct);
    }
}

6.

Write the function greaterThan that prints true if the first double argument is greater than the second double argument.
Hint.
Write the function greaterThan that prints true if the first double argument is greater than the second double argument. Be sure to include any necessary headers. Use the lines to construct the code, then go back to complete the Activecode.

Activity 4.13.3.

7.

Write the function goodVibes 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.
Solution.
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.
#include <iostream>
using namespace std;

void goodVibes(string mood) {
    if (mood == "bad") {
        return;
    }
    cout << "I'm having a " << mood << " day";
}

8.

Write the function exclusiveOr that prints true If either a OR b is true, and prints false otherwise.
Hint.
Write the function exclusiveOr that prints true If either a OR b is true, and prints false otherwise. Be sure to include any necessary headers. Use the lines to construct the code, then go back to complete the Activecode.

Activity 4.13.4.

9.

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!”
Solution.
Below is one way to write the program.
#include <iostream>
using namespace std;

void countdown(int num) {
    if (num != 0) {
        cout << num << endl;
        num -= 1;
        countdown (num);
    }
    else {
        cout << "Blastoff!";
    }
}

10.

Write the function printNegativeNum 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.
Hint.
Write the function printNegativeNum 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. Use the lines to construct the code, then go back to complete the Activecode.

Activity 4.13.5.

You have attempted 1 of 17 activities on this page.