
February 20th, 2013, 02:08 PM
#1
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).
Any idea on how to fix it?
Code:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void change(double cents, int a[]);
int main()
{
double Dollars;
double cents;
// int a[]={25,10,5,1}; denominations of coins, could have been any numbers in DECREASING order.
int a[]={1000,500,100,25,10};
cout<<"Please enter an amount in the format $x.xx: ";
cin>> Dollars;
cents=100*Dollars;
cout<<endl<<Dollars<<" dollars is equal to "<<cents<<" cents"<<endl<<endl;
change(cents, a);
cin.get();
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
void change(double cents, int a[]){
int money=cents;
double coins[4];
cout<<"The change is respectively [";
for(int i=0;i<5;i++){
double remainder=money%a[i];
coins[i]=(moneyremainder)/a[i];
money=remainder;
cout<<coins[i]<<" ";
}
//cout<<"] quarters, dimes, nickels and pennies."<<endl<<endl;
cout<<"] 10's, 5's, 1's, quarters and dimes."<<endl<<endl;
}

February 20th, 2013, 02:11 PM
#2
Re: Coin/Money change code doesn't give exact change!
Shouldn't it give you two quarters and three dimes?

February 20th, 2013, 02:19 PM
#3
Re: Coin/Money change code doesn't give exact change!
yeah, it should, my bad. Unfortunately, it doesn't.
Last edited by math8; February 20th, 2013 at 02:22 PM.

February 20th, 2013, 02:30 PM
#4
Re: Coin/Money change code doesn't give exact change!
The first thing to do is figure out the rules for the algorithm using a pencil and paper. Figure out the logic then worry about the code.

February 20th, 2013, 03:57 PM
#5
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.
 design
 code
 test
 debug

February 20th, 2013, 05:10 PM
#6
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.

February 20th, 2013, 08:48 PM
#7
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.

February 20th, 2013, 08:56 PM
#8
Re: Coin/Money change code doesn't give exact change!
Originally Posted by math8
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.

February 20th, 2013, 09:32 PM
#9
Re: Coin/Money change code doesn't give exact change!
Originally Posted by math8
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.
Regards,
Paul McKenzie

February 20th, 2013, 09:41 PM
#10
Re: Coin/Money change code doesn't give exact change!
when you say "evenly" do you mean by solving: 25x+10x=y, where y is the cents value?

February 20th, 2013, 09:49 PM
#11
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".
Regards,
Paul McKenzie
Last edited by Paul McKenzie; February 20th, 2013 at 09:54 PM.

February 20th, 2013, 10:22 PM
#12
Re: Coin/Money change code doesn't give exact change!
hmmm, let see. Maybe something like:
Code:
for(int i=0; i<=n/10;i++)
{
if((n(i*10))%25 ==0)
{
x=i; //number of 10's
z=(n(i*10))/25 ; //number of 25's
break;
}
}

February 21st, 2013, 05:42 AM
#13
Re: Coin/Money change code doesn't give exact change!
You're overthinking it. Read what I said in post #8.

February 21st, 2013, 11:34 AM
#14
Re: Coin/Money change code doesn't give exact change!
Originally Posted by math8
hmmm, let see. Maybe something like:
Code:
for(int i=0; i<=n/10;i++)
{
if((n(i*10))%25 ==0)
{
x=i; //number of 10's
z=(n(i*10))/25 ; //number of 25's
break;
}
}
No code.
Pretend you don't know algebra, just basic arithmetic and common sense smarts.
How would a layperson attempt to solve the problem? Wouldn't the person try to take the most quarters they can, and then see if they can fill in the missing amount with just dimes?
If they find out they can fill in the missing amount with dimes, fine. If they can't, then they will try again the same step but use less quarters and fill in the missing amount with dimes. Repeat with the "less quarters, fill with dimes" until done.
That is basically GCDEF is stating, and why you don't need to know any C++, programming, computer science, or even algebra to come up with a solution.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; February 21st, 2013 at 11:53 AM.

February 20th, 2013, 10:08 PM
#15
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 n10 and see whether it is divisible by 25, if not, then take n20 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 nonnegative integer.
x: would be the number of 10's;
z=[n(x*10) ]/25 would be the number of 25's.
Last edited by math8; February 20th, 2013 at 10:59 PM.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
