Error with remainder (rem) and fix - change problem

4 ビュー (過去 30 日間)
B.M.
B.M. 2014 年 2 月 10 日
コメント済み: B.M. 2014 年 2 月 14 日
Hi all, I'm working through the following problem:
=====================================
Design an algorithm (i.e., write the structure plan) for a machine that must give the
correct amount of change from a $100 bill for any purchase costing less than $100. The
plan must specify the number and type of all bills and coins in the change, and should
in all cases give as few bills and coins as possible. (If you are not familiar with dollars
and cents, use your own monetary system.)
=====================================
I have written a code that should do this, but occasionally have problems with the output. The problems are
(1) Sometimes, for example when the amount of change ends with .30, "Nickels" will be skipped and go to pennies. That is, instead of 1 quarter and 1 nickel, my output will say 1 quarter and 5 pennies.
EXAMPLE OUTPUT:
In what amount is the purchase? 90.70
Change given is: $9.3
Given in the form of 0 fifties, 0 twenties, 0 tens, 1 fives, 4 ones, 1 quarters, 0 dimes, 0 nickels, and 5 pennies.
(2) Sometimes, even when "Nickels" works (such as the case when the change ends in .40), the amount "Pennies" becomes something other than zero--in my case, 5.684341886080801e-13.
EXAMPLE OUTPUT:
In what amount is the purchase? 90.60
Change given is: $9.4
Given in the form of 0 fifties, 0 twenties, 0 tens, 1 fives, 4 ones, 1 quarters, 1 dimes, 1 nickels, and 5.6843e-013 pennies.
Here is my code. It overwrites "Remainder" throughout.
=====================================
Bill=100;
Purchase=input('In what amount is the purchase? ');
if Purchase==100
disp('No change given.')
else Purchase<Bill
Remainder=Bill-Purchase;
Fifties=fix(Remainder/50);
Remainder=rem(Remainder, 50);
Twenties=fix(Remainder/20);
Remainder=rem(Remainder, 20);
Tens=fix(Remainder/10);
Remainder=rem(Remainder, 10);
Fives=fix(Remainder/5);
Remainder=rem(Remainder, 5);
Ones=fix(Remainder/1);
Remainder=rem(Remainder, 1);
Quarters=fix(Remainder/0.25);
Remainder=rem(Remainder, 0.25);
Dimes=fix(Remainder/0.10);
Remainder=rem(Remainder, 0.10);
Nickels=fix(Remainder/0.05);
Remainder=rem(Remainder, 0.05);
Pennies=Remainder/0.01;
disp(['Change given is: $' num2str(Bill-Purchase)])
disp(['Given in the form of ' num2str(Fifties) ' fifties, ' num2str(Twenties) ' twenties, ' num2str(Tens) ' tens, ' num2str(Fives) ' fives, ' num2str(Ones) ' ones, ' num2str(Quarters) ' quarters, ' num2str(Dimes) ' dimes, ' num2str(Nickels) ' nickels, and ' num2str(Pennies) ' pennies.'])
end

採用された回答

Roger Stafford
Roger Stafford 2014 年 2 月 10 日
I suspect your difficulty is due, not to the 'rem' function, but to the impossibility of representing the fractional values 0.10 and 0.05 exactly. This is a well-known characteristic of binary floating point numbers in all binary computers.
The remedy for you is to immediately multiply the number you receive as an input by 100 and round to an integer. In other words, do your problem entirely in pennies, not dollars.
Purchase=input('In what amount is the purchase? ');
Pennies = round(100*Purchase);
Then revise the rest to deal in pennies, not dollars.
  3 件のコメント
Roger Stafford
Roger Stafford 2014 年 2 月 11 日
No, you need to change to pennies BEFORE you do the subtraction
Remainder=Bill-Purchase;
and you left out the 'round'. Both are essential here to avoid fractional parts of cents in the computations that follow.
Do this:
Bill=10000; % In cents
Purchase=input('In what amount is the purchase? ');
Cents = round(100*Purchase); % Ensure a whole number of cents
Remainder = Bill-Cents;
Fifties=fix(Remainder/5000);
etc.
B.M.
B.M. 2014 年 2 月 14 日
That worked, thanks Roger!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by