CPSC150A
Scientific Computing

Activity 6

Fruitful Functions

Multiply Integers

Write a function called multiply_integers(a, b), which has two integers as parameters. The function should return the result of multiplying the integers a and b together.

Average Four

Write a function called average_four(value1, value2, value3, value4), which has 4 floating point values as parameters. The function should return the arithmetic mean (standard average) of the four values.

Coin Counting

There are many people that would call me a lazy person. I don’t find that offensive, but I think it’s not a valid description. It’s not that I’m lazy, I just try to be as efficient as possible. For example, in college a group of friends and I deduced the fewest amount of coins to carry that would produce any arbitrary amount of change. While a computation like that is still a little too complex, figuring out the fewest amount of coins for a fixed amount of change is possible.

Details

Create the function count_coins(cents) that has one parameter, cents, an integer in the range 0 to 100. The function should return the fewest number of coins (using the American monetary system consisting of quarters, dimes, nickles and pennies) necessary to make the specified amount of change.

Test your function by calling the function multiple times with different parameters.

Test Cases

Function Parameters Expected Output
0 0
1 1
2 2
5 1
10 1
15 2
16 3
25 1
50 2

Hint

  • While it might seem like you are going to use a loop for this function, you actually won’t use a single loop.
  • You can use integer division (//) to determine how many times a coin goes into the change amount evenly.
  • Recall that you can use the mod (%) operator to determine the remainder of an integer division operator. Use this to determine the change amount the rest of the coins need to cover.
  • Start with the largest coin first (quarters) and work your way down. This is known as a greedy approach, and is very common in computer science.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only programs that have perfect functionality will be accepted as complete.