Amount of coin change

Posted on 2005-04-12
Medium Priority
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

ID: 13769327
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.

Accepted Solution

jhshukla earned 150 total points
ID: 13769877
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

ID: 13774458
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

ID: 13786113
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

862 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