Skip to main content

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

Exercises 3.15 Activecode Exercises

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

1.

Fix the errors in the code below so that it prints the area of a circle with radius 5. Use cmath functions to get an accurate value for pi.
Solution.
Below is one way to fix the program. C++ doesn’t use the ^ operator for exponents. We can get the square of r by multiplying it by itself. We call the function with an argument of 5.
void printArea(double r) {
    double pi = acos(-1.0);
    double area = pi * r * r;
    cout << area;
}

2.

Fix the code below so that it prints “2 elephants”.
Hint.
Fix the code below so that it prints “2 elephants”. Use the lines to construct the code, then go back to complete the Activecode.

Activity 3.15.1.

3.

Fix the code below so that it prints 12 / 8 = 1.5.
Solution.
Below is one way to fix the program. It’s crucial that you input your arguments in the correct order so as to avoid a semantic error. Also, it’s important that you understand that when you divide two integers you will get an integer as a result.
#include <iostream>
using namespace std;

void divide(double a, double b) {
    cout << a / b;
}

int main() {
    int a = 8;
    int b = 12;
    cout << b << " / " << a << " = ";
    divide (b, a);
}

4.

Finish the code below so that it calculates the common log of a minus the natural log of a and prints the difference. You will need to use cmath functions.
Hint.
Finish the code below so that it calculates the common log of a minus the natural log of a and prints the difference. You will need to use cmath functions. Use the lines to construct the code, then go back to complete the Activecode.

Activity 3.15.2.

5.

Finish the code below so that it prints “First Line”, a border, and “Second Line.” on three separate lines.
Solution.
Below is one way to complete the program.
#include <iostream>
using namespace std;

void border() {
    cout << "------------" << endl;
}

int main() {
    cout << "First Line." << endl;
    border();
    cout << "Second Line." << endl;
}

6.

Write a function called intDivision that takes two doubles as parameters and prints the quotient of the integer division of the first number divided by the second. Be sure to include any necessary headers.
Hint.
Write a function called intDivision that takes two doubles as parameters and prints the quotient of the integer division of the first number divided by the second. Use the lines to construct the code, then go back to complete the Activecode.

Activity 3.15.3.

7.

Write a function called gpaBoost that prints your GPA rounded up to the nearest point. If your GPA is already at the nearest point, there is no rounding. Be sure to include any necessary headers.
Solution.
Below is one way to complete the program. I used the ceil function from the cmath library, but you could have solved this problem without using any functions from cmath.
#include <iostream>
#include <cmath>
using namespace std;

void gpaBoost(double GPA) {
    int betterGPA = ceil(GPA);
    cout << betterGPA << ".000";
}

8.

Write a function called volumePrism that takes three double sidelengths as parameters, and calculates and prints the volume of a the rectangular prism. Be sure to include any necessary headers.
Hint.
Write a function called volumePrism that takes three double sidelengths as parameters, and calculates and prints the volume of a the rectangular prism. Use the lines to construct the code, then go back to complete the Activecode.

Activity 3.15.4.

9.

Write a function called tanD that prints the tangent of an angle given as a double in degrees. Use 3.14 for pi. Be sure to include any necessary headers.
Solution.
Below is one way to complete the program. You need to make sure to convert your angle to radians before doing any calculations with sinusoidal functions.
#include <iostream>
#include <cmath>
using namespace std;

void tanDegrees(double degrees) {
    double radians = degrees * (2 * 3.14) / 360.0;
    double tangent = tan(radians);
    cout << tangent;
}

10.

Write a function called volumeSphere that takes a double radius as a parameter, and calculates and prints the volume of a sphere with that radius. Use 3.14 for pi. Be sure to include any necessary headers.
Hint.
Write a function called volumeSphere that takes a double radius as a parameter, and calculates and prints the volume of a sphere with that radius. Use 3.14 for pi. Use the lines to construct the code, then go back to complete the Activecode.

Activity 3.15.5.

You have attempted 1 of 16 activities on this page.