4.19. Coding PracticeΒΆ

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!

Q1

Question

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!

Example cp_5_AC_1q
1double calculator (double first, double second, char operation) {
2    // Write your implementation here.
3}

Answer

Below is one way to implement the calculator function. Using conditionals, we return the correct result depending on which operation was given.

Example cp_5_AC_1a
 1 double calculator (double first, double second, char operation) {
 2     if (operation == '+') {
 3         return first + second;
 4     }
 5     if (operation == '-') {
 6         return first - second;
 7     }
 8     if (operation == '*') {
 9         return first * second;
10     }
11     return first / second;
12 }

Q2

A binary number is one that is expressed in the base-2 numeral system. Write a function convert_to_binary which takes a decimal as a parameter. convert_to_binary takes the number in decimal, converts it into a binary number, and returns the binary number. Run and test your code!

Example cp_5_AC_2q
1int to_binary (int decimal) {
2    // Write your implementation here.
3}

Q3

Question

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 interior_angle, which takes a sides as a parameter and returns a double. interior_angle finds the interior angle of a regular polygon with sides sides. The formula to find the interior angle of a regular ngon is (n - 2) x 180 / n. Run and test your code!

Example cp_5_AC_3q
1double interior_angle (int sides) {
2    // Write your implementation here.
3}

Answer

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.

Example cp_5_AC_3a
1double interior_angle (int sides) {
2    return (sides - 2) * 180.0 / sides;
3}

Q4

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 birth_season, which takes two parameters, month and day. birth_season calculates which season the birthday falls in according to the meteorological start and returns a string with the correct season. For example, birth_season (7, 5) returns "summer" since July 5 is in the summer.

Run and test your code!

Example cp_5_AC_4q
1string birth_season (int month, int day) {
2    // Write your implementation here.
3}

Q5

Question

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 dog_to_human_years function, which takes an dog_age as a parameter. dog_to_human_years 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!

Example cp_5_AC_5q
1int dog_to_human_years (int dog_age) {
2    // Write your implementation here.
3}

Answer

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. We also don't try to convert negative dog years.

Example cp_5_AC_5a
1int dog_to_human_years (int dog_age) {
2    if (dog_age < 1) {
3        return 0;
4    }
5    if (dog_age == 1) {
6        return 15;
7    }
8    return 24 + (dog_age - 2) * 4;
9}

Q6

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 is_common_factor, which takes three parameters, num1, num2, and factor. is_common_factor returns true if factor is a factor of both num1 and num2, and returns false otherwise. Run and test your code!

Example cp_5_AC_6q
1bool is_common_factor (int num1, int num2, int factor) {
2    // Write your implementation here.
3}

Q7

Question

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 is_leap_year, 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!

Example cp_5_AC_7q
1bool is_leap_year (int year) {
2    // Write your implementation here.
3}

Answer

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.

Example cp_5_AC_7a
 1bool is_leap_year (int year) {
 2    if (year % 400 == 0) {
 3        return true;
 4    }
 5    if (year % 100 == 0) {
 6        return false;
 7    }
 8    if (year % 4 == 0) {
 9        return true;
10    }
11    return false;
12}

Q8

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 poisonous, which takes an char size, int num_spots, and bool is_red 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. poisonous should return true if the mushroom is poisonous and false otherwise. Run and test your code!

Example cp_5_AC_8q
1bool poisonous (char size, int num_spots, bool is_red) {
2    // Write your implementation here.
3}

Q9

Question

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 triangular_num function, which takes an int n as a parameter and returns the nth triangular number. Use recursion. Run and test your code!

Example cp_5_AC_9q
1int triangular_num (int n) {
2    // Write your implementation here.
3}

Answer

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 triangular_num on n-1.

Example cp_5_AC_9a
1int triangular_num (int n) {
2    if (n == 1) {
3        return 1;
4    }
5    return n + triangular_num(n - 1);
6}

Q10

Write the function digit_sum which takes an int num as a parameter and returns the sum of all its digits. For example, digit_sum (1423) would return 10. Use recursion. Run and test your code!

Example cp_5_AC_10q
1int digit_sum (int num) {
2    // Write your implementation here.
3}