Amount of coin change

Posted on 2005-04-12
Last Modified: 2010-05-18
Topic Write a program that tells what coins to give out for any amount of change from 1 cent to 99 cent. For example, if the amount is 86 cents, the ouput is like
86 cents can be given as
3 quarters, 1 dime, 1 penny

should use this function
void compute_coin (int coin_value, int& number, int& amount_left);

Can someone help me with this program, i dont know how to start it.
Question by:cuong5985
    LVL 32

    Expert Comment

    This sounds like homework, so I can't do it for you. But I can give you a few hints:

    We can assume the question is to give out change with the least number of coins. For example, 86 cents could also be given out as 86 pennies, or as 8 dimes and 6 pennies, etc.

    Given that, you could start by seing how many of the largest coin (i.e. 25 cents) would fit within 86 cents (hint: 86/3 => 3 quarters). That leaves 11 cents, then you repeat for next largest coin, and so on.

    Hope i did not give too much help.
    LVL 9

    Accepted Solution

    just a small correction:
    >> hint: 86/3 => 3 quarters
    should be 86/25 => 3 quarters
    and if you want to be more picky, there are half-dollar coins too... although they are not used very often mainly because of their size and weight.

    r-k gave you the high level algo. here's my interpretation of the parameters.
    coin_value: pretty straight fwd - 1 for penny, 5 for nickel, 10 for dime...
    number: the max number of coins that can fit in the given amount
    amount_left: the given amount. when we subtract coin_value*number, this will be interpreted as amount_left
    #define QUARTER 25
    amount_left = 86;
    compute_coin(QUARTER, number, amount_left);
    //at this point, number = 3 and amount_left = 11.
    #undef QUARTER
    #undef HALF_DOLLAR  //etc...
    //end of code
    LVL 39

    Expert Comment

    An enumeration may be a good helper:

    enum Coin { DOLLAR = 100, QUARTER = 25, DIME = 10, NICKEL = 5, PENNY = 1, NUM_COINS = 5 };

    Your main would be like that:

    int main()
          double   input = 1.;
          int         cents;   // total
          int         dollars;
          int         quarters;
          int         dimes;
          int         pennies;
          while (true)
               // get input (cout/cin)
               // break if input less or equal 0.
               // convert dollar input to cents
               // check how many dollar coins could be given out
               // subtract cent equivalent to given dollar coins from cents
               // check how many quarter coins could be given out
               // subtract cent equivalent to given quarter coins from cents
               // do same for dime, nickel
               // remainder in cents is pennies          
               // output the result
               // you could have two strings for each coin (singular and plural)
               // you could make string arrays where NUM_COINS is the size of the arrays
               // if a coin value is 0 you should suppress output


    Regards, Alex
    LVL 11

    Expert Comment

    How about the 50 cent coins.  It could be 3 quarters or a half dollar and a quarter.  Anyway, the principle is the same whatever the coinage.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now