Skip to main content

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

Exercises 5.16 Coding Practice

1.

Write a function called calculator which takes two doubles, first and second and a char operation as parameters. calculator performs addition, subtraction, multiplication, or division with the two doubles depending on what operation is passed in (+, -, \*, /). It then returns the result. Run and test your code!
Solution.
Below is one way to implement the calculator function. Using conditionals, we return the correct result depending on which operation was given.
double calculator(double first, double second, char operation) {
    if (operation == '+') {
        return first + second;
    }
    else if (operation == '-') {
        return first - second;
    }
    else if (operation == '*') {
        return first * second;
    }
    else {
        return first / second;
    }
}

2.

A binary number is one that is expressed in the base-2 numeral system. Write a function convertToBinary which takes a decimal as a parameter. convertToBinary takes the number in decimal, converts it into a binary number, and returns the binary number. Test your function in main.
Hint.

Activity 5.16.1.

A binary number is one that is expressed in the base-2 numeral system. Write a function convertToBinary which takes a decimal as a parameter. convertToBinary takes the number in decimal, converts it into a binary number, and returns the binary number. Test your function in main. Use the lines to construct the code, then go back to complete the Activecode.

3.

An interior angle of a polygon is the angle between two adjacent sides of the polygon. Each interior angle in an equilateral triangle measures 60 degree, each interior angle in a square measures 90 degrees, and in a regular pentagon, each interior angle measures 108 degrees. Write the function calculateIntAngle, which takes a numSides as a parameter and returns a double. calculateIntAngle finds the interior angle of a regular polygon with numSides sides. The formula to find the interior angle of a regular ngon is (n - 2) x 180 / n. Run and test your code!
Solution.
Below is one way to implement the program. Using the formula given, we can find the interior angle and return it. Notice how we use 180.0 instead of 180 to avoid integer division.
#include <iostream>
using namespace std;

double calculateIntAngle(int numSides) {
    return (numSides - 2) * 180.0 / numSides;
}

4.

The astronomical start and end dates of the four seasons are based on the position of the Earth relative to the Sun. As a result, it changes every year and can be difficult to remember. However, the meteorological start and end dates are based on the Gregorian calendar and is easier to remember. Spring starts on March 1, summer starts on June 1, fall starts on September 1, and winter starts on December 1. Write a function called birthSeason, which takes two parameters, month and day. birthSeason calculates which season the birthday falls in according to the meteorological start and returns a string with the correct season. For example, birthSeason (7, 5) returns “summer” since July 5 is in the summer.
Hint.

Activity 5.16.2.

The astronomical start and end dates of the four seasons are based on the position of the Earth relative to the Sun. As a result, it changes every year and can be difficult to remember. However, the meteorological start and end dates are based on the Gregorian calendar and is easier to remember. Spring starts on March 1, summer starts on June 1, fall starts on September 1, and winter starts on December 1. Write a function called birthSeason, which takes two parameters, month and day. birthSeason calculates which season the birthday falls in according to the meteorological start and returns a string with the correct season. For example, birthSeason (7, 5) returns “summer” since July 5 is in the summer. Use the lines to construct the code, then go back to complete the Activecode.

5.

Dog owners will know that figuring out a dog’s age is more complicated than just counting age directly. Dogs mature faster than humans do, so to get a more accurate calculation of a dog’s age, write the dogToHumanYears function, which takes an dogAge as a parameter. dogToHumanYears converts and returns the dog’s age to human years. A one year old dog is 15 years old in human years; a two year old dog is 24 years old in human years. Each year after the second year counts as 4 additional human years. For example, a dog that is 3 years old is actually 28 years old in human years. Run and test your code!
Solution.
Below is one way to implement the program. We can use a conditional to check to see if the dog is one year old. If it is older than one, then we can use the formula to return the correct age in human years.
#include <iostream>
using namespace std;

int dogToHumanYears(int dogAge) {
    if (dogAge == 1) {
        return 15;
    }
    return 24 + (dogAge - 2) * 4;
}

6.

A number is a common factor of two other numbers if it divides evenly into both of the other numbers. For example, 2 is a common factor of 4 and 18, because 2 goes evenly into 4 and 18. Write the function isCommonFactor, which takes three parameters, num1, num2, and factor . isCommonFactor returns true if factor is a factor of both num1 and num2, and returns false otherwise. Run and test your code!.
Hint.

Activity 5.16.3.

A number is a common factor of two other numbers if it divides evenly into both of the other numbers. For example, 2 is a common factor of 4 and 18, because 2 goes evenly into 4 and 18. Write the function isCommonFactor, which takes three parameters, num1 , num2, and factor. isCommonFactor returns true if factor is a factor of both num1 and num2, and returns false otherwise. Run and test your code! Use the lines to construct the code, then go back to complete the Activecode.

7.

If a year is divisible by 4, then it is a leap year. However, if it is also divisible by 100, then it is not a leap year. However, if it is also divisible by 400, then it is a leap year. Thus, 2001 is not a leap year, 2004 is a leap year, 2100 is not a leap year, and 2000 is a leap year. Write the boolean function isLeapYear, which takes a year as a parameter and returns true if the year is a leap year and false otherwise. Run and test your code!
Solution.
Below is one way to implement the program. We can use conditionals in this order to efficiently determine whether or not a given year is a leap year.
#include <iostream>
using namespace std;

bool isLeapYear(int year) {
    if (year % 400 == 0) {
        return true;
    }
    else if (year % 100 == 0) {
        return false;
    }
    else if (year % 4 == 0) {
        return true;
    }
    else {
        return false;
    }
}

8.

In the enchanted Mushroom Forest, there are many different types of mushrooms as far as the eye can see. Most of these mushrooms can make delicious stews and dishes, but some of them are poisonous. Write the function isPoisonous, which takes an char size, int numSpots, and bool isRed as parameters. If a mushroom is large (‘L’) and has fewer than 3 spots, it is poisonous. If a mushroom is small (‘S’) and is red, it is poisonous. If a mushroom has fewer than 3 spots or is not red, it is poisonous. Otherwise, it is not. isPoisonous should return true if the mushroom is poisonous and false otherwise.
Hint.

Activity 5.16.4.

Use the lines to construct the code, then go back to complete the Activecode.

9.

We know that a factorial is the product of an integer and all the integers below it. For example, four factorial (4!) is 24. A triangular number is the same as a factorial, except you add all the numbers instead of multiplying. For example, the 1st triangular number is 1, the 2nd is 3, the 3rd is 6, the 4th is 10, the 5th is 15, etc. You can imagine rows of dots, where each successive row has one more dot, thus forming a triangular shape. Write the triangularNum function, which takes an int n as a parameter and returns the nth triangular number. Use recursion. Run and test your code!
Solution.
Below is one way to implement the program. We can use conditionals to separate the base case and recursive cases. Our base case is when n is 1, and in that case we return 1. Otherwise, we recursively call triangularNum on n-1.
#include <iostream>
using namespace std;

int triangularNum(int n) {
    if (n == 1) {
        return 1;
    }
    else {
        return n + triangularNum(n - 1);
    }
}

10.

Write the function digitSum which takes an int num as a parameter and returns the sum of all its digits. For example, digitSum(1423) would return 10. Use recursion.
Hint.

Activity 5.16.5.

Write the function digitSum which takes an int num as a parameter and returns the sum of all its digits. For example, digitSum (1423) would return 10. Use recursion. Use the lines to construct the code, then go back to complete the Activecode.
You have attempted 1 of 16 activities on this page.