Skip to main content

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

Exercises 2.14 Activecode Exercises

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

1.

Fix the code below so that it runs without errors. Hint: you might need to change the names of some variables.
Solution.
Below is one way to fix the program. true and false are keywords, so they cannot be used as variable names.
#include <iostream>
using namespace std;

int main() {
    char t = 'T';
    char f = 'F';
    cout << t << " is short for true. ";
    cout << f << " is short for false." << endl; cout << f << " is short for false." << endl;
}

2.

Finish the code below so that it prints “I drive a 2014 Buick Regal”
Hint.
Finish the code below so that it prints “I drive a 2014 Buick Regal”. Use the lines to construct the pseudocode, then go back to complete the exercise.

Activity 2.14.1.

Finish the code below so that it prints “I drive a 2014 Buick Regal”. Use the lines to construct the pseudocode, then go back to complete the exercise.

3.

Fix the code below so that it prints “Cady scored 90% on the exam.”
Solution.
Below is one way to fix the program. We want to use doubles so that our result isn’t rounded down to 0 through integer division.
#include <iostream>
using namespace std;

int main() {
    double Cady = 3 * 5 * (6 / 100.0);
    cout << "Cady scored " << Cady * 100 << "% on the exam.";
}

4.

Finish the code below so that it returns the correct volume of a sphere. Hint: think about what happens when you use integer division. The volume of a sphere is given by:
V=43πr3
Hint.
Use the lines on to construct the pseudocode, then go back to complete the exercise.

Activity 2.14.2.

5.

Fix the code below so that assigns a its correct value of 'a'. Hint: use character operations!
Solution.
Below is one way to complete the program. There are many creative ways that you could use the order of operations to come up with a complex expression that will bring you to ’a’, here is one way.
#include <iostream>
using namespace std;

int main() {
    char a = 's';
    a = a - (3 * (4 + 1) + 3);
    cout << a;
}

6.

Write code that assigns “apples” to the variable oranges, and “oranges” to the variable apples, then swaps their values. Be sure to include any necessary headers. YOU MAY NOT HARDCODE YOUR SOLUTION.
Hint.
Use the lines to construct the pseudocode, then go back to complete the exercise.

Activity 2.14.3.

7.

Write code that prints “Eat”, “More”, and “Chicken” on 3 consecutive lines. Be sure to inclue any necessary headers.
Solution.
Below is one way to implement the solution.
#include <iostream>
using namespace std;

int main() {
    cout << "Eat" << endl;
    cout << "More" << endl;
    cout << "Chicken" << endl;
}

8.

Write code that calculates how much you you will spend after tipping 20% on your $36.25 dinner. Save the result of this calculation in plusTip. Be sure to include any necessary headers.
Hint.
Use the lines to construct the pseudocode, then go back to complete the exercise.

Activity 2.14.4.

9.

You have about three hours and fifteen minutes of homework to do today. Rather than starting it right away, you choose to procrastinate by calculating how many seconds you’ll be spending on your work. Convert the time to seconds and store the result in seconds. Be sure to inclue any necessary headers.
Solution.
Below is one way to implement the solution.
#include <iostream>
using namespace std;

int main() {
    int hours = 3;
    int minutes = 15;
    int totalMinutes = minutes + 60 * hours;
    int seconds = totalMinutes * 60;
}

10.

Write code that calculates and prints the average of a and b if a = 3.14, and b = 1.59. You may only use one line of code. Be sure to inclue any necessary headers.
Hint.
Write code that calculates and prints the average of a and b if a = 3.14, and b = 1.59. You may only use one line of code. Use the lines on the to construct the pseudocode, then go back to complete the Activecode.

Activity 2.14.5.

You have attempted 1 of 16 activities on this page.