MATLAB: Making change with coins, problem (greedy algorithm)

coins

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?

Best Answer

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];