problem with money denomination 
Author Message
 problem with money denomination

Hi
I am writing a program that read in an amount of money under
a R100-00( the R stands for Rand, South African currency.) and
then calculates the least amount of denominations to issue.
The problem comes when I try to issue for example, 0.89 cents
The program will just issue 0.88 cents.

I am really stuck at this problem for 2 days. Can anybody
please assist me with solving this problem.
Attached is the code to have a look at
Thanks

program MoneyChange;
(* This programs reads in an amount of money and calculates the least value
of denominations in notes and coins to issue. *)

Uses crt;

Var money, count:real;

BEGIN
  clrscr;
  write ('Enter amount of money to evaluate ');
  readln (money);
  count := 0;
  while money >= 50.00 do
    begin
       money := money - 50.00;
       count := count + 1;
    end;
  writeln ('R50 note[s] : ', count:5:0);
  count := 0;
  while money >= 20.00 do
    begin
       money := money - 20.00;
       count := count + 1;
    end;
  writeln ('R20 note[s] : ', count:5:0);
  count := 0;
  while money >= 10.00 do
    begin
       money := money - 10.00;
       count := count + 1;
    end;
  writeln ('R10 note[s] : ', count:5:0);
  count := 0;
  while money >= 5.00 do
    begin
       money := money - 5.00;
       count := count + 1;
    end;
  writeln ('R5 coin[s] :  ', count:5:0);
  count := 0;
  while money >= 2.00 do
    begin
       money := money - 2.00;
       count := count + 1;
    end;
  writeln ('R2 coin[s] :  ', count:5:0);
  count := 0;
  while money >= 1.00 do
    begin
       money := money - 1.00;
       count := count + 1;
    end;
  writeln ('R1 coin[s] :  ', count:5:0);
  count := 0;
  while money >= 0.50 do
    begin
       money := money - 0.50;
       count := count + 1;
    end;
  writeln ('50c coin[s] : ', count:5:0);
  count := 0;
  while money >= 0.20 do
     begin
       money := money - 0.20;
       count := count + 1;
     end;
  writeln ('20c coin[s] : ', count:5:0);
  count := 0;
  while money >= 0.10 do
    begin
       money := money - 0.10;
       count := count + 1;
    end;
  writeln ('10c coin[s] : ', count:5:0);
  count := 0;
  while money >= 0.05 do
    begin
       money := money - 0.05;
       count := count + 1;
    end;
  writeln ('5c coin[s] :  ', count:5:0);
  count := 0;
  while money >= 0.02 do
     begin
       money := money - 0.02;
       count := count + 1;
     end;
  writeln ('2c coin[s] :  ', count:5:0);
  count := 0;
  while money >= 0.01 do
     begin
       money := money - 0.01;
       count := count + 1;
     end;
   writeln ('1c coin[s] :  ', count:5:0);
readln;
END.



Thu, 27 Oct 2005 03:11:44 GMT  
 problem with money denomination

Quote:

> Hi
> I am writing a program that read in an amount of money under
> a R100-00( the R stands for Rand, South African currency.) and
> then calculates the least amount of denominations to issue.
> The problem comes when I try to issue for example, 0.89 cents
> The program will just issue 0.88 cents.

> I am really stuck at this problem for 2 days. Can anybody
> please assist me with solving this problem.
> Attached is the code to have a look at
> Thanks

> program MoneyChange;
> (* This programs reads in an amount of money and calculates the least value
> of denominations in notes and coins to issue. *)

> Uses crt;

> Var money, count:real;

> BEGIN
>   clrscr;
>   write ('Enter amount of money to evaluate ');
>   readln (money);
>   count := 0;
>   while money >= 50.00 do
>     begin
>        money := money - 50.00;
>        count := count + 1;
>     end;
>   writeln ('R50 note[s] : ', count:5:0);
>   count := 0;
>   while money >= 20.00 do
>     begin
>        money := money - 20.00;
>        count := count + 1;
>     end;
>   writeln ('R20 note[s] : ', count:5:0);
>   count := 0;
>   while money >= 10.00 do
>     begin
>        money := money - 10.00;
>        count := count + 1;
>     end;
>   writeln ('R10 note[s] : ', count:5:0);
>   count := 0;
>   while money >= 5.00 do
>     begin
>        money := money - 5.00;
>        count := count + 1;
>     end;
>   writeln ('R5 coin[s] :  ', count:5:0);
>   count := 0;
>   while money >= 2.00 do
>     begin
>        money := money - 2.00;
>        count := count + 1;
>     end;
>   writeln ('R2 coin[s] :  ', count:5:0);
>   count := 0;
>   while money >= 1.00 do
>     begin
>        money := money - 1.00;
>        count := count + 1;
>     end;
>   writeln ('R1 coin[s] :  ', count:5:0);
>   count := 0;
>   while money >= 0.50 do
>     begin
>        money := money - 0.50;
>        count := count + 1;
>     end;
>   writeln ('50c coin[s] : ', count:5:0);
>   count := 0;
>   while money >= 0.20 do
>      begin
>        money := money - 0.20;
>        count := count + 1;
>      end;
>   writeln ('20c coin[s] : ', count:5:0);
>   count := 0;
>   while money >= 0.10 do
>     begin
>        money := money - 0.10;
>        count := count + 1;
>     end;
>   writeln ('10c coin[s] : ', count:5:0);
>   count := 0;
>   while money >= 0.05 do
>     begin
>        money := money - 0.05;
>        count := count + 1;
>     end;
>   writeln ('5c coin[s] :  ', count:5:0);
>   count := 0;
>   while money >= 0.02 do
>      begin
>        money := money - 0.02;
>        count := count + 1;
>      end;
>   writeln ('2c coin[s] :  ', count:5:0);
>   count := 0;
>   while money >= 0.01 do
>      begin
>        money := money - 0.01;
>        count := count + 1;
>      end;
>    writeln ('1c coin[s] :  ', count:5:0);
> readln;
> END.

Was it your intent to have no fraction on your printouts ?

--
Warning: poster of this email may be under the influence of
sleep inducing documents !



Thu, 27 Oct 2005 09:10:22 GMT  
 problem with money denomination

schreibt:

Quote:
>The problem comes when I try to issue for example, 0.89 cents
>The program will just issue 0.88 cents.

You mean 0.89 Rands?

This is a common problem with floating point numbers and arithmetic. Some
programming languages have an Currency data type, which allows for the correct
handling of numbers with a limited number of fractional digits.

You'll have less problems when you convert the amount into cents (or whatever
makes whole numbers) and use integer variables and arithmetic for all
calculations.

DoDi



Thu, 27 Oct 2005 15:11:04 GMT  
 problem with money denomination


Quote:

> schreibt:

> >The problem comes when I try to issue for example, 0.89 cents
> >The program will just issue 0.88 cents.

> You mean 0.89 Rands?

> This is a common problem with floating point numbers and arithmetic. Some
> programming languages have an Currency data type, which allows for the
correct
> handling of numbers with a limited number of fractional digits.

> You'll have less problems when you convert the amount into cents (or
whatever
> makes whole numbers) and use integer variables and arithmetic for all
> calculations.

> DoDi

Yes, I actually meant 0.89 Rands. I will try to convert the amounts into
cents.
and so how it goes. Thanks for the help.
Marlo
1st Year Comp Science student


Thu, 27 Oct 2005 20:53:10 GMT  
 problem with money denomination


Sat, 10 May 2003 21:11:44 :-

Quote:
>I am writing a program that read in an amount of money under
>a R100-00( the R stands for Rand, South African currency.) and
>then calculates the least amount of denominations to issue.
>The problem comes when I try to issue for example, 0.89 cents
>The program will just issue 0.88 cents.
>Var money, count:real;

Type real is inexact, like all floats.  In TP>3 / BP, it is deprecated,
too. being slow; use double instead, normally.  See via <URL:http://www.
merlyn.demon.co.uk/Pascal.htm>.  This might be a candidate for mini-FAQ
sec 3 or 3.1 (FAQ 3, before 3.1, should maybe be 3.0?).  AFAICS, it's
not really in TSFAQP, though #133, #143 are related.  Web/news FAQ
http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm #3 deals
with it, briefly; permission to copy should be available.

Change, however, to working in cents, with variables of type integer or
word or longint.

You should be able to combine all of those one-denomination loops into a
looped loop.

The problem is a subset of that in jad_9514.pas.

--

  <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
  <URL:http://www.merlyn.demon.co.uk/clpb-faq.txt>   RAH Prins : c.l.p.b mFAQ;
  <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.



Thu, 27 Oct 2005 21:53:31 GMT  
 problem with money denomination
[snip helpfull solution]

Quote:

> Yes, I actually meant 0.89 Rands. I will try to convert the amounts into
> cents.
> and so how it goes. Thanks for the help.
> Marlo
> 1st Year Comp Science student

Okay, then I'll provide the explanation of why this happes in short
terms to you:

By using the type REAL or any floating point datatype (like EXTENDED)
you only have a limited number of bits which are stored in some exponent
notation. For example a 32 bit floating point number could be stored
like this:

1  Bit sign (+/-)
23 Bit mantissa (something like the value of the number)
8  Bit expnent based on 2, means two^this exponent

The exponent somehow determines where you have to think the comma in the
mantissa. The values after the comma are fractions added up, so there
always is some space between two neighbouring numbers which cannot be
covered by this sheme so the next possible number (the one whith the
minimum failure) is stored.

I'm no mathematician, I'm only student of some sort of computer science
myself, but I'm short before diploma ;-) So please forgive sligh
inaccurancied - I think at least the shema is correct.

Greetings

Markus



Fri, 28 Oct 2005 23:33:58 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. money, money, money

2. money money money money... ez and quick - money.txt (0/1)

3. money money money money... ez and quick - money.txt (1/1)

4. MONEY MONEY

5. Great Chance To Make Money!

6. 'Money wanted' algorithm (OOP version)

7. Make 300% profit or double your money in a month vGLFCbBJ

8. Make 300% profit or double your money in a month DHGwJCEw

9. Make 300% profit or double your money in a month FMAWDSGE

10. MAKE MONEY

11. DBEdits ($ (Money))

 

 
Powered by phpBB® Forum Software