To summarize the algorithm provided by Lazarus, you just add as many of the biggest coin you can. Then repeat for the next biggest until you have the amount.
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
Actually the OP more-or-less did this. The algorithm does not work. Let's take what the program's output is supposed to do in this example:
User enters $4.63. Your output should be:
The least number of coins required to make up $4.64 is 4. The coins needed are:
1 Twonie $2.00
2 Jonnies $2.62
1 Penny $0.01
____________
$4.63
Now with your algorithm:$2.00 is largest, and it goes into $4.63 twice
Remaining change is $0.63
Since Joonies can't divide into $0.63, it's forced to go to the next one, which is the quarter
Do you see the logic? This is what the OP meant when he/she said that "Joonies make it harder". There must be a simple solution to this, but at the moment my brain is not working.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
The code you posted seems to work fine for me... I get
Please enter the amount of money you would like changed: 4.63
2 Twonie(s)
0 Jonnie(s)
0 Loonie(s)
2 Quarter(s)
1 Dimes
0 Nickel(s)
3 Penny(ies)
thats:
4.00 2 Twonie(s)
0 Jonnie(s)
0 Loonie(s)
.50 2 Quarter(s)
.10 1 Dimes
0 Nickel(s)
.03 3 Penny(ies)
-----
4.63
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Actually, my brain wasn't really functioning either, now that I look at it. The no-brainer algorithm we all jumped on works so long as the coins are multiples of each other, or have a fairly high greatest denominator.
A more complicated algorithm would be about as follows:
1. Take the largest coin possible and subtract it. Now find the best way of creating the remainder, recursively.
2. Then take the next best option from the original amount, and recursively find it's best. If at any point, the best cannot be better than a solution already found, break. Compare this to the result for part 1 and keep the better solution.
3. Repeat 2 as feasible. In this instance, you'd only have to do it twice (once for toonies, once for jonnies), since a $2 is always better than 2x$1.
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
Seems to me like it's a variation of the knapsack problem.
http://en.wikipedia.org/wiki/Knapsack_problem
One way would be
- all permutations of 1 coin
- all permutations of 2 coins
- all permutations of 3 coins
etc
For each permutation, add them up and see if they match the total you want.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
I'm having trouble also coming with my own solution.
It would be great if someone would post an example of:
all permutations of 2 coins
I'll keep working on it.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
>Why not make it simple.
Why not read the entire thread so that you actually know the problem that's going on?
The OP's code worked fine for that. As stated previously, the algorithm works as long as the amounts are multiples of each other. When they're not, you have to use an optimizing algorithm to find the best method.
>does the modulous(sp?) operator only work on integers?
It works on any number type as far as I know.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>does the modulous(sp?) operator only work on integers?
It works on any number type as far as I know.
The modulus operator can be use only with integer operands
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
>The modulus operator can be use only with integer operands
Right. If it were a float there'd be no remainder.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
A simpler way would be to convert each and everything to intger and do away with the floating point confusion and havoc.
Penny - 0.01
Scaled Penny - 1 ( scaled by factor of 100 )
And in the same way, you scale the amount entered by the user and your calculations would be made simpler.
Also if you want an equivalent of modulus operator for floats look for [search]fmod[/search] but keep in mind that it operates only with floating point numbers.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
You need 3 coin arrays:
A) The best coin solution so far
B) The last permutation of the coin solutions
C) The next (working) permutation being calculated.
1) First solution (A) is your initial run with 2 twonies.
2) Load that solution also into (B).
3) Load the next permutation based on (B) into (C)
4) Load (B) with (C) for the next permutation.
5) If (C) yields a better solution, replace (A).
6) Generate next permutation at step #3
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
@ amthwee
Thank you for those pdf files full with information.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218