Tags: %TAGME{ tpaction="" web="KenyonCpp" tag="" }% view all tags

Lab 4

Due: Oct 8, 11:55pm


  • 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.


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%

Problem 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);

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.

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.

Turn in Runs for:

  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.

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

void shoot(bool& targetAlive, double accuracy);

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:

shoot(charlieAlive, 0.5);

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.

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

Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r5 - 2015-10-08 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback