Difference: Lab4 (22 vs. 23)

Revision 232018-02-20 - JimSkon

Line: 1 to 1

 META TOPICPARENT name="WebHome"

Lab 4-1 - Count Coins

Changed:
<
<

>
>

Due: Feb 22, 11:55pm

Changed:
<
<
Moodle Link  >
>
Moodle Link  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.

Run examples with correct output on required trial data   25
Good clean, easy to understand input and output operations   5
Code is correctly implements the computeCoins function   15
Complete source code with good formatting and organization, variables names, and comments   5
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.

`<-- SyntaxHighlightingPlugin -->`
```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.
```
`<-- end SyntaxHighlightingPlugin -->`

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):

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

The above sample code would return:

Deleted:
<
<

```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:

 META FILEATTACHMENT attachment="mystica-Coins-Money.png" attr="" comment="" date="1475259505" name="mystica-Coins-Money.png" path="mystica-Coins-Money.png" size="1312873" user="JimSkon" version="1" attachment="United_States_coins_public_domain.jpg" attr="" comment="" date="1475278095" name="United_States_coins_public_domain.jpg" path="United_States_coins_public_domain.jpg" size="113450" user="JimSkon" version="1" by="JimSkon" date="1443609232" from="KenyonCpp.Lab5" to="KenyonCpp.Lab4"

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