Jump to content

need help in money change problem ...

- - - - -

  • Please log in to reply
6 replies to this topic

#1
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
Posted Image

how can i make the number of change to works .. for example if the change is $0.80 the number of change will appear 4 for $0.20 and the other like $0.10 , $0.50 and $1.00 will appear to be 0 ?

#2
anotheruser

anotheruser

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
there are multiple ways to express a value as the sum multiples of other values. An example: $0.50 could be one piece of $0.50 or 5 pieces of $0.10.
Assuming you want to give out the higher value coins first, you need to take the original value and, starting with the coin with the highest value, find out how often it fits in, then take the rest to the next step. Example: $1.60.
$1.00 fits in 1 time, $0.60 remaining.
$0.50 fits in 1 time, $0.10 remaining.
$0.20 fits in 0 times, $0.10 remaining.
$0.10 fits in 1 time, $0.00 remaining.
if anything other than zero remains in the end, something is wrong with the money.

#3
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
yea exactly wht u are trying to say ... how to do ???

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I allready posted you that.
double change = ans - total;
int piecesOf1 = (int) change;
change -= piecesOf1;
int piecesOf05 = (int) (change/0.5);
change -= (piecesOf05 *0.5);
int piecesOf02 = (int) (change/0.2);
change -= (piecesOf02 *0.2);
int piecesOf01 = (int) (change/0.1);
change -= (piecesOf01 *0.1);

But this return 0.80 as 0.5+0.2+0.1 but you want it as 0.2+0.2+0.2+0.2 for some reason...

#5
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
sorry oxano i think i misunderstood ... yea i want the thing to be least number of change ... yea you are right :) but i now facing problem when the change is $0.70 ..the code u provide will appear to have 1 $0.50 and 1 $0.10 the rest 0 ... do you know how to solve ?

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
WTF since when does C# say that 0.2 / 0.2 != 1 :confused:

I have now instead of using 0.2 etc in the code replaced that by Double.parse("0.2")
Just using doubles now.
code:

                double change = Double.Parse("0.7");

                double piecesOf1 = Math.Floor(change);

                change -= piecesOf1;

                double piecesOf05 = Math.Floor(change/Double.Parse("0.5"));

                change -= (piecesOf05 *Double.Parse("0.5"));

                double piecesOf02 = Math.Floor(change/Double.Parse("0.2"));

                change -= (piecesOf02 *Double.Parse("0.2"));

                double piecesOf01 = Math.Floor(change/Double.Parse("0.1"));

                change -= (piecesOf01 *Double.Parse("0.1"));
And yet he calculates 0.2/0.2 as something that's smaller than 1.

You can get around it by doing
double change = ans - total + 0.001

But this is rediculous.

#7
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
wow ... oxano THANK YOU SO MUCH MAN!!!! you are a real pro in programming that doesnt give up on noob. Thanks my program can work ardy.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users