Making change with coins, problem (greedy algorithm)
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm trying to write (what I imagine is) a simple matlab script. I want to be able to input some amount of cents from 0-99, and get an output of the minimum number of coins it takes to make that amount of change. For example, if I put in 63 cents, it should give
coin = [2 1 0 3]
meaning: 2 quarters, 1 dime, 0 nickles, and 3 pennies
Here's where I am at now:
function[money] = change(money)
money = [quarter dime nickle penny];
%initial amounts of each coin
quarter = 0;
dime = 0;
nickle = 0;
penny = 0;
money = true;
while money>=0
if money >= 25
money = money - 25;
quarter = quarter + 1;
elseif money >= 10
money = money - 10;
dime = dime + 1;
elseif money >= 5
money = money - 5;
nickle = nickle + 1;
elseif money >= 1
money = money - 1;
penny = penny +1
end
end
clear;clc;
And then this is the error I get from matlab, "Line: 6 Column: 1 "penny" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable. A possible cause of this error is that you forgot to initialize the variable, or you have initialized it implicitly using load or eval."
What (probably very obvious) thing am I missing?
0 comentarios
Respuesta aceptada
Srinivas
el 2 de Mzo. de 2012
you are writing on to money again and you have an infinite loop
function[coins] = change12(money)
%initial amounts of each coin
quarter = 0;
dime = 0;
nickle = 0;
penny = 0;
% money = [quarter dime nickle penny]; money =[0 0 0 0]
while money>0 % while money>=0 makes an infinte loop
if money >= 25
money = money - 25;
quarter = quarter + 1;
elseif money >= 10
money = money - 10;
dime = dime + 1;
elseif money >= 5
money = money - 5;
nickle = nickle + 1;
elseif money >= 1
money = money - 1;
penny = penny +1;
end
end
coins = [quarter dime nickle penny];
0 comentarios
Más respuestas (3)
Geoff
el 2 de Mzo. de 2012
Have already voted on Srinivas' correct answer to your question, but this is for your interest and study:
function [coins] = change( money )
values = int32([25,10,5,1]);
remain = int32(money)
coins = zeros(1,4);
for v = 1:4
coins(v) = idivide(remain, values(v));
remain = mod(remain, values(v));
end
end
There's far less than can go wrong here, and you can even play with the priority of each denomination by reordering the values array. =)
-g-
0 comentarios
Walter Roberson
el 2 de Mzo. de 2012
You have the line
money = [quarter dime nickle penny];
when none of those variables have been initialized. MATLAB is analyzing and saying that the only way that could happen and it be valid is if you happen to have functions with those names.
You cannot use a variable name until after the variable has been initialized.
Image Analyst
el 2 de Mzo. de 2012
You might try returning the answers instead of money, which will be zero because you decremented it until it was zero:
function [quarter dime nickel penny] = change(money)
0 comentarios
Ver también
Categorías
Más información sobre Historical Contests en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!