If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Coin/Money change code doesn't give exact change!

My coin/money change code works when there can be an exact change each time, i.e. when the 1 cent option is available. However, when the change options are only $10, $5, $1, 25 cents and 10 cents, it does not give me what I want for instance, I wanted to get change for $237.80, I was expecting to get:

23 10's, one 5, two 1's and 8 dimes. However, the code below is giving me 23 10's, one 5, two 1's and 3 quarters (there is no option left for the 5 remaining cents).

Re: Coin/Money change code doesn't give exact change!

What debugging have you done? You don't mention this. If the code doesn't perform as per the design then you need to debug the code. If the code is performing as per the design, then the design needs modifying and the code updated to reflect the new design.

Re: Coin/Money change code doesn't give exact change!

Originally Posted by 2kaud

What debugging have you done? You don't mention this. If the code doesn't perform as per the design then you need to debug the code. If the code is performing as per the design, then the design needs modifying and the code updated to reflect the new design.

- design
- code
- test
- debug

You can look at his code and see that algorithm isn't correct. That's why I said he needs to actually figure out how he's going to approach it before he tries to code it.

Re: Coin/Money change code doesn't give exact change!

I think the code is performing how it should, I think the problem is behind the logic (I guess that's the design). I am hoping to get 23 10's, one 5, two 1's ,2 quarters and 3 dimes.

The logic that I had was to use as many higher denominations in an effort to use the least number of coins. Let say, all denominations were available, then by using this logic, I would have at the end(for the cents), 3 quarters and 1 nickel, (i.e. 4 coins total), as opposed to 2 quarters and 3 dimes (5 coins total). However, in this case, it doesn't work since nickels and pennies aren't available.

Re: Coin/Money change code doesn't give exact change!

I think the code is performing how it should, I think the problem is behind the logic (I guess that's the design). I am hoping to get 23 10's, one 5, two 1's ,2 quarters and 3 dimes.

The logic that I had was to use as many higher denominations in an effort to use the least number of coins. Let say, all denominations were available, then by using this logic, I would have at the end(for the cents), 3 quarters and 1 nickel, (i.e. 4 coins total), as opposed to 2 quarters and 3 dimes (5 coins total). However, in this case, it doesn't work since nickels and pennies aren't available.

I am not sure in which direction I should go.

As I said, stop thinking in terms of programming and start thinking in terms of logic. Basically, if the number remaining after you take out the quarters isn't divisible by 10, you took out too many quarters.

Re: Coin/Money change code doesn't give exact change!

I think the code is performing how it should, I think the problem is behind the logic (I guess that's the design). I am hoping to get 23 10's, one 5, two 1's ,2 quarters and 3 dimes.

You're wasting time thinking about the dollars. That is simple and can be done by "greedy" logic -- take as many 10's, 5's and 1's to make the dollar total.

You should concentrate on the cents value. Given a cents value, can you divide it evenly into 25 cent and 10 cent portions? And if so, how would you do it? And you don't need C++ code to figure it out, as this is a general logic question.

Re: Coin/Money change code doesn't give exact change!

Originally Posted by math8

when you say "evenly" do you mean by solving: 25x+10x=y, where y is the cents value?

You have an unlimited number of quarters and dimes to grab from a pot. You have to make a total value of n cents. Can you take those quarters and dimes and create n cents with it? If so, how would you do it in a logical, consistent manner? Included in this solution is the "I can't do it" result, meaning that n can't be broken down into quarters and dimes evenly.

Forget about algebra for the moment -- write it out on paper if you have to and figure out a way of doing this. It is simply stated, so I don't know how much more simpler the problem can be presented.

Anyway, how was your formula going to work? The number of dimes and number of quarters are not necessarily the same value, yet you represented them both with "x".

Re: Coin/Money change code doesn't give exact change!

I wasn't sure what you meant by "evenly". But first of, intuitively, for this problem to make sense , the "cents part" n must be either 0 or >=10 and at least n must be a multiple of 5 .
So the only "logic thing" I can think of doing, is to check if n is divisible by 25, if not, take n-10 and see whether it is divisible by 25, if not, then take n-20 and check whether it is is divisible by 25, etc..

In other words, I would solve:
minimize x
such that n-(x*10) modulo 25 = 0.
x non-negative integer.

x: would be the number of 10's;
z=[n-(x*10) ]/25 would be the number of 25's.