Need help fixing my code to calculate the most efficient form of euro change to give a customer (greedy algorithm)
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
bill = 1 + (200-1) .* rand(1,1)
bill = round(bill,2)
pay = input("How much cash did the customer give you?")
change1 = pay-bill
%calculates the change the customer should recieve after handing you a certain amount of money
function[efficient_change] = change(change1)
initial amounts of each coin
one_cent = 0;
two_cents = 0;
five_cents = 0;
ten_cents = 0;
twenty_cents = 0;
fifty_cents = 0;
one_eurocoin = 0;
two_eurocoin = 0;
five_eurobill = 0
ten_eurobill = 0;
twenty_eurobill = 0;
fifty_eurobill = 0;
onehundred_eurobill = 0;
fivehundred_eurobill = 0;
change = [one_cent two_cent five_cents ten_cents twenty_cents fifty_cents one_eurocoin two_eurocoin five_eurobill ten_eurobill twenty_eurobill fifty_eurobill onehundred_eurobill fivehundred_eurobill];
change =[0 0 0 0 0 0 0 0 0 0 0 0 0 0]
while money>0
while money>=0
makes an infinte loop
if money >= 0.01
money = money - 0.01;
one_cent = one_cent + 1;
elseif money >= 0.02
money = money - 0.02;
two_cents = two_cents + 1;
elseif money >= 0.05
money = money - 0.05;
five_cents = five_cents + 1;
elseif money >= 0.10
money = money - 0.10;
ten_cents = ten_cents +1;
elseif money >= 0.20
money = money - 0.20;
twenty_cents = twenty_cents + 1;
elseif money >= 0.50
money = money - 0.50;
fifty_cents = fifty_cents + 1;
elseif money >= 1.00
money = money - 1.00;
one_eurocoin = one_eurocoin +1;
end
end
coins = [one_cent two_cent five_cents ten_cents twenty_cents fifty_cents one_eurocoin two_eurocoin];
bills = [five_eurobill ten_eurobill twenty_eurobill fifty_eurobill onehundred_eurobill fivehundred_eurobill]
disp("Give them back" (bills,coins))
end
end
%supposed to calulate the most efficient change in coins and billls (but not sure what to do)
0 commentaires
Réponses (1)
Govind KM
le 26 Juil 2024
Hi Nicky,
It seems to me that you are trying to calculate the change to be given to a customer, randomly generating the bill amount to be paid and taking the cash given by them as input.
A general structure you can follow for your function to calculate change efficiently without the need to use a while loop is:
function[efficient_change]=change(money)
%Initialise the amount of each coin or bill using a single array of
%size equal to the number of different denominations
efficient_change=zeros(1,n);
%Here n is the number of different denominations
%Calculate the number of bills of each denomination needed using math,
%going from higher to lower value. An example for the five euro bill is
efficient_change(4)=floor(money/5);
money=money-efficient_change(4)*5;
%Assuming the five euro bill is 4th in the efficient_change array
end
Hope this is useful.
0 commentaires
Voir également
Catégories
En savoir plus sur Instrument Control Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!