Design an algorithm and write the program for a machine that must give the current amount of change from rs500 for any amount of change costing less than rs 500. The program must specify the number and all notes and coins as possible. Assume purc
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/158004/image.png)
0 commentaires
Réponse acceptée
Stephen23
le 17 Mar 2016
Modifié(e) : Stephen23
le 17 Mar 2016
function chg = change(num)
den = [200,100,50,20,10,5,2,1,0.5,0.2,0.1,0.05];
vec = int32(100*den);
lft = int32(100*num);
tmp = zeros(size(vec),'int32');
for v = 1:numel(vec)
tmp(v) = idivide(lft, vec(v));
lft = mod(lft, vec(v));
end
chg(:,2) = den;
chg(:,1) = double(tmp);
end
You should be able to adapt its output to suit your homework. You will have to change the denominations to suit your requirements, and also consider how to display the output (hint: use fprintf). Currently the functions simply returns a matrix showing how many of each denomination are used:
>> change(499)
ans =
2 200
0 100
1 50
2 20
0 10
1 5
2 2
0 1
0 0.5
0 0.2
0 0.1
0 0.05
>> change(99)
ans =
0 200
0 100
1 50
2 20
0 10
1 5
2 2
0 1
0 0.5
0 0.2
0 0.1
0 0.05
>> change(1.75)
ans =
0 200
0 100
0 50
0 20
0 10
0 5
0 2
1 1
1 0.5
1 0.2
0 0.1
1 0.05
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Particle Swarm 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!