CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Feb 2013
    Posts
    36

    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]=(money-remainder)/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; 
            
                 
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

    Shouldn't it give you two quarters and three dimes?

  3. #3
    Join Date
    Feb 2013
    Posts
    36

    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 03:22 PM.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

    Quote Originally Posted by 2kaud View Post
    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.

  7. #7
    Join Date
    Feb 2013
    Posts
    36

    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.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

    Quote Originally Posted by math8 View Post
    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.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by math8 View Post
    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

  10. #10
    Join Date
    Feb 2013
    Posts
    36

    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?

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by math8 View Post
    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 10:54 PM.

  12. #12
    Join Date
    Feb 2013
    Posts
    36

    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.
    Last edited by math8; February 20th, 2013 at 11:59 PM.

  13. #13
    Join Date
    Feb 2013
    Posts
    36

    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;
         }
    }

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

    You're overthinking it. Read what I said in post #8.

  15. #15
    Join Date
    Feb 2013
    Posts
    36

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

    alright, but aside from that, what is wrong with what I wrote before?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured