Iniciar sesiónCrear cuenta

# Dev - C++ - C++ ayuda

Vista:

## C++ ayuda

Publicado por Juan (3 intervenciones) el 14/11/2009 19:45:41
Estoy empesando en c++ con un libro que me compre y no entiendo estos dos ejercicios por favor que alguien me pueda ayudar gracias. Gracias anticipadas.

1) 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. Aron 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.
A) Write a function to simulate a single shot. It should use the fallowing declaration:
Void shoot(bool& targetAlive, double accuracy);

This would simulate someone shootine at targetAlive with the given accuracy by generating a random number between 0 and 1. If the random number is less tha accuracy, then the target is hit and targetAlive should be set to false. Appendix 4 illustrates how to generate random numbers.

For example, if Bob is shooting at Charlie, this could be invoked as: shoot(charlieAlive, 0.5);
B) An obvious 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 second function named startDuel that uses the shoot function to simulate an entire duel using this strategy. It should loop until only one contestant is left, invoking the shoot function with the proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.
C) In your main function, invoke the startDuel function 1,000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter left alive.
D) A counterintuitive strategy is for Aaron to intentionally miss on his first shot. Thereafter, everyone uses the strategy of shooting at the accurate shooter left alive. This strategy means that Aaron is guaranteed to live past the first round , since Bob and Charlie will fire at each other. Modify the program to accommodate this new strategy and output the probability of winning for each contestant.

5) Write a program that will correct a C++ program that has errors in which operator, <<or>>, it uses with cin and cout. The program replaces each (incorrect) occurrence of
cin<<
with the corrected version
cin >>
and each (incorrect) occurrence of
cout >>
with the corrected version
cout <<
For an easier version, assume that these is always exactly one blanck space between any occurrence of cin and a following << and similarly assume that there is always exactly one blanck space between each occurrence of cout and a following>>
For a harder version, allow for the possibility that there may be any number of blanks, even zero blanks between cin and << and between cout and >>. In this harder case the replacement corrected version has only one blank between the cin or cout and the following operator. The program to be corrected is in one file and the corrected version is output to a second file. Your programs should define a function that is called with the input- and output0 file streams as arguments.
Valora esta pregunta
0

## RE:C++ ayuda

Publicado por victor (6 intervenciones) el 17/11/2009 12:14:36
El primer ejercicio te pide un programa en el que 2 jugadores se baten en un duelo siendo Aaron el peor de ellos con un 33% de posibilidades de acierto, Bob con una posibilidad de acierto del 50% y charlie el mejor de ellos(100%) . deberias crearte una estructura o clase en la que se pudiese almacenar la informacion de cada jugador algo parecido a esto:

struct Player
{
char* nombre;
bool targetAlive;
double accuracy;
};

y asignar a cada uno de los 3 jugadores los valores correspondientes en esos 3 campos, para Aaron seria:

Player player1;
player1.nombre="aaron";
player1.targetAlive=true;
player1.accuracy=0.3333;

A partir de aqui debes implementar las 2 funciones Shoot() y StartDuel() y entiendo que deberias hacer un menu en el main() donde selecciones los 2 jugadores que se van a batir en duelo ( StartDuel(jugador1,jugador2) ), aunque el ejercicio te pide que relices un bucle de 1000 iteraciones pero bueno yo haria un menu y ya esta, dentro de esta función llamaras a shoot() donde lo unico que haces es comparar el valor devuelto por la funcion aleatoria con el valor de accuracy si este es menor el jugador al que le han disparado muere y el targetAlive de este jugador pasa a false.

Para generar números aleatorios utiliza estas funciones:

rand()%10; //para generar numeros del 0 al 9
srand(time(null)) //determina la semilla que usara rand(), esta función hace que la secuencia se salida de rand() sea más aleatoria. No olvides incluir las librerias de estas funciones.

Respecto al ejercicio 2 te pide hacer un programa que detecte una sintaxis incorrecta en el cin o en el cout (cin>>,cout<<)

Espero que te sirva de ayuda, un saludo.
Valora esta respuesta
0