Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Making Change Write a MATLAB function called makechange to determine the minimum number of coins that are needed to make a given amount of change.

Making Change
Write a MATLAB function called makechange to determine the minimum number of coins that are
needed to make a given amount of change. For example, with US currency, the minimum number of
coins to make 36 cents in change is three (1 quarter, 1 dime, and 1 penny). Your function should
take two input arguments in this order: the amount of change needed and an array of coin
denominations. For example, for US currency, the amount of change needed must be a whole
number of cents and the array of coin denominations should be 25,10,5,1. Your function should
sort this array in descending order using the sort function with the 'descend' option. Your
function should have one return argument: the number of coins needed. Your function should check
that the inputs are valid. Specifically, your function should verify that the amount of change is non-
negative and the array of coin denominations contains at least one value. If the inputs are invalid,
your function should return a value of -1. Also, if it is not possible to make exact change with the
given set of coin denominations, your function should return a value of -1. As one purpose of this
assignment is to provide experience with loops, your code should solve this problem using loops.
Your code should use the following approach:
amount: amount of change to make. E.g.,36
sort coins into descending order
Loop over the array of coins, with c being the current coin:
Loop while it possible to deduct c from amount:
subtract c from amount
increase the coin count by 1
End_Loop
End_Loop
Note, it is possible to solve this problem more efficiently using integer division tricks such as modulo
and floor, but that would circumvent the purpose of this assignment.
Write a script called runMakechange that calls your makeChange function to compute the
number of US coins needed to make change for every whole number of cents between 0 and 99.
Your script should then graph the number of coins vs. the amount of change. Your graph should
include appropriate titles and labels. Save the graph as a png file with the name "change.png".
Finally, your script should call your Arraystats function to determine the maximum, minimum,
mean, median, and standard deviation of the coins needed. Print these results to the console with
appropriate formatting to make the output easily understandable.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions