Link to home
Start Free TrialLog in
Avatar of cuong5985
cuong5985

asked on

Amount of coin change

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.
Avatar of r-k
r-k

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.
ASKER CERTIFIED SOLUTION
Avatar of jhshukla
jhshukla
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.