Homework help please

Write a program that tells what coins to give out for any amount of change from 1 cent to 99 cents.
For example, if the amount is 86 cents, the output would be something like the following:

86 cents can be given as 3 quarter(s) 1 dime(s) amd 1 penny(pennies).
Use coin denominations of 25 cents (quarters),
10 cents (dimes) and 1 cent (pennies).
Do not use nickel and half-dollar coins.
Your program will use the following function (amoung others): void computeCoin(int coinValue, int& number, int& amountLeft);

This where i am at but dont know what else to add.

#include <iostream>
using namespace std;

void compute_coins(int coin_value, int& num, int& amount_left);



void compute_coins(int coin_value, int& num, int& amount_left)
{
num = amount_left / coin_value;
amount_left = amount_left % coin_value;
}

int main()
{
int quarters;
int dimes;
int pennies;
int change;

compute_coins(25, quarters, change);
compute_coins(10, dimes, change);
pennies = change;

cout << dimes << endl;
cout << quarters << endl;
cout << change<< endl ;

return 0;
}


Last edited on
I think, you need to make your compute_coins a bit generic. Also, a bit of change in signature is required. What type of coins it will return. lets take an enum

1
2
3
4
5
6
7
8
enum typeofcoin
{
quarter = 1,
dime = 2,
penny = 3
};

typeofcoin computeCoin(int coinValue, int& number, int& amountLeft); 


Multiple calls to computeCoin will be required. Lets say for 86;

call1:
computeCoin(86, pm1, pm2);
pm1 will have 3, pm2 will have 11 and return type will be quarter;

call2:
computeCoin(11, pm1, pm2);
pm1 will have 1, pm2 will have 1 and return type will be dime.

call3:
computeCoin(1, pm1, pm2);
pm1 will have 1, pm2 will have 0 and return type will be dime.

as soon you see pm2 is 0, which means all the change is returned, you will stop.

in the main, you can do something like this:

1
2
3
4
while(pm2 != 0)
{
computeCoin(86, pm1, pm2)
}


make sense ? Could you try to write computeCoin now ?
Last edited on
Topic archived. No new replies allowed.