Adam, Bob, and Charlie agreed on a duel to the death (using paintball guns). Adam was a poor shot and only hit his target with a probability of 1/3. Bob hit his target with a probability of 1/2. Charlie never missed. A hit means a "kill" and the person hit drops out of the duel. They decided to fire in turns, staring with Adam, followed by Bob, and then Charlie. The cycle would repeat until one man was standing. An obvious and reasonably strategy is for each man to shoot at the most accurate opponent still alive.
Write a program to simulate the duel and this strategy. Your program should use this pseudo random number function (Xn+1 =(a*Xn)+c) where a = 22695477, c = 1, and Xn is a single unsigned integer parameter and the probabilities given in the problem to determine whether a shooter hits his target. Once you can simulate a single duel, add an outer loop that simulates 10,000 duels. Count the number of times that each contestant wins and display his winning percentage
Hint: Use the % operator to convert the pseudo-random numbers to a hit or miss
you can use the opening and closing code tags to post your code. This makes it a lot more readable and also keeps the formatting you used like follows:
int main()
{
int i;
char win;
double winners[3]={0,0,0};
int times;
cout<< "How many times do you want to play?"<<endl;
cin>> times;
for(i=0; i<times; i++)
{
win= duel(times);
if (win=='A')
{
winners[0] += 1;
}
elseif (win=='B')
{
winners[1] += 1;
}
else
winners[2] += 1;
}
cout<< "Adam won " << (winners[0]/times)*100 << " percent of the time." << endl;
cout<< "Bob won " << (winners[1]/times)*100 << " percent of the time." << endl;
cout<< "Charlie won " << (winners[2]/times)*100 << " percent of the time." << endl;
}
If you are studying C++ consider downloading a C++ compiler. I use one of either MinGW, Visual C++ or GNU using Cygwin for projects I need to compile via Linux.
PS: Dont forget to return an integer in your main function return 0;