4.19. Coding Practice¶
Write a function called calculator
which takes two double
s, first
and
second
, and a char operation
as parameters. calculator
performs
addition, subtraction, multiplication, or division with the two double
s
depending on what operation is passed in (+
, -
, *
, /
).
It then returns the result.
Run and test your code!
Write a function called calculator
which takes two double
s, first
and
second
, and a char operation
as parameters. calculator
performs
addition, subtraction, multiplication, or division with the two double
s
depending on what operation is passed in (+
, -
, *
, /
).
It then returns the result.
Run and test your code!
Below is one way to implement the calculator
function. Using conditionals,
we return the correct result depending on which operation was given.
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.
Run and test your code!
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!
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.
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.
Run and test your code!
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!
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.
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!
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!
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.
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 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. poisonous
should return true
if
the mushroom is poisonous and false
otherwise. Run and test your code!
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 n
th triangular number. Use recursion. Run and test your code!
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
.
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!