3.17. Activecode ExercisesΒΆ

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

Q1

Question

Fix the errors in the code below so that it returns the area of a circle with radius r. Use cmath functions to get an accurate value for pi.

Example functions_a1q
1double area(int r) {
2    constexpr double pi = sin(1.0);
3    double area = pi * r ^ 2;
4    return area;
5}

Answer

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.

Example functions_a1a
1#include <cmath>
2
3double area(double r) {
4    constexpr double pi = 4*atan(1.0);
5    double area = pi * r * r;
6    return area;
7}

Q2

Fix the code below so that it prints "2 elephants".

Example functions_a2
 1#include <iostream>
 2using namespace std;
 3
 4void print_animals(string a, int b) {
 5    cout << b << a;
 6}
 7
 8int main () {
 9    // DO NOT MODIFY ANYTHING BELOW THIS LINE
10    print_animals(2, "elephants");
11}

Q3

Question

Fix the code below so that it prints 12 / 8 = 1.5.

Example functions_a3q
 1#include <iostream>
 2
 3void divide (int a, int b) {
 4    cout << a / b;
 5}
 6
 7int main () {
 8    int a = 8;
 9    int b = 12;
10
11    // DO NOT MODIFY ANYTHING BELOW THIS LINE
12    cout << b << " / " << a << " = "; divide (a, b);
13}

Answer

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.

Example functions_a3a
 1#include <iostream>
 2
 3void divide (double a, double b) {
 4    std::cout << a / b;
 5}
 6
 7int main () {
 8    int a = 8;
 9    int b = 12;
10    std::cout << b << " / " << a << " = "; divide (b, a);
11}

Q4

Finish the code below so that it calculates the common log of a minus the natural log of a and returns the difference. You will need to use cmath functions.

Example functions_a4
 1double log_subtract (double a) {
 2
 3    return difference;
 4}
 5
 6int main () {
 7    // DO NOT MODIFY ANYTHING BELOW THIS LINE
 8    cout << "Testing with a = 8..." << '\n';
 9    cout << "    Your solution has difference = ";
10    log_subtraction(8);
11    cout << '\n';
12    cout << "    The correct solution has difference = -1.17635" << '\n';
13    cout << "Testing with a = -2..." << '\n';
14    cout << "    Your solution has difference = "; log_subtraction(-2);
15    cout << '\n';
16    cout << "    The correct solution has difference = nan";
17}

Q5

Question

Finish the code below so that it prints "First Line", a border, and "Second Line." on three separate lines.

Example functions_a5q
 1#include <iostream>
 2using namespace std;
 3
 4void border () {
 5    cout << "------------\n";
 6}
 7
 8int main () {
 9    // Write some code below to call the function appropriately
10
11}

Answer

Below is one way to complete the program.

Example functions_a5a
 1#include <iostream>
 2using std::cout;
 3
 4void border () {
 5    cout << "------------\n";
 6}
 7
 8int main () {
 9    cout << "First Line.\n";
10    border();
11    cout << "Second Line.\n";
12}

Q6

Write a function called int_division that takes two doubles as parameters and returns the quotient of the integer division of the first number divided by the second. Be sure to include any necessary headers.

Example functions_a6
1int int_division () {
2
3    return quotient;
4}

Q7

Question

Write a function called gpa_boost that returns your GPA rounded up to the nearest whole value. If your GPA is already at the nearest whole value, then there is no rounding. Be sure to include any necessary headers.

Example functions_a7q
1double gpa_boost () {
2
3}

Answer

Below is one way to complete the program. I used the ceil <math/ceil> function from the ``cmath` library, but you could have solved this problem without using any functions from cmath.

Example functions_a7a
1#include <cmath>
2
3double gpa_boost (double gpa) {
4    return ceil(gpa);
5}

Q8

Write a function called volume_prism that takes three double side-lengths as parameters, and returns the volume of a the rectangular prism. Be sure to include any necessary headers.

Example functions_a8
1volume_prism ();

Q9

Question

Write a function called tan_degrees that returns the tangent of an angle given as a double in degrees. Use 3.14159 for pi. Be sure to include any necessary headers.

Example functions_a9q
1tan_degrees ();

Answer

Below is one way to complete the program. You need to make sure to convert your angle to radians before doing any calculations with trigonometric functions.

Example functions_a9a
 1#include <cmath>
 2
 3namespace mesa {
 4    constexpr double pi = 3.14159;
 5
 6    double deg_to_radian (double degrees) {
 7       return degrees * (pi / 180);
 8    }
 9}
10
11double tan_degrees (double degrees) {
12    double radians = mesa::deg_to_radian(degrees);
13    return tan(radians);
14}

Q10

Write a function called volume_sphere that takes a double radius as a parameter and returns the volume of a sphere with the provided radius. Use 3.14159 for pi. Be sure to include any necessary headers.

Example functions_a10
1volume_sphere ();