Skip to main content

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:
\begin{equation*} V = \frac{4}{3} \pi r^3 \end{equation*}
Hint.
Use the lines on to construct the pseudocode, then go back to complete the exercise.

Activity 2.14.2.

Open main() Initialize and assign radius; Initialize and assign pi; Initialize volume as double; --- Initialize volume as int; #paired Calculate volume with spherical volume formula; Output calculated volume; Close main()

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.

Open main() Initialize oranges as string; Assign "apples" to oranges; Initialize apples as string; Assign "oranges" to apples; Assign apples to a temporary variable; Assign oranges to apples; Assign the temporary variable to oranges; Output apples and oranges; Close main()

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.

Open main() Initialize and assign $36.25 to price; Initialize and assign 1.20 to tip; --- Initialize and assign .20 to tip; #paired Calculate plusTip using tip and price; Output plusTip; Close main()

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.

Open main() Output the expression for average of 2 numbers; Close main()
You have attempted of activities on this page.