# Lab 4-1 - Count Coins

### Instructions

• Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.
• Remember to format the code as described and the book and text, and to include comments including complete commetns at the beginning of the program.

Code is correctly implements the computeCoins function   15
Complete source code with good formatting and organization, variables names, and comments   5
Good clean, easy to understand input and output operations   5
Run examples with correct output on required trial data   25
Total   50

### Problem 1 - Change Computation

This is a very simple program. It is merely a main function that calls the computeCoin function. The trick is that you MUST follow a "specification" (given below) to write the computeCoin function to work exactly as the the comments in the declaration below specify. The learning goals here are:

1. Use a formal specification of a function to write a function that meets that specification
2. Get a opportunity to use call-by-reference parameter passing.

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) and 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 a function that meets the following specifications. The idea is that you are being asked to write a specific function computeCoins, that is a general purpose function that when given a coin value coinValue (25, 10, or 1 in this case), and an amount of change remaining amountLeft, will return the quantity of that coin that should be given as change in the cal-by-reference parameter number, and will update the amount left in the call-by-reference parameter amountLeft to be the amount left AFTER subtracting the number * coinValue quantity from the amountLeft.

```void computeCoins(int coinValue, int& number, int& amountLeft);
//Precondition: 0 < coinValue < 100; 0 <= amountLeft < 100.
//(CoinValue will always be 25, 10, or 1 for this project)
//Postcondition: number has been set equal to the maximum number of coins of
//denomination coinValue cents that can be obtained from amountLeft
//amountLeft has been decreased by the value of the coins to be returned, that is,
//amountLeft is decreased by number*coinValue.
```

For example, suppose the value of the variable amountLeft is 86 . Then, after the following call, the value of number will be 3 (for 3 quaters) and the value of amountLeft will be 11 (because when you take 3 quarters from 86 cents, this leaves 11 cents):

```amountLeft = 37;
computeCoins(25, number, amountLeft);
cout << "Quarters to return:" << number << " with " << amountLeft << "cents remaining." << endl;```

The above sample code would return:

```Quarters to return: 1 with 12 cents remaining.
```

So - once you have written the function computeCoins, you will write a main program that uses computeCoins to figure out the change for an given amount entered by the user. It will need to call computerCoins several times to do this correctly.

Note that computeCoins will NOT do any input or output (e.g. cin or cout). This will all be done in the main program.

Include a loop in your program that lets the user repeat this computation for new input values until the user says he or she wants to end the program.

###### Hint
• Use integer division (/ operator) and the % operator to implement this function.
• computeCoins is not a complicated function. I needed only two lines of code to write it. If it is much more than this, you are on the wrong track, and should review this document again, and perhaps get some help form the tutor or instructor.

### Sample runs

```Enter the amount of money between 1 and 99 cents: 62

Quarters: 2
Dimes: 1
Pennies: 2
go again? (y to continue):y
Enter the amount of money between 1 and 99 cents: 23

Quarters: 0
Dimes: 2
Pennies: 3
go again? (y to continue):y
Enter the amount of money between 1 and 99 cents: 133
Amount must be between 1 and 99 cents
Enter the amount of money between 1 and 99 cents: 45

Quarters: 1
Dimes: 2
Pennies: 0
go again? (y to continue):n

```
##### Turn in Runs for:
1. 43
2. 79
3. 99
4. 1
5. -3
6. 104

• mystica-Coins-Money.png:

• United_States_coins_public_domain.jpg:
Topic attachments
I Attachment History Action Size Date Who Comment
jpg United_States_coins_public_domain.jpg r1 manage 110.8 K 2016-09-30 - 23:28 JimSkon
png mystica-Coins-Money.png r1 manage 1282.1 K 2016-09-30 - 18:18 JimSkon
Topic revision: r23 - 2018-02-20 - JimSkon

 Home Fall2016 Web View Edit Account
Copyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback