SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Functions For All Subtasks
Exercise:
Programming Projects
Question:15 | ISBN:9780321531346 | Edition: 7

Question

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.

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.

 

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#include <iostream>
#include <cstdlib> 
using namespace std;
void shoot(bool& targetAlive, double accuracy);
int startDuel();
const double ACCURACYARRONA = 1.0/3;
const double ACCURACYBOB = 0.5;
const double ACCURACYCHARLIE = 1.0;
const int DUENUM = 1000;

void shoot(bool& targetAlive, double accuracy)
{
  double r;
  if (targetAlive == true)
  {
   r = rand() % 100; 
   if (r < (accuracy * 100))
   {
     targetAlive = false;
   }
  }
}
int startDuel()
{
   bool aliveAaron = true, aliveBob = true,
                               aliveCharlie = true;
  while ((aliveAaron && aliveBob) || 
              (aliveAaron &&aliveCharlie) ||
                         (aliveBob &&aliveCharlie))
  {

     if (aliveBob)
     {
       if (aliveCharlie)
       {
         shoot(aliveCharlie, ACCURACYBOB);
       }
       else if (aliveAaron)
       {
         shoot(aliveAaron, ACCURACYBOB);
       }
    }
      if (aliveCharlie)
      {
       if (aliveBob)
      {
        shoot(aliveBob, ACCURACYCHARLIE);
        }
      else if (aliveAaron)
      {
        shoot(aliveAaron, ACCURACYCHARLIE);
      }
    }
   
    if (aliveAaron)
    {
      if (aliveCharlie)
     {
       shoot(aliveCharlie, ACCURACYARRONA);
     }
     else if (aliveBob)
    {
     shoot(aliveBob, ACCURACYARRONA);
    }
   }
  }
   if (aliveAaron) return 0;
   else if (aliveBob) return 1;
   else if (aliveCharlie) return 2;
}
int main()
{
  int i;
  int winner;
  double winAaron=0, winBob=0, winCharlie=0;
  for (i=0; i < DUENUM; i++)
  {
    winner = startDuel();
    if (winner == 0)
    {
      winAaron++;
    }
else if (winner == 1)
{
   winBob++;
 }
 else if (winner == 2)
 {
  winCharlie++;
 }
}
 cout << "Using the strategy of shooting at the best shooter  alive, but"  <<" Aaron intentionally misses on the first shot:" << endl;
 cout << "Aaron  win percentage: " << (winAaron/DUENUM) << endl;
 cout << "Bob win percentage: " << (winBob/DUENUM) << endl;
 cout << "Charlie win percentage: " << (winCharlie/DUENUM) << endl;
 system("pause"); 
 return 0;
}

Output:

1 0

Discussions

Post the discussion to improve the above solution.