MATLAB: Min- imum number of coins required to pay

numberofcoins

Suppose that you are working as a cashier in a supermarket and there are
no banknotes left in the cash box. Therefore, for each transaction, you must pay the
remainder of the money by coins.
Write a function numberOfCoins(totalAmount, paidAmount) that returns the min-
imum number of coins required to pay the remainder of the money to the customer.
If the customer pays less than the total amount of transaction, the function should
give the following error message:
The customer paid less than the total amount of the transaction
and abort. Note that you have sucient amounts of 1kr, 5kr, 10kr, 25kr, 50kr, 1TL
coins in the cash box.
The following table shows a possible list of outputs of the function numberOfCoins.
Function call Output
numberOfCoins(99.12, 100) 6
numberOfCoins(17.95, 20) 3
numberOfCoins(10, 10) 0
numberOfCoins(3.99, 3.5) Error
Table 1: A list of possible outputs of the function numberOfCoins
For instance, if the total transaction is 99.12TL and the customer gives you 100TL,
you must pay 1x50kr, 1x25kr, 1×10 kr and 3x1kr coins to the customer (6 coins in
total).
I tried like this but i couldnt:
function coins=numberOfCoins(totalAmount, paidAmount)
totalAmount=input('totalAmount: ');
paidAmount=input('paidAmount: ');
if paidAmount>=totalAmount
transaction = paidAmount - totalAmount;
while transaction >= 1
transaction = transaction - 1.00;
numberOfCoins = numberOfCoins + 1;
end
if transaction >= 0.50
transaction = transaction - 0.50;
numberOfCoins = numberOfCoins + 1;
elseif transaction >= 0.25
transaction = transaction - 0.25;
numberOfCoins = numberOfCoins + 1;
elseif transaction >= 0.10
transaction = transaction - 0.10;
numberOfCoins = numberOfCoins + 1;
elseif transaction >= 0.05
transaction = transaction - 0.05;
numberOfCoins = numberOfCoins + 1 ;
elseif transaction >= 0.01
transaction = transaction - 0.01;
numberOfCoins = numberOfCoins + 1;
end
end
end

Best Answer

You have some issues in your if-else structure, but also you have a numerical problem - use uint datatype to avoid this:
[NumCoins1, UniqueCoinNumber1, CoinsCollection1] = numberOfCoins(99.12, 100)
[NumCoins2, UniqueCoinNumber2, CoinsCollection2] = numberOfCoins(17.95, 20)
[NumCoins3, UniqueCoinNumber3, CoinsCollection3] = numberOfCoins(10, 10)
[NumCoins4, UniqueCoinNumber4, CoinsCollection4] = numberOfCoins(3.99, 3.5)
function [NumCoins, UniqueCoinNumber, CoinsCollection] = numberOfCoins(totalAmount, paidAmount)
% totalAmount=input('totalAmount: ');
% paidAmount=input('paidAmount: ');
coins = [];
if paidAmount<totalAmount
disp('The paid amount is less then the total amount')
NumCoins = NaN;
UniqueCoinNumber = NaN;
CoinsCollection = NaN;
else
transaction = paidAmount - totalAmount;
transaction = uint16(transaction * 100);
while transaction > 0
if transaction >= 100
transaction = transaction - 100;
coins = [coins 1];
elseif transaction >= 50
transaction = transaction - 50;
coins = [coins 0.5];
elseif transaction >= 25
transaction = transaction - 25;
coins = [coins 0.25];
elseif transaction >= 10
transaction = transaction - 10;
coins = [coins 0.1];
elseif transaction >= 5
transaction = transaction - 5;
coins = [coins 0.05];
elseif transaction >= 1
transaction = transaction - 1;
coins = [coins 0.01];
end
end
NumCoins = numel(coins);
UniqueCoinNumber = numel(unique(coins));
CoinsCollection = coins;
end
end