In: Computer Science
Hello there, I'm wondering can you do this problem without arrays? Using the given code on C++ Platform.
Let me know ASAP.
#include <iostream>
#include <time.h>
using namespace std;
void shoot(bool &targetAlive, double accuracy)
{
double random = (rand() % 1000) / 1000.;
targetAlive = !(random < accuracy);
}
int startDuel(int initialTurn)
{
bool personAlive[3] = {true, true, true};
double accuracy[3] = {0.333, 0.5, 1.0};
int turn = initialTurn; // which person has to shoot, initialTurn represents the first person who shoots
int aliveCount = 3; // total persons still alive
while (aliveCount > 1) { // loop until only one person is alive
if(!personAlive[turn]) { // if person is dead
turn = (turn + 1) % 3; // give turn to next person
continue;
}
int highestAccuracyPersonAlive = -1;
int highestAccuracy = -1;
for (int i = 0; i < 3; i++)
{
if (i != turn && personAlive[i] && accuracy[i] > highestAccuracy) { // person has the highest accuracy and is alive, so far
highestAccuracyPersonAlive = i;
highestAccuracy = accuracy[i];
}
}
// shoot the person with the highest accuracy and who is still alive
shoot(personAlive[highestAccuracyPersonAlive], accuracy[turn]);
if(!personAlive[highestAccuracyPersonAlive])
aliveCount--; // decrease alive count if person shot is dead
turn = (turn + 1) % 3; // give the turn to shoot to the next person
}
if(personAlive[0])
return 0;
if(personAlive[1])
return 1;
return 2;
}
int main() {
srand((unsigned) time(NULL));
int wins[3] = {0, 0, 0};
for (int i = 0; i < 1000; i++)
{
wins[startDuel(0)]++;
}
cout << "Probability of Aaron winning: " << wins[0]/1000. << endl;
cout << "Probability of Bob winning: " << wins[1]/1000. << endl;
cout << "Probability of Charlie winning: " << wins[2]/1000. << endl;
wins[0] = wins[1] = wins[2] = 0;
for (int i = 0; i < 1000; i++)
{
// Counterintuitive strategy is equivalent to giving the first turn to shoot to Bob (person with index 1)
wins[startDuel(1)]++;
}
cout << "Probability of Aaron winning with counterintuitive strategy: " << wins[0]/1000. << endl;
cout << "Probability of Bob winning counterintuitive strategy: " << wins[1]/1000. << endl;
cout << "Probability of Charlie winning counterintuitive strategy: " << wins[2]/1000. << endl;
return 0;
}
Hey! Your question is good .
Yes, we can do this without using arrays as you see the size of array is very less which is mostly less than 3 so, we can take variables for that without array.
#include <iostream>
#include <time.h>
using namespace std;
void shoot(bool &targetAlive, double accuracy)
{
double random = (rand() % 1000) / 1000.;
targetAlive = !(random < accuracy);
}
int startDuel(int initialTurn)
{
//change
bool personAliveOne ,personAliveTwo,personAliveThree;
personAliveOne = personAliveTwo = personAliveThree = true;
//change
double accuracyOne,accuracyTwo,accuracyThree ;
accuracyOne = 0.333;
accuracyTwo = 0.5;
accuracyThree = 1.0;
int turn = initialTurn; // which person has to shoot, initialTurn represents the first person who shoots
int aliveCount = 3; // total persons still alive
while (aliveCount > 1) { // loop until only one person is alive
//change
if((turn == 0 && !personAliveOne) || (turn == 1 && !personAliveTwo) || (turn == 2 && !personAliveThree)) { // if person is dead
turn = (turn + 1) % 3; // give turn to next person
continue;
}
int highestAccuracyPersonAlive = -1;
int highestAccuracy = -1;
for (int i = 0; i < 3; i++)
{
//change
if (i != turn && ((i == 0 && personAliveOne) || (i == 1 && personAliveTwo) || (i == 2 && personAliveThree))
&& ((i == 0 && accuracyOne>highestAccuracy) || (i == 1 && accuracyTwo>highestAccuracy) || (i == 2 && accuracyThree>highestAccuracy))) { // person has the highest accuracy and is alive, so far
highestAccuracyPersonAlive = i;
//change
highestAccuracy = (i == 0)?accuracyOne : (i == 1)?accuracyTwo :accuracyThree;
}
}
// shoot the person with the highest accuracy and who is still alive
//change
shoot( (highestAccuracyPersonAlive == 0)?personAliveOne : (highestAccuracyPersonAlive == 1)?personAliveTwo :personAliveThree
, (turn == 0)?accuracyOne : (turn == 1)?accuracyTwo :accuracyThree);
//change
if((highestAccuracyPersonAlive == 0 && !personAliveOne) || (highestAccuracyPersonAlive == 1 && !personAliveTwo) || (highestAccuracyPersonAlive == 2 && !personAliveThree))
aliveCount--; // decrease alive count if person shot is dead
turn = (turn + 1) % 3; // give the turn to shoot to the next person
}
if(personAliveOne)
return 0;
if(personAliveTwo)
return 1;
return 2;
}
int main() {
srand((unsigned) time(NULL));
//change
int winsOne,winsTwo,winsThree;
winsOne = winsTwo = winsThree = 0;
for (int i = 0; i < 1000; i++)
{
//change
int temp = startDuel(0);
(temp == 0)?winsOne++:(temp == 1)?winsTwo++:winsThree++;
}
cout << "Probability of Aaron winning: " << winsOne/1000. << endl;
cout << "Probability of Bob winning: " << winsTwo/1000. << endl;
cout << "Probability of Charlie winning: " << winsThree/1000. << endl;
winsOne = winsTwo = winsThree = 0;
for (int i = 0; i < 1000; i++)
{
// Counterintuitive strategy is equivalent to giving the first turn to shoot to Bob (person with index 1)
//change
int temp = startDuel(1);
(temp == 0)?winsOne++:(temp == 1)?winsTwo++:winsThree++;
}
cout << "Probability of Aaron winning with counterintuitive strategy: " << winsOne/1000. << endl;
cout << "Probability of Bob winning counterintuitive strategy: " << winsTwo/1000. << endl;
cout << "Probability of Charlie winning counterintuitive strategy: " << winsThree/1000. << endl;
return 0;
}
So, as you can see at evey point where we had array we replace it with variables called (name)One,Two,Three and when we need that we used either matching it with the current index of previous array or with the ternary operator
But as you can see the program is now little complicated and dificult to understand.
So, thats why we used array to reduce the code.
I hope I am able to solve your problem then do give it a like.
It really helps :)