Question

In: Computer Science

Hello there, I'm wondering can you do this problem without arrays? Using the given code on...

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;

}

Solutions

Expert Solution

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


Related Solutions

MIPS ASSEMBLY LANGUAGE (I'm using MARS) Can you provide me with the code of this, without...
MIPS ASSEMBLY LANGUAGE (I'm using MARS) Can you provide me with the code of this, without using DIV (4a-b)%[(2+c)/d] (a,b,c,d are user inputs)
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft and dft. Applied to discrete signals. If you can with an example.Thank you!!
Hello, I was wondering if you could tell me how to do a general Ledger? I...
Hello, I was wondering if you could tell me how to do a general Ledger? I just finish doing a Sales Journal, Purchase Journal, Cash Receipt Journal, Cash Disbursements Journal and a General Journal under one company Sound Bytes Electronics). What I wanted to know is do I use the General Journal to do the General Ledger? Or do I use the Special Journals and the General Journal to do the General Ledger? Can you put me in the right...
hello, I was wondering if I can see an example of a statistics prop1ztest. and a...
hello, I was wondering if I can see an example of a statistics prop1ztest. and a bit more information about when to use it.
So pretty much I need my code without the arrays, or lists. Please and thank you!...
So pretty much I need my code without the arrays, or lists. Please and thank you! Important: You may not use arrays, lists, or similar for your questions. This will be covered in the next module. The objective is to use conditionals in order to achieve the overall task. Checkpoint 3 is a continuation of the “Quiz” Programming Project. This module week, you will implement repetitive tasks in your program while using conditional and iteration statements in C#. Implement a...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed text. I'm having the absolute hardest time getting stared and figuring out how to identify in the code if the input needs to be encrypted/decrypted and identifying the string to encrypt/decrypt with. These are the instructions: Objectives Command line input File input and output Rethrowing exceptions Program Description Gaius Julius Caesar encoded his battle messages so that the opponent could not read them should...
Problem 4 ..... you can use Matlab Using the same initial code fragment as in Problem...
Problem 4 ..... you can use Matlab Using the same initial code fragment as in Problem 1, add code that calculates and plays y (n)=h(n)?x (n) where h(n) is the impulse response of an IIR bandstop filter with band edge frequencies 750 Hz and 850 Hz and based on a 4th order Butterworth prototype. Name your program p3.sce the below is the Problem 1 initail code .. you can use it Matlab The following cilab code generates a 10-second “chirp”...
Hello, I am wondering how I can determine the direction in which a wave is travelling...
Hello, I am wondering how I can determine the direction in which a wave is travelling given a function I must verify if it first is a solution to the wave equation by finding the partial derivatives then I do not know how to extrapolate the direction and speed of a wave. Thanks
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT