Click here to Skip to main content
15,666,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have code that looks like this

void dispense_change(int denomination, int price_in_cents)
   int denom_value=denomination*100;
   int change = denom_value-price_in_cents;
   int loonies = change / 100;
   int ql = change % 100;
   int halfloonies = ql / 50;
   int qh = ql % 50;
   int quarters = qh / 25;
   int qq = qh % 25;
   int dimes = qq / 10;
   int qd = qq % 10;
   int nickels = qd / 5;
   int pennies = qd % 5;
   printf("%d loonies + %d half-loonies + %d quarters + %d dimes + %d nickels + %d pennies\n",loonies, halfloonies, quarters, dimes, nickels, pennies);

Where if you input in values for currency it outputs how many of each coin you will get as change. If I wanted to alter it so that it prints something like this

0 loonies 0 half-loonies 0 quarters...
0 loonies 2 half-loonies 0 quarters...
0 loonies 0 half-loonies 4 quarters...

How would I go about this?

What I have tried:

I've tried having more than one printf, but I'm not sure how to alter them so that each printf prints all zeroes and only one value.
Updated 3-Oct-21 20:48pm
Richard MacCutchan 4-Oct-21 4:00am    
You need a loop so you exclude certain coin types each time round. So first time round you report only cents, second time nickels and cents, third dimes, nickels and cents ...

1 solution

Your printf statement shows the values that were passed to the function, so the easiest - and probably best - way is to leave that function alone, and look at where you call it.
Somewhere, you will have code that looks something like this:
dispence_change(1, 73);
Though the actual values passed are likely to be different and probably be variables rather than constants.
If you duplicate the line that calls the function, it will be executed twice, and will print two rows. If each call passes different values, the line should show different results.

Perhaps start with a loop:
for (int price = 0; price < 100; price++)
   dispense_change(1, price);
And see what happens?
Share this answer
Richard Deeming 4-Oct-21 5:36am    
You've added a typo that doesn't seem to be in the OP's question. :)
OriginalGriff 4-Oct-21 6:17am    
Or corrected a typo by mistake ... :laugh:
Richard Deeming 4-Oct-21 7:15am    
Except "dispence" is an obsolete spelling of "dispense". :)
dispence - Wiktionary[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900