Problem creating a cash register function

3 views (last 30 days)
Hey there,
I'm trying to write a simple function that acts as a cash register to figure out how much you're owed in Euro bills. The input is the price of the item and how much you give (e.g. moneyback(15, 200), meaning the price is 15 and you give them 200, it will give you one 100, one 50, one 20, and one 10). I found this code to loosely base mine off of and it seems to work for them but when I try to implement it in my code the final vector, which is supposed to tell you which kind of bills and how many you're owed, is all zeros and I'm not sure why.
I couldn't figure out how to explain the error without including my entire code, sorry about that.
function change = moneyback(price, given)
% set initial amounts of each type of bill
one_cent = 0;
two_cent = 0;
five_cent = 0;
ten_cent = 0;
twenty_cent = 0;
fifty_cent = 0;
one_euro = 0;
two_euro = 0;
five_euro = 0;
ten_euro = 0;
twenty_euro = 0;
fifty_euro = 0;
hundred_euro = 0;
twohun_euro = 0;
fivehun_euro = 0;
total = [one_cent two_cent five_cent ten_cent twenty_cent fifty_cent one_euro two_euro five_euro ten_euro twenty_euro fifty_euro hundred_euro twohun_euro fivehun_euro];
% find how much you're owed
changetotal = given-price
% loop to figure out how many bills that will take
% I only included bills down to five because no point adding cents when I
% cant get this to work yet
while changetotal > 0
if changetotal >= 500
fivehun_euro = fivehun_euro + 1;
changetotal = changetotal - 500;
elseif changetotal >= 200
twohun_euro = twohun_euro + 1;
changetotal = changetotal - 200;
elseif changetotal >= 100
hundred_euro = hundred_euro + 1;
changetotal = changetotal - 100;
elseif changetotal >= 50
fifty_euro = fifty_euro + 1;
changetotal = changetotal - 50;
elseif changetotal >= 20
twenty_euro = twenty_euro + 1;
changetotal = changetotal - 20;
elseif changetotal >= 10
ten_euro = ten_euro + 1;
changetotal = changetotal - 10;
elseif changetotal >= 5
five_euro = five_euro + 1;
changetotal = changetotal - 5;
end
change = total;
end
end

Accepted Answer

Chetan Bhavsar
Chetan Bhavsar on 3 Oct 2021
You forgot to udpate total variable
function change = moneyback(price, given)
% set initial amounts of each type of bill
one_cent = 0;
two_cent = 0;
five_cent = 0;
ten_cent = 0;
twenty_cent = 0;
fifty_cent = 0;
one_euro = 0;
two_euro = 0;
five_euro = 0;
ten_euro = 0;
twenty_euro = 0;
fifty_euro = 0;
hundred_euro = 0;
twohun_euro = 0;
fivehun_euro = 0;
total = [one_cent two_cent five_cent ten_cent twenty_cent fifty_cent one_euro two_euro five_euro ten_euro twenty_euro fifty_euro hundred_euro twohun_euro fivehun_euro];
% find how much you're owed
changetotal = given-price
% loop to figure out how many bills that will take
% I only included bills down to five because no point adding cents when I
% cant get this to work yet
while changetotal > 0
if changetotal >= 500
fivehun_euro = fivehun_euro + 1;
changetotal = changetotal - 500;
elseif changetotal >= 200
twohun_euro = twohun_euro + 1;
changetotal = changetotal - 200;
elseif changetotal >= 100
hundred_euro = hundred_euro + 1;
changetotal = changetotal - 100;
elseif changetotal >= 50
fifty_euro = fifty_euro + 1;
changetotal = changetotal - 50;
elseif changetotal >= 20
twenty_euro = twenty_euro + 1;
changetotal = changetotal - 20;
elseif changetotal >= 10
ten_euro = ten_euro + 1;
changetotal = changetotal - 10;
elseif changetotal >= 5
five_euro = five_euro + 1;
changetotal = changetotal - 5;
end
% update total here
total = [one_cent two_cent five_cent ten_cent twenty_cent fifty_cent one_euro two_euro five_euro ten_euro twenty_euro fifty_euro hundred_euro twohun_euro fivehun_euro];
change = total;
end
end
  2 Comments
Chetan Bhavsar
Chetan Bhavsar on 3 Oct 2021
will this work for you ?
Please click "accept this answer" if it fullfilled your requirement.
function change = moneyback(price, given)
% set initial amounts of each type of bill
one_cent = 0;
two_cent = 0;
five_cent = 0;
ten_cent = 0;
twenty_cent = 0;
fifty_cent = 0;
one_euro = 0;
two_euro = 0;
five_euro = 0;
ten_euro = 0;
twenty_euro = 0;
fifty_euro = 0;
hundred_euro = 0;
twohun_euro = 0;
fivehun_euro = 0;
total = [one_cent two_cent five_cent ten_cent twenty_cent fifty_cent one_euro two_euro five_euro ten_euro twenty_euro fifty_euro hundred_euro twohun_euro fivehun_euro];
% find how much you're owed
changetotal = given-price
% loop to figure out how many bills that will take
% I only included bills down to five because no point adding cents when I
% cant get this to work yet
while changetotal > 0
if changetotal >= 500
fivehun_euro = fivehun_euro + 1;
changetotal = changetotal - 500;
elseif changetotal >= 200
twohun_euro = twohun_euro + 1;
changetotal = changetotal - 200;
elseif changetotal >= 100
hundred_euro = hundred_euro + 1;
changetotal = changetotal - 100;
elseif changetotal >= 50
fifty_euro = fifty_euro + 1;
changetotal = changetotal - 50;
elseif changetotal >= 20
twenty_euro = twenty_euro + 1;
changetotal = changetotal - 20;
elseif changetotal >= 10
ten_euro = ten_euro + 1;
changetotal = changetotal - 10;
elseif changetotal >= 5
five_euro = five_euro + 1;
changetotal = changetotal - 5;
elseif changetotal >= 2
two_euro = two_euro + 1;
changetotal = changetotal - 2;
elseif changetotal >= 1
one_euro = one_euro + 1;
changetotal = changetotal - 1;
elseif round(changetotal,2) >= .5
fifty_cent = fifty_cent + 1;
changetotal = changetotal - .50;
elseif round(changetotal,2) >= .2
twenty_cent = twenty_cent + 1;
changetotal = changetotal - .20;
elseif round(changetotal,2) >= .1
ten_cent = ten_cent + 1;
changetotal = changetotal - .10;
elseif round(changetotal,2) >= .05
five_cent = five_cent + 1;
changetotal = changetotal - .05;
elseif round(changetotal,2) >= .01
one_cent = one_cent + 1;
changetotal = changetotal - .01;
elseif round(changetotal,2) == 0
break;
end
total = [one_cent two_cent five_cent ten_cent twenty_cent fifty_cent one_euro two_euro five_euro ten_euro twenty_euro fifty_euro hundred_euro twohun_euro fivehun_euro];
change = total;
end
end

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 3 Oct 2021
Every time through your while loop you overwrite the value of the change variable with the contents of the total variable. Neither of those variables change anywhere but on the last line of the loop, so change ends up with the initial contents of the total variable which is all 0's.
You have a lot of information stored in the variable names, which would make this code hard to extend. The way I'd do this is with a vector of bill denominations. I'll use US currency for this example.
denominations = [0.01 0.05 0.1 0.25 0.5 1 5 10];
Now if you wanted to make change for $37.84, describe for me how you would do that, step by step, using the variable denominations. Once you can describe the algorithm in words (which should probably include phrases like "for each", which is equivalent to the for keyword in MATLAB, "if" which is if, and/or "while" which is while) then write that algorithm as comments in your file. Finally implement each comment.
If you can get the written algorithm down but can't translate it into MATLAB code, add a comment showing your algorithm.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!