Difference: Lab4 (1 vs. 23)

Revision 232018-02-20 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4-1 - Count Coins

Changed:
<
<

Due: Oct 3, 11:55pm

>
>

Due: Feb 22, 11:55pm

 
Changed:
<
<
Moodle Link
United_States_coins_public_domain.jpg
>
>
Moodle Link
United_States_coins_public_domain.jpg
 

Instructions

Revision 222017-09-22 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4-1 - Count Coins

Changed:
<
<

Due: Feb 23, 11:55pm

>
>

Due: Oct 3, 11:55pm

 
Changed:
<
<
Moodle Link
United_States_coins_public_domain.jpg
>
>
Moodle Link
United_States_coins_public_domain.jpg
 

Instructions

Revision 212017-02-23 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4-1 - Count Coins

Line: 30 to 30
 %CODE{"c++"}% void computeCoins(int coinValue, int& number, int& amountLeft); //Precondition: 0 < coinValue < 100; 0 <= amountLeft < 100.
Changed:
<
<
//(CoinValue will always be 24, 10, or 1 for this project)
>
>
//(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,

Revision 202017-01-17 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4-1 - Count Coins

Changed:
<
<

Due: Oct 5, 11:55pm

>
>

Due: Feb 23, 11:55pm

 
Changed:
<
<
Moodle Link
United_States_coins_public_domain.jpg
>
>
Moodle Link
United_States_coins_public_domain.jpg
 

Instructions

Line: 21 to 21
 

Problem 1 - Change Computation

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

Revision 192016-10-04 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4-1 - Count Coins

Line: 21 to 21
 

Problem 1 - Change Computation

Added:
>
>
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. %CODE{"c++"}% void computeCoins(int coinValue, int& number, int& amountLeft); //Precondition: 0 < coinValue < 100; 0 <= amountLeft < 100.
Added:
>
>
//(CoinValue will always be 24, 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,
Line: 37 to 42
 %CODE{"c++"}% amountLeft = 37; computeCoins(25, number, amountLeft);
Changed:
<
<
cout << Quarters to return:" << number << " with " << amountLeft << "cents remaining." << endl;
>
>
cout << "Quarters to return:" << number << " with " << amountLeft << "cents remaining." << endl;
 %ENDCODE%

The above sample code would return:

Revision 182016-10-03 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4-1 - Count Coins

Line: 13 to 13
 

Grading Table

Requirement Grading Comments Points Score
Changed:
<
<
Run examples with correct output on required trial data   35  
>
>
Run examples with correct output on required trial data   25  
 
Good clean, easy to understand input and output operations   5  
Changed:
<
<
Code is correctly implements the computeCoins function   4  
>
>
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

Changed:
<
<
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 give a coin value coinValue (25, 10, or 1 in this case), and an amount of change left amountLeft, will return the quantity of that coin that should be given in the change in reference parameter number, and will update the amount left in the reference parameter amountLeft be the amount left in change AFTER subtract the number * coinValue from the amountLeft.
>
>
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.
  %CODE{"c++"}% void computeCoins(int coinValue, int& number, int& amountLeft); //Precondition: 0 < coinValue < 100; 0 <= amountLeft < 100.
Changed:
<
<
//Postcondition: number has been set equal to the maximum number of coins
>
>
//Postcondition: number has been set equal to the maximum number of coins of
 //denomination coinValue cents that can be obtained from amountLeft
Changed:
<
<
//amountLeft has been decreased by the value of the coins, that is, //decreased by number*coinValue.
>
>
//amountLeft has been decreased by the value of the coins to be returned, that is, //amountLeft is decreased by number*coinValue.
  %ENDCODE%
Changed:
<
<
For example, suppose the value of the variable amountLeft is 86 . Then, after the following call, the value of number will be 3 and the value of amountLeft will be 11 (because if you take 3 quarters from 86 cents, that leaves 11 cents):
>
>
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):
  %CODE{"c++"}% amountLeft = 37;
Line: 45 to 45
 
Quarters to return: 1 with 12 cents remaining.
Changed:
<
<
SO - once you have written a the function computeCoins, you will write a main program that uses computeCoins to figure out the change for an given amount. It will need to call computerCoins several times to do this correctly
>
>
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.
 
Changed:
<
<
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.
Functions
>
>
Note that computeCoins will NOT do any input or output (e.g. cin or cout). This will all be done in the main program.
 
Changed:
<
<
Hint: Use integer division (/ operator) and the % operator to implement this function.
>
>
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

Revision 172016-10-02 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4-1 - Count Coins

Line: 21 to 21
 

Problem 1 - Change Computation

Changed:
<
<
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 the following function (among others):
>
>
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 give a coin value coinValue (25, 10, or 1 in this case), and an amount of change left amountLeft, will return the quantity of that coin that should be given in the change in reference parameter number, and will update the amount left in the reference parameter amountLeft be the amount left in change AFTER subtract the number * coinValue from the amountLeft.
  %CODE{"c++"}% void computeCoins(int coinValue, int& number, int& amountLeft);
Line: 32 to 32
 //decreased by number*coinValue. %ENDCODE%
Changed:
<
<
For example, suppose the value of the variable amountLeft is 86 . Then, after the following call, the value of number will be 3 and the value of amountLeft will be 11 (because if you take 3 quarters from 86 cents, that leaves 11 cents):
>
>
For example, suppose the value of the variable amountLeft is 86 . Then, after the following call, the value of number will be 3 and the value of amountLeft will be 11 (because if you take 3 quarters from 86 cents, that leaves 11 cents):
  %CODE{"c++"}% amountLeft = 37;
Line: 40 to 40
  cout << Quarters to return:" << number << " with " << amountLeft << "cents remaining." << endl; %ENDCODE%
Changed:
<
<
The above code would return:
>
>
The above sample code would return:
 
Quarters to return: 1 with 12 cents remaining.
Changed:
<
<
Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program.
>
>
SO - once you have written a the function computeCoins, you will write a main program that uses computeCoins to figure out the change for an given amount. It will need to call computerCoins several times to do this correctly

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.

 
Functions

Hint: Use integer division (/ operator) and the % operator to implement this function.

Line: 85 to 87
 
  • mystica-Coins-Money.png:
Changed:
<
<
  • United_States_coins_public_domain.jpg:
>
>
  • 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"
META FILEATTACHMENT 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"

Revision 162016-09-30 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4-1 - Count Coins

Due: Oct 5, 11:55pm

Changed:
<
<
Moodle Link
mystica-Coins-Money.png
>
>
Moodle Link
United_States_coins_public_domain.jpg
 

Instructions

Line: 84 to 85
 
  • mystica-Coins-Money.png:
Added:
>
>
  • 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"
Added:
>
>
META FILEATTACHMENT 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"
 
META TOPICMOVED by="JimSkon" date="1443609232" from="KenyonCpp.Lab5" to="KenyonCpp.Lab4"

Revision 152016-09-30 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"
Changed:
<
<

Lab 4-1

>
>

Lab 4-1 - Count Coins

 

Due: Oct 5, 11:55pm

Changed:
<
<
Moodle Link
>
>
Moodle Link
mystica-Coins-Money.png

 

Instructions

  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.
Line: 13 to 15
 
Requirement Grading Comments Points Score
Run examples with correct output on required trial data   35  
Good clean, easy to understand input and output operations   5  
Changed:
<
<
Code is correctly implements the computCoins function   4  
>
>
Code is correctly implements the computeCoins function   4  
 
Complete source code with good formatting and organization, variables names, and comments   5  
Total   50  
Line: 81 to 82
  Solutions
Added:
>
>
  • mystica-Coins-Money.png:

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"
 
META TOPICMOVED by="JimSkon" date="1443609232" from="KenyonCpp.Lab5" to="KenyonCpp.Lab4"

Revision 142016-09-30 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"
Changed:
<
<

Lab 4

>
>

Lab 4-1

 

Due: Oct 5, 11:55pm

Moodle Link

Line: 11 to 11
 

Grading Table

Requirement Grading Comments Points Score
Changed:
<
<
Change Computation: Run examples with correct output on required trial data   15  
Change Computation: Good clean, easy to understand input and output operations   5  
Change Computation: Complete source code with good formatting and organization, variables names, and comments   5  
Duel Simulation: Run examples with correct output on required trial data   50  
Duel Simulation: Good clean, easy to understand Input and output operations   10  
Duel Simulation Complete source code with good formatting and organization, variables names, and comments   10  
Total   100  
>
>
Run examples with correct output on required trial data   35  
Good clean, easy to understand input and output operations   5  
Code is correctly implements the computCoins function   4  
Complete source code with good formatting and organization, variables names, and comments   5  
Total   50  
 

Problem 1 - Change Computation

Line: 45 to 43
 
Quarters to return: 1 with 12 cents remaining.
Changed:
<
<
Include a loop 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.
>
>
Include a loop that lets the user repeat this computation for new input values until the user says he or she wants to end the program.
Functions

Hint: Use integer division (/ operator) and the % operator to implement this function.

 

Sample runs

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

Line: 79 to 79
 
  1. -3
  2. 104
Deleted:
<
<

Problem 2 - Duel Simulation

In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron is a poor shooter and only hits his target with a probability of 1/3. Bob is a bit better and hits his target with a probability of 1/2. Charlie is an expert marksman and never misses. A hit means a kill and the person hit drops out of the duel.

To compensate for the inequities in their marksmanship skills, it is decided that the contestants would fire in turns starting with Aaron, followed by Bob, then by Charlie, then back to Aaron. The cycle would repeat until there was only one man standing. And that man would be remembered as the greatest puzzler of all time.

a. Write a function to simulate a single shot. It should use the following declaration:

<-- SyntaxHighlightingPlugin -->
void shoot(bool& targetAlive, double accuracy);
 
<-- end SyntaxHighlightingPlugin -->

This would simulate someone shooting at targetAlive with the given accuracy by generating a random number between 0 and 1. If the random number is less than accuracy , then the target is hit and targetAlive should be set to false . Below is a link that illustrates how to generate random numbers.

Generating Random Numbers

You should write a function to seed the random number generator, and another to return the next random number between 0 and 1.

So, for example, if Bob is shooting at Charlie, this could be invoked as:

<-- SyntaxHighlightingPlugin -->
shoot(charlieAlive, 0.5);
<-- end SyntaxHighlightingPlugin -->

Here, charlieAlive is a Boolean variable that indicates if Charlie is alive. Test your function using a driver program before moving on to step b.

b. An obvious strategy is for each man to shoot at the most accurate shooter still alive on the grounds that this shooter is the deadliest and has the best chance of hitting back. Write a second function named startDuel that uses the shoot function to simulate an entire duel using this strategy. The function will cycle through the men in the given order (Aaron, Bob, Charlie), dropping each men out of the conetest when they die.

It should loop until only one contestant is left, invoking the shoot function with the proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.

For example, you might have the series:

  • Aaron shoots at Charlie, misses
  • Bob shoots at Charlie, misses
  • Charlie shoots at Bob, hits (Bob dies)
  • Aaron shoots at Charlie, hits (Charlie dies)
  • Aaron wins!
c. In your main function, invoke the startDuel function 1,000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive. Thus you end up with a solution that shows the win scores for each (fake results here):
  • Aaron 354 wins
  • Bob 452 wins
  • Charlie 194 wins
d. A counterintuitive strategy is for Aaron to intentionally miss on his first shot. Thereafter, everyone uses the strategy of shooting at the most accurate shooter left alive. This strategy means that Aaron is guaranteed to live past the first round, since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.
What to do

You can either write two similar programs that use the different strategies, or you can write a single program with two versions of duel, e.g. duel1 (the first strategy) and duel2 (the second strategy), and have it first run the test with duel1, then with duel2.

After each set of 1000 duels, print the probability that each contestant will win for that strategy. Example Run:

Strategy one: always shoot at the best shot.
Out of a 1000 duels:
  Aaron wins 374 times, probability of winning 0.374
  Bob wins 419 times, probability of winning 0.419
  Charlie wins 207 times, probability of winning 0.207


Strategy two: always shoot at the best shot. Aaron misses first shot
Out of a 1000 duels:
  Aaron wins 356 times, probability of winning 0.356
  Bob wins 422 times, probability of winning 0.422
  Charlie wins 222 times, probability of winning 0.222

(The above answers are examples, not the correct numbers).

Turn In

  1. A run of each simluation strategy
  2. All source code
 Solutions

META TOPICMOVED by="JimSkon" date="1443609232" from="KenyonCpp.Lab5" to="KenyonCpp.Lab4"

Revision 132016-09-28 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Due: Oct 5, 11:55pm

Line: 122 to 123
  You can either write two similar programs that use the different strategies, or you can write a single program with two versions of duel, e.g. duel1 (the first strategy) and duel2 (the second strategy), and have it first run the test with duel1, then with duel2.
Changed:
<
<
After each set of 1000 duels, print the probability that each contestant will win for that strategy:
Probability of winning with this strategy
Charlie: 0.345
Bob: 0.255
Aaron: 0.4

>
>
After each set of 1000 duels, print the probability that each contestant will win for that strategy. Example Run:

Strategy one: always shoot at the best shot.
Out of a 1000 duels:
  Aaron wins 374 times, probability of winning 0.374
  Bob wins 419 times, probability of winning 0.419
  Charlie wins 207 times, probability of winning 0.207


Strategy two: always shoot at the best shot. Aaron misses first shot
Out of a 1000 duels:
  Aaron wins 356 times, probability of winning 0.356
  Bob wins 422 times, probability of winning 0.422
  Charlie wins 222 times, probability of winning 0.222

 

(The above answers are examples, not the correct numbers).

Revision 122016-09-28 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Due: Oct 5, 11:55pm

Line: 91 to 90
 void shoot(bool& targetAlive, double accuracy); %ENDCODE%
Changed:
<
<
This would simulate someone shooting at targetAlive with the given accuracy by generating a random number between 0 and 1. If the random number is less than accuracy , then the target is hit and targetAlive should be set to false . Appendix 4 illustrates how to generate random numbers.
>
>
This would simulate someone shooting at targetAlive with the given accuracy by generating a random number between 0 and 1. If the random number is less than accuracy , then the target is hit and targetAlive should be set to false . Below is a link that illustrates how to generate random numbers.
Generating Random Numbers

You should write a function to seed the random number generator, and another to return the next random number between 0 and 1.

 
Changed:
<
<
For example, if Bob is shooting at Charlie, this could be invoked as:
>
>
So, for example, if Bob is shooting at Charlie, this could be invoked as:
  %CODE{"c++"}% shoot(charlieAlive, 0.5);

Revision 112016-09-27 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Due: Oct 5, 11:55pm

Line: 8 to 8
 
  • 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.
Deleted:
<
<

Grading

 
Changed:
<
<
Feature %
Program correctness and completeness with respect to defination 70%
Code Format (Indenting, variable names) 10%
Code Comments 10%
Turning in complete run of every function at least twice with difference values. 10%
>
>

Grading Table

Requirement Grading Comments Points Score
Change Computation: Run examples with correct output on required trial data   15  
Change Computation: Good clean, easy to understand input and output operations   5  
Change Computation: Complete source code with good formatting and organization, variables names, and comments   5  
Duel Simulation: Run examples with correct output on required trial data   50  
Duel Simulation: Good clean, easy to understand Input and output operations   10  
Duel Simulation Complete source code with good formatting and organization, variables names, and comments   10  
Total   100  
 

Problem 1 - Change Computation

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 the following function (among others):

Line: 124 to 129
 

(The above answers are examples, not the correct numbers).

Added:
>
>

Turn In

  1. A run of each simluation strategy
  2. All source code
  Solutions

Revision 102016-09-27 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Due: Oct 5, 11:55pm

Line: 35 to 35
  computeCoins(25, number, amountLeft); cout << Quarters to return:" << number << " with " << amountLeft << "cents remaining." << endl; %ENDCODE%
Added:
>
>
 The above code would return:
Changed:
<
<
Quarters to return: 1 with 12 cents remaining.

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

 
Added:
>
>
 Include a loop 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.

Sample runs

Line: 124 to 125
  (The above answers are examples, not the correct numbers).
Added:
>
>
Solutions
 
META TOPICMOVED by="JimSkon" date="1443609232" from="KenyonCpp.Lab5" to="KenyonCpp.Lab4"

Revision 92016-09-20 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Due: Oct 5, 11:55pm

Line: 17 to 17
 
Turning in complete run of every function at least twice with difference values. 10%

Problem 1 - Change Computation

Changed:
<
<
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 the following function (among others): void compute_coin(int coin_value, int& number, int& amount_left);
>
>
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 the following function (among others):
  %CODE{"c++"}%
Changed:
<
<
void compute_coin(int coin_value, int& number, int& amount_left); //Precondition: 0 < coin_value < 100; 0 <= amount_left < 100.
>
>
void computeCoins(int coinValue, int& number, int& amountLeft); //Precondition: 0 < coinValue < 100; 0 <= amountLeft < 100.
 //Postcondition: number has been set equal to the maximum number of coins
Changed:
<
<
//denomination coin_value cents that can be obtained from amount_left //amount_left has been decreased by the value of the coins, that is, //decreased by number*coin_value.
>
>
//denomination coinValue cents that can be obtained from amountLeft //amountLeft has been decreased by the value of the coins, that is, //decreased by number*coinValue.
  %ENDCODE%
Changed:
<
<
For example, suppose the value of the variable amount_left is 86 . Then, after the following call, the value of number will be 3 and the value of amount_left will be 11 (because if you take 3 quarters from 86 cents, that leaves 11 cents): compute_coins(25, number, amount_left); Include a loop 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 and the % operator to implement this function.
>
>
For example, suppose the value of the variable amountLeft is 86 . Then, after the following call, the value of number will be 3 and the value of amountLeft will be 11 (because if you take 3 quarters from 86 cents, that leaves 11 cents):
<-- SyntaxHighlightingPlugin -->
amountLeft = 37;
    computeCoins(25, number, amountLeft);
    cout << Quarters to return:" << number << " with " << amountLeft << "cents remaining." << endl;
<-- end SyntaxHighlightingPlugin -->
The above code would return:
Quarters to return: 1 with 12 cents remaining.
Include a loop 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.

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

Revision 82016-09-20 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Changed:
<
<

Due: Oct 4, 11:55pm

>
>

Due: Oct 5, 11:55pm

Moodle Link

 

Instructions

  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.

Revision 72016-09-06 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Due: Oct 4, 11:55pm

Line: 13 to 13
 
Code Format (Indenting, variable names) 10%
Code Comments 10%
Turning in complete run of every function at least twice with difference values. 10%
Changed:
<
<

Problem 1

>
>

Problem 1 - Change Computation

  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 the following function (among others): void compute_coin(int coin_value, int& number, int& amount_left);
Line: 36 to 36
 
  1. -3
  2. 104
Changed:
<
<

Problem 2

>
>

Problem 2 - Duel Simulation

  In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron is a poor shooter and only hits his target with a probability of 1/3. Bob is a bit better and hits his target with a probability of 1/2. Charlie is an expert marksman and never misses. A hit means a kill and the person hit drops out of the duel.
Changed:
<
<
To compensate for the inequities in their marksmanship skills, it is decided that the contestants would fire in turns starting with Aaron, followed by Bob, and then by Charlie. The cycle would repeat until there was one man standing. And that man would be remembered as the greatest puzzler of all time.
>
>
To compensate for the inequities in their marksmanship skills, it is decided that the contestants would fire in turns starting with Aaron, followed by Bob, then by Charlie, then back to Aaron. The cycle would repeat until there was only one man standing. And that man would be remembered as the greatest puzzler of all time.
  a. Write a function to simulate a single shot. It should use the following declaration:
Line: 56 to 56
 shoot(charlieAlive, 0.5); %ENDCODE%
Changed:
<
<
Here, charlieAlive is a Boolean variable that indicates if Charlie is alive. Test your function using a driver program before moving on to step b.
>
>
Here, charlieAlive is a Boolean variable that indicates if Charlie is alive. Test your function using a driver program before moving on to step b.
 
Changed:
<
<
An obvious strategy is for each man to shoot at the most accurate shooter still alive on the grounds that this shooter is the deadliest and has the best chance of hitting back. Write a second function named startDuel that uses the shoot function to simulate an entire duel using this strategy. It should loop until only one contestant is left, invoking the shoot function with the proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.
>
>
b. An obvious strategy is for each man to shoot at the most accurate shooter still alive on the grounds that this shooter is the deadliest and has the best chance of hitting back. Write a second function named startDuel that uses the shoot function to simulate an entire duel using this strategy. The function will cycle through the men in the given order (Aaron, Bob, Charlie), dropping each men out of the conetest when they die.
 
Changed:
<
<
c. In your main function, invoke the startDuel function 1,000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive.
>
>
It should loop until only one contestant is left, invoking the shoot function with the proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.
 
Added:
>
>
For example, you might have the series:
  • Aaron shoots at Charlie, misses
  • Bob shoots at Charlie, misses
  • Charlie shoots at Bob, hits (Bob dies)
  • Aaron shoots at Charlie, hits (Charlie dies)
  • Aaron wins!
c. In your main function, invoke the startDuel function 1,000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive. Thus you end up with a solution that shows the win scores for each (fake results here):
  • Aaron 354 wins
  • Bob 452 wins
  • Charlie 194 wins
 d. A counterintuitive strategy is for Aaron to intentionally miss on his first shot. Thereafter, everyone uses the strategy of shooting at the most accurate shooter left alive. This strategy means that Aaron is guaranteed to live past the first round, since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.
What to do
Changed:
<
<
You can either write two similar programs that use the different strategies, or you can write a single program with two versions of duel, e.g. duel1 (the first strategy) and duel2 (the second strategy), and have it first run the test with duel1, then with duel2.
>
>
You can either write two similar programs that use the different strategies, or you can write a single program with two versions of duel, e.g. duel1 (the first strategy) and duel2 (the second strategy), and have it first run the test with duel1, then with duel2.
 
Changed:
<
<
After each set of 1000 duels, print the probability that each contestant will win for that strategy:
>
>
After each set of 1000 duels, print the probability that each contestant will win for that strategy:
 
Probability of winning with this strategy
Charlie: 0.345
Bob: 0.255

Revision 62016-08-24 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Changed:
<
<

Due: Oct 8, 11:55pm

>
>

Due: Oct 4, 11:55pm

 

Instructions

  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.

Revision 52015-10-08 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Due: Oct 8, 11:55pm

Line: 13 to 13
 
Code Format (Indenting, variable names) 10%
Code Comments 10%
Turning in complete run of every function at least twice with difference values. 10%
Changed:
<
<

Problems

>
>

Problem 1

 
Changed:
<
<
1. 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 the following function (among others): void compute_coin(int coin_value, int& number, int& amount_left);
>
>
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 the following function (among others): void compute_coin(int coin_value, int& number, int& amount_left);
  %CODE{"c++"}%
Line: 28 to 28
  %ENDCODE%

For example, suppose the value of the variable amount_left is 86 . Then, after the following call, the value of number will be 3 and the value of amount_left will be 11 (because if you take 3 quarters from 86 cents, that leaves 11 cents): compute_coins(25, number, amount_left); Include a loop 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 and the % operator to implement this function.

Added:
>
>
Turn in Runs for:
 
Changed:
<
<
2. In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron is a poor shooter and only hits his target with a probability of 1/3. Bob is a bit better and hits his target with a probability of 1/2. Charlie is an expert marksman and never misses. A hit means a kill and the person hit drops out of the duel.
>
>
  1. 43
  2. 79
  3. 99
  4. 1
  5. -3
  6. 104

Problem 2

In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron is a poor shooter and only hits his target with a probability of 1/3. Bob is a bit better and hits his target with a probability of 1/2. Charlie is an expert marksman and never misses. A hit means a kill and the person hit drops out of the duel.

  To compensate for the inequities in their marksmanship skills, it is decided that the contestants would fire in turns starting with Aaron, followed by Bob, and then by Charlie. The cycle would repeat until there was one man standing. And that man would be remembered as the greatest puzzler of all time.
Line: 54 to 64
 c. In your main function, invoke the startDuel function 1,000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive.

d. A counterintuitive strategy is for Aaron to intentionally miss on his first shot. Thereafter, everyone uses the strategy of shooting at the most accurate shooter left alive. This strategy means that Aaron is guaranteed to live past the first round, since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.

Added:
>
>
What to do

You can either write two similar programs that use the different strategies, or you can write a single program with two versions of duel, e.g. duel1 (the first strategy) and duel2 (the second strategy), and have it first run the test with duel1, then with duel2.

After each set of 1000 duels, print the probability that each contestant will win for that strategy:

Probability of winning with this strategy
Charlie: 0.345
Bob: 0.255
Aaron: 0.4

(The above answers are examples, not the correct numbers).

 
META TOPICMOVED by="JimSkon" date="1443609232" from="KenyonCpp.Lab5" to="KenyonCpp.Lab4"

Revision 42015-10-06 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Changed:
<
<

Due: Oct 7, 11:55pm

>
>

Due: Oct 8, 11:55pm

 

Instructions

  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.

Revision 32015-10-01 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Changed:
<
<

Due: Oct 6, 11:55pm

>
>

Due: Oct 7, 11:55pm

 

Instructions

  • Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.
Line: 29 to 29
  For example, suppose the value of the variable amount_left is 86 . Then, after the following call, the value of number will be 3 and the value of amount_left will be 11 (because if you take 3 quarters from 86 cents, that leaves 11 cents): compute_coins(25, number, amount_left); Include a loop 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 and the % operator to implement this function.
Changed:
<
<
2. In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron is a poor shooter and only hits his target with a probability of 1/3. Bob is a bit better and hits his target with a probability of 1/2. Charlie is an expert marksman and never misses. A hit means a kill and the person hit drops out of the duel.

To compensate for the inequities in their marksmanship skills, it is decided that the contestants would fire in turns starting with Aaron, followed by Bob, and then by Charlie. The cycle would repeat until there was one man standing. And that man would be remembered as the greatest puzzler of all time.

>
>
2. In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron is a poor shooter and only hits his target with a probability of 1/3. Bob is a bit better and hits his target with a probability of 1/2. Charlie is an expert marksman and never misses. A hit means a kill and the person hit drops out of the duel.
 
Changed:
<
<
a. Write a function to simulate a single shot. It should use the following declaration:
>
>
To compensate for the inequities in their marksmanship skills, it is decided that the contestants would fire in turns starting with Aaron, followed by Bob, and then by Charlie. The cycle would repeat until there was one man standing. And that man would be remembered as the greatest puzzler of all time.

a. Write a function to simulate a single shot. It should use the following declaration:

 
<-- SyntaxHighlightingPlugin -->
void shoot(bool& targetAlive, double accuracy);
 
<-- end SyntaxHighlightingPlugin -->
Changed:
<
<
This would simulate someone shooting at targetAlive with the given accuracy by generating a random number between 0 and 1. If the random number is less than accuracy , then the target is hit and targetAlive should be set to false . Appendix 4 illustrates how to generate random numbers.
>
>
This would simulate someone shooting at targetAlive with the given accuracy by generating a random number between 0 and 1. If the random number is less than accuracy , then the target is hit and targetAlive should be set to false . Appendix 4 illustrates how to generate random numbers.
  For example, if Bob is shooting at Charlie, this could be invoked as:
<-- SyntaxHighlightingPlugin -->
shoot(charlieAlive, 0.5);
 
<-- end SyntaxHighlightingPlugin -->
Changed:
<
<
Here, charlieAlive is a Boolean variable that indicates if Charlie is alive. Test your function using a driver program before moving on to step b.

An obvious strategy is for each man to shoot at the most accurate shooter still alive on the grounds that this shooter is the deadliest and has the best chance of hitting back. Write a second function named startDuel that uses the shoot function to simulate an entire duel using this strategy. It should loop until only one contestant is left, invoking the shoot function with the proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.

c. In your main function, invoke the startDuel function 1,000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive.

d. A counterintuitive strategy is for Aaron to intentionally miss on his first shot. Thereafter, everyone uses the strategy of shooting at the most accurate shooter left alive. This strategy means that Aaron is guaranteed to live past the first round, since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.

>
>
Here, charlieAlive is a Boolean variable that indicates if Charlie is alive. Test your function using a driver program before moving on to step b.

An obvious strategy is for each man to shoot at the most accurate shooter still alive on the grounds that this shooter is the deadliest and has the best chance of hitting back. Write a second function named startDuel that uses the shoot function to simulate an entire duel using this strategy. It should loop until only one contestant is left, invoking the shoot function with the proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.

c. In your main function, invoke the startDuel function 1,000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive.

d. A counterintuitive strategy is for Aaron to intentionally miss on his first shot. Thereafter, everyone uses the strategy of shooting at the most accurate shooter left alive. This strategy means that Aaron is guaranteed to live past the first round, since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.

 
META TOPICMOVED by="JimSkon" date="1443609232" from="KenyonCpp.Lab5" to="KenyonCpp.Lab4"

Revision 22015-09-30 - JimSkon

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Lab 4

Due: Oct 6, 11:55pm

Line: 83 to 83
 guaranteed to live past the first round, since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.
Added:
>
>
META TOPICMOVED by="JimSkon" date="1443609232" from="KenyonCpp.Lab5" to="KenyonCpp.Lab4"

Revision 12015-09-24 - JimSkon

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

Lab 4

Due: Oct 6, 11:55pm

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.

Grading

Feature %
Program correctness and completeness with respect to defination 70%
Code Format (Indenting, variable names) 10%
Code Comments 10%
Turning in complete run of every function at least twice with difference values. 10%

Problems

1. 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 the following function (among others): void compute_coin(int coin_value, int& number, int& amount_left);

<-- SyntaxHighlightingPlugin -->
void compute_coin(int coin_value, int& number, int& amount_left);
//Precondition: 0 < coin_value < 100; 0 <= amount_left < 100.
//Postcondition: number has been set equal to the maximum number of coins
//denomination coin_value cents that can be obtained from amount_left
//amount_left has been decreased by the value of the coins, that is,
//decreased by number*coin_value.
 
<-- end SyntaxHighlightingPlugin -->

For example, suppose the value of the variable amount_left is 86 . Then, after the following call, the value of number will be 3 and the value of amount_left will be 11 (because if you take 3 quarters from 86 cents, that leaves 11 cents): compute_coins(25, number, amount_left); Include a loop 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 and the % operator to implement this function.

2. In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron is a poor shooter and only hits his target with a probability of 1/3. Bob is a bit better and hits his target with a probability of 1/2. Charlie is an expert marksman and never misses. A hit means a kill and the person hit drops out of the duel.

To compensate for the inequities in their marksmanship skills, it is decided that the contestants would fire in turns starting with Aaron, followed by Bob, and then by Charlie. The cycle would repeat until there was one man standing. And that man would be remembered as the greatest puzzler of all time.

a. Write a function to simulate a single shot. It should use the following declaration:

<-- SyntaxHighlightingPlugin -->
void shoot(bool& targetAlive, double accuracy);
 
<-- end SyntaxHighlightingPlugin -->
This would simulate someone shooting at targetAlive with the given accuracy by generating a random number between 0 and 1. If the random number is less than accuracy , then the target is hit and targetAlive should be set to false . Appendix 4 illustrates how to generate random numbers.

For example, if Bob is shooting at Charlie, this could be invoked as:

<-- SyntaxHighlightingPlugin -->
shoot(charlieAlive, 0.5);
 
<-- end SyntaxHighlightingPlugin -->
Here, charlieAlive is a Boolean variable that indicates if Charlie is alive. Test your function using a driver program before moving on to step b.

An obvious strategy is for each man to shoot at the most accurate shooter still alive on the grounds that this shooter is the deadliest and has the best chance of hitting back. Write a second function named startDuel that uses the shoot function to simulate an entire duel using this strategy. It should loop until only one contestant is left, invoking the shoot function with the proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.

c. In your main function, invoke the startDuel function 1,000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive.

d. A counterintuitive strategy is for Aaron to intentionally miss on his first shot. Thereafter, everyone uses the strategy of shooting at the most accurate shooter left alive. This strategy means that Aaron is guaranteed to live past the first round, since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.

 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 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