l of tice writing call-by-reference function and using random numbers In the lan
ID: 3589740 • Letter: L
Question
l of tice writing call-by-reference function and using random numbers 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%. 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 thre was one man standing -the greatest puzzler of all time 1. 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 2. Strategies shoot at the most accurate shooter still alive on the grounds b. a. shoot at random target still alive on the grounds Write a second function that use the shoot function to simulate the entire fuel using the above mentioned strategy respectively, it should loop until only one contestant is left. The function will return a variable that indicates who won the duel In your main function, test run your functions and output the probability that each contestant will win with the above mentioned strategyExplanation / Answer
#include <iostream>
using namespace std;
#include<string.h>
double fRand(double fMin, double fMax)
{
double f = ( rand() % 1000 )/ 1000.0;
return fMin + f * (fMax - fMin);
}
void shoot(bool& targetAlive, double accuracy )
{
double get_shot = fRand(0.0,1.0);
if(get_shot<accuracy)
{
targetAlive = false;
}
}
int chooseRandomPerson(bool alive[], int person)
{
while(true)
{
int num = rand() % 3;
if(person!= num && alive[num]!=false)
return num;
}
}
int checkBestShooter(double accuracy[], bool alive[])
{
int i;
double acc = 0.0;
int index = -1;
for(i=0; i<3; i++)
{
if(alive[i] == true)
{
if(acc < accuracy[i])
{
acc = accuracy[i];
index = i;
}
}
}
return index;
}
int strategy(double accuracy[], bool alive[])
{
int count = 3;
int i=0,j,k;
//cout << "in Strategy";
while( count > 1 )
{
for(int n=0; n<3; n++)
{
if(alive[n] == false)
{
count--;
//cout << n;
}
}
i = i%3;
//cout << i;
if(alive[i] == true)
{
j = checkBestShooter(accuracy,alive);
if(j==i)
{
k = chooseRandomPerson(alive,j);
shoot(alive[k],accuracy[i]);
}
else
{
shoot(alive[j],accuracy[i]);
}
}
}
for(int n=0; n<3; n++)
{
if(alive[n] == true)
return n;
}
}
int main()
{
double accuracy[3] = { 0.333, 0.5, 1 };
bool alive[3] = {true,true,true};
//cout<<"calling strategy";
int person;
string player[] = {"Aaron", "Bob", "Cahrlie"};
int probability[3]={0,0,0};
int i=0;
for(i=0 ; i<100; i++)
{
person = strategy(accuracy,alive);
cout<<"The game ";
cout<< i ;
cout << " owned by " ;
cout<<player[person] << endl;
probability[person]++;
}
cout << " Aron alive probability ";
cout << probability[0]/100;
cout << " Bob alive probability ";
cout << probability[1]/100;
cout << " Charlie alive probability ";
cout << probability[2]/100;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.