필터 지우기
필터 지우기

Making change with coins, problem (greedy algorithm)

조회 수: 7 (최근 30일)
Edward
Edward 2012년 3월 2일
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?

채택된 답변

Srinivas
Srinivas 2012년 3월 2일
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];

추가 답변 (3개)

Geoff
Geoff 2012년 3월 2일
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-

Walter Roberson
Walter Roberson 2012년 3월 2일
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.
  댓글 수: 1
Edward
Edward 2012년 3월 2일
function[money] = change(money)
%initial amounts of each coin
quarter = 0;
dime = 0;
nickle = 0;
penny = 0;
money = [quarter dime nickle penny];
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;
It still does not work. I tried to run it, and matlab couldn't do it. Is there something else I may be missing?

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2012년 3월 2일
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)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by