Skip to main content

Exercises 5.15 Exercises

1.

What would be returned by secretFunction if the input was 14?
int secretFunction(int input) {
  if (input % 2 == 0) {
    return 3 * input - 2;
  }
  else {
    if (input % 7 == 0) {
      return input;
    }
    return 2 * input + 9;
  }
  return input + 4;
}
  • Although 14 is divisible by 7, take another look at the conditionals.
  • The flow of code would never reach the last return statement.
  • Check your order of operations!
  • Take a closer look at the conditional statements.
  • Since 14 is divisible by 2, the function returns two less than three times 14.

2.

Vacation time! But before you go, you need to convert your currency. Let’s write the code for the dollarToYen function. dollarToYen takes dollar as a parameter and returns the equivalent amount of Japanese yen. The conversion rate is 1 USD equals 149.72 Japanese yen. Put the necessary blocks of code in the correct order.

3.

Many computations can be expressed more concisely using the β€œmultadd” operation, which takes three operands and computes a * b + c. Some processors even provide a hardware implementation of this operation for floating-point numbers. Write the function multadd so that it takes in three double values and returns the result of multiplying the first two and then adding the third value.

4.

The β€œwind chill temperature” is the temperature it feels like when the wind is blowing. The National Weather Service uses the formula \(WCT = 35.74 + 0.6215T - 35.75(V^{0.16}) + 0.4275T(V^{0.16})\) where T is the temperature in degrees F and V is the wind speed (velocity) in miles per hour to calculate the wind chill temperature. You need to write a function that will do that computation. Examine the tests and determine what the function you need to write is called, what parameters it should take, and what it should return. Then write the function.
You have attempted of activities on this page.