I need help creating a code to calculate monetary change

What is the code to determine how to give change in the correct amount of bills and coins, minimizing the total number of items returned. With the amounts of money owed and given must be variable and based on user input.

3 Kommentare

This sounds like homework. What have you tried so far?
You can find guidelines for posting homework on this forum here. If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).
bill = 1 + (200-1) .* rand(1,1)
bill = round(bill,2)
pay = input("How much cash did the customer give you?")
change = pay-bill
This is what I have so far, its really simple and it calculates the change but I'm not sure where to start when determining how many bills and coins? Would that need some kind of a conditional statement, with If, and when, etc?
The difficulty of minimizing the number of coins returned depends upon the list of values of the coins.
The UK monetary system uses denominators that are 10^N times {1, 2, 5} -- so for example, 1 pence, 2 pence, 5 pence, 10 pence, 20 pence, 50 pence, and so on. That system allows you to use the "greedy" algorithm.
The US monetary system, which has 1 cent, 5 cent, 10 cent, 25 cent, 50 cent (seldom seen), and $1 . The "greedy" algorithm does not work with US coins.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021a

Gefragt:

am 21 Sep. 2021

Kommentiert:

am 21 Sep. 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by