Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program is supposed to use loops and call by reference to simulate a 3 person duel. However, my output is just Aaron Misses. Bob

This program is supposed to use loops and call by reference to simulate a 3 person duel. However, my output is just "Aaron Misses. Bob Misses." repeatededly. I've attached the instructions I was given aswell as my code. (C++)

# include

# include

# include

# include

using namespace std;

// constants

const int NUMBER_DUELS = 10000;

const double CHARLIE_ACCUR = 1.0;

const double BOB_ACCUR = 0.5;

const double AARON_ACCUR = (1.0 / 3);

bool A_alive, B_alive, C_alive = true;

// prototypes

bool at_least_two_alive(bool A_alive, bool B_alive, bool C_alive);

/* Input: A_alive indicates whether Aaron is alive */

/* B_alive indicates whether Bob is alive */

/* C_alive indicates whether Charlie is alive */

/* Return: true if at least two are alive */

/* otherwise return false */

/*

* Add other function prototypes below

*/

void Aaron_shoots1(bool& B_alive, bool& C_alive);

/* Strategy 1: Use call by reference

* Input: B_alive indicates whether Bob alive or dead

* C_alive indicates whether Charlie is alive or dead

* Return: Change B_alive into false if Bob is killed.

* Change C_alive into false if Charlie is killed.

*/

void Bob_shoots(bool& A_alive, bool& C_alive);

/* Call by reference

* Input: A_alive indicates if Aaron is alive or dead

* C_alive indicates whether Charlie is alive or dead

* Return: Change A_alive into false if Aaron is killed.

* Change C_alive into false if Charlie is killed.

*/

void test_at_least_two_alive(void);

/* This is a test driver for at_least_two_alive() */

/*

* Add more prototypes for other test drivers below

*/

void Charlie_shoots(bool& A_alive, bool& B_alive);

/* Call by reference

* Input: A_alive indicates if Aaron is alive or dead

* B_alive indicates whether Bob is alive or dead

* Return: Change A_alive into false if Aaron is killed.

* Change B_alive into false if Bob is killed.

*/

int main() {

//Strategy 1:

int aaronWins, bobWins, charlieWins = 0;

int counter = 0;

while (counter

Aaron_shoots1(B_alive, C_alive);

Bob_shoots(A_alive, C_alive);

if (at_least_two_alive(A_alive, B_alive, C_alive) == true) {

Charlie_shoots(A_alive, B_alive);

}

if (at_least_two_alive(A_alive, B_alive, C_alive) == true) {

Aaron_shoots1(B_alive, C_alive);

}

if (at_least_two_alive(A_alive, B_alive, C_alive) == true) {

Bob_shoots(A_alive, C_alive);

}

if (A_alive == true) {

aaronWins++;

}

else if (B_alive == true) {

bobWins++;

}

else {

charlieWins++;

}

counter++;

}

}

/* Implementation of at_least_two_alive() */

bool at_least_two_alive(bool a_alive, bool b_alive, bool c_alive) {

if ((a_alive && b_alive) || (a_alive && c_alive) || (b_alive && c_alive)) {

return true;

}

else {

return false;

}

}

/* Implementation of Aaron_shoots1() */

void Aaron_shoots1(bool& B_alive, bool& C_alive) {

double accuracy = (rand() % 100);

if ((B_alive && C_alive) == true || (B_alive == false && C_alive == true)) {

if (accuracy

C_alive = false;

cout

}

else {

cout

}

}

else if (B_alive == true && C_alive == false) {

if (accuracy

B_alive = false;

cout

}

else {

cout

}

}

}

/* Implementation of Bob_shoots() */

void Bob_shoots(bool& A_alive, bool& C_alive) {

double accuracy = (rand() % 100);

if ((A_alive && C_alive) == true || (A_alive == false && C_alive == true)) {

if (accuracy

C_alive = false;

cout

}

else {

cout

}

}

else if (A_alive == true && C_alive == false) {

if (accuracy

A_alive = false;

cout

}

else {

cout

}

}

}

/* Implementation of Charlie_shoots() */

void Charlie_shoots(bool& A_alive, bool& B_alive) {

double accuracy = (rand() % 100);

if ((A_alive && B_alive) == true || (A_alive == false && B_alive == true)) {

if (accuracy

B_alive = false;

cout

}

else {

cout

}

}

else if (A_alive == true && C_alive == false) {

if (accuracy

A_alive = false;

cout

}

else {

cout

}

}

}

/* Implementation of Aaron_shoots2() */

void Aaron_shoots2(bool& B_alive, bool& C_alive) {

int strategy = 1;

if (strategy == 1) {

double accuracy = (rand() % 100);

if ((A_alive && B_alive) == true || (A_alive == false && B_alive == true)) {

strategy++;

cout

}

else if (A_alive == true && C_alive == false) {

strategy++;

cout

}

}

else if (strategy == 2) {

double accuracy = (rand() % 100);

if ((B_alive && C_alive) == true || (B_alive == false && C_alive == true)) {

if (accuracy

C_alive = false;

cout

}

else {

cout

}

}

else if (B_alive == true && C_alive == false) {

if (accuracy

B_alive = false;

cout

}

else {

cout

}

}

}

}

/* Implementation of the test driver for at_least_two_alive() */

void test_at_least_two_alive(void) {

cout

cout

assert(true == at_least_two_alive(true, true, true));

cout

cout

assert(true == at_least_two_alive(false, true, true));

cout

cout

assert(true == at_least_two_alive(true, false, true));

cout

cout

assert(true == at_least_two_alive(true, true, false));

cout

cout

assert(true == at_least_two_alive(false, false, true));

cout

cout

assert(true == at_least_two_alive(false, true, false));

cout

cout

assert(true == at_least_two_alive(true, false, false));

cout

cout

assert(true == at_least_two_alive(false, false, false));

cout

cout

cin.ignore().get();

}

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

In the land of Puzzlevania, Aaron, Bob, and Charlie had an argument over which one of them was the greatest puzzle-solver of all time. To end the argument once and for all, they agreed on a duel to the death (this makes sense?). Aaron was a poor shot and only hit this target with a probability of 1/3. Bob was a bit better and hit his target with a probability of 1/2. Charlie was an expert marksman and never missed. A hit means a kill and the person hit drops out of the duel To compensate for the inequities in their marksmanship skills, the three decided that they would fire in turns, starting with Aaron, followed by Bob, and then by Charlie. The cycle would repeat until there was one man standing. That man would be remembered for all time as the Greatest Puzzle-Solver of All Time. An obvious and reasonable 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 program to simulate the duel using this strategy. Your program should use random numbers and the probabilities given in the problem to determine whether a shooter hits his target. You will likely want to create multiple functions to complete the problem. My solution had only one function to simulate the duels and it passed in the odds and the three guys as pass-by-reference parameters. Once you can simulate a duel, add a loop to your program that simulates 10,000 duels. Count the number of times that each contestant wins and print the probability of winning for each contestant (e.g., for Aaron your might output "Aaron won 3612/|0000 duels or 36.12%) Strategy 2: An alternative strategy for Aaron is to intentionally miss on his first shot. Write a function to simulate Strategy 2. Your program will determine which strategy is better for Aaron. Note: You must provide the following user interface. Red text might be different for different runs due to the random number. You do not need to display text in red

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 3 Lnai 6323

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

3642159389, 978-3642159381

More Books

Students also viewed these Databases questions