7 Making change

A cashier needs to compute the number of quarters, dimes, and nickels when making change. Usually, the cashier tries to return the minimum number of coins back to the customer. To do this, the cashier first computes the maximum number of quarters whose total value is less than or equal to the amount of change required; the cashier then performs a similar calculation for the number of dimes needed for the remaining amount of change, and finally the cashier performs a similar calculation for the number of nickels needed for the remaining amount of change.

In MATLAB, open the file numQuarters.m. This file contains a user-defined function that when completed returns the maximum number of quarters whose total value does not exceed a given number of cents.

To solve this problem, you need to divide the given number of cents by \(25\) (the number of cents in one quarter) and then round the result down to the nearest integer. In MATLAB there are four different rounding functions:

  1. round
  2. ceil
  3. fix
  4. floor

Read the help documentation for each of the rounding functions to determine which one you need to use to find the correct number of quarters.

Complete the function remembering to save the file every time that you edit it.

7.1 Using numQuarters.m

Test your function by typing the following in the MATLAB Command Window:

price = 51;
numQ = numQuarters(price)

A correct function would cause the value 2 to be output. If the correct value is not output, review your function and correct any errors that you find.

Test your function using different values for the price; for example, type following statements in the MATLAB Command Window and verify that the outputs are correct:

numQuarters(0)
numQuarters(99)
numQuarters(100)
numQuarters(101)

7.2 Solve a simple problem using numQuarters.m

Suppose that you need to make change equal to 74 cents. You can minimize the number of coins needed to make 74 cents by using the following steps:

  1. compute the number of quarters needed to make 74 cents
  2. compute the remaining amount of change needed after subtracting the total value of the quarters
  3. compute the number of dimes needed to make the remaining amount of change
  4. compute the remaining amount of change needed after subtracting the total value of the dimes
  5. compute the number of nickels needed to make the remaining amount of change

In MATLAB, open the script called quarters.m. Edit the script so that solves Steps 1 and 2 in the above problem—you do not need to compute the number of dimes and nickels (although you should understand how you would do so). Your script should use the function numQuarters.m that you created. Your script should store the the number of quarters in a variable called nq and it should store the remaining amount of change needed after subtracting the total value of the quarters in a variable named remaining.

Note that if someone changes the value of the variable cost in the script then the script should compute the correct values of nq and remaining (in other words, your script should not simply set nq to 2 and remaining to 24).