Write a c++ program that simulates a million of games in craps. I am having a tr
ID: 641239 • Letter: W
Question
Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far.
#include <iostream>
#include <cstdlib>// contains prototypes for functions srand and rand
#include <ctime>// contains prototype for function time
#include <iomanip>
using namespace std;
int rollDice(); // rolls dice, calculates and displays sum
void printstats();
int totroll = 0, games, point, roll;
int won[11] = { }, lost[11]= { };
int numberofRolls;
enum Status { CONTINUE, WON, LOST }; // all caps in constants
int main()
{
cout << "Histogram showing the likelihood of winning for a given number of rolls. ";
cout << "Rolls ";
int myPoint; // point if no win or loss on first roll
Status gameStatus; // can contain CONTINUE, WON or LOST
// randomize random number generator using current time
srand(time(0));
int sumOfDice = rollDice(); // first roll of the dice
numberofRolls = 0;
// determine game status and point (if needed) based on first roll
switch (sumOfDice)
{
case 7: // win with 7 on first roll
case 11: // win with 11 on first roll
gameStatus = WON;
won[NULL]++;
break;
case 2: // lose with 2 on first roll
case 3: // lose with 3 on first roll
case 12: // lose with 12 on first roll
gameStatus = LOST;
lost[NULL]++;
// break;
default: // did not win or lose, so remember point
gameStatus = CONTINUE; // game is not over
myPoint = sumOfDice; // remember the point
} // end switch
// while game is not complete
while (gameStatus == CONTINUE) // not WON or LOST
{
sumOfDice = rollDice(); // roll dice again
numberofRolls++;
// determine game status
if (sumOfDice == myPoint){ // win by making point
gameStatus = WON;
won[numberofRolls < 11 ? numberofRolls: 10]++;}
else
if (sumOfDice == 7) // lose by rolling 7 before point
gameStatus = LOST;
lost[numberofRolls < 11 ? numberofRolls: 10 ]++;
} // end while
} // end main
// roll dice, calculate sum and display results
int rollDice()
{
// pick random die values
int die1 = 1 + rand() % 6; // first die roll
int die2 = 1 + rand() % 6; // second die roll
int sum = die1 + die2; // compute sum of die values
return sum;
} // end function rollDice
the output i need to get
Histogram showing the likelihood of winning for a given number of rolls.
Rolls
1 wins: ********************************************
losses: **********************
2 wins: ***************
losses: **********************
3 wins: ***********
losses: ****************
4 wins: ********
losses: ***********
5 wins: ******
losses: ********
6 wins: ****
losses: ******
7 wins: ***
losses: ****
8 wins: **
losses: ***
9 wins: *
losses: **
10 wins: *
losses: **
more wins: ***
losses: ****
Percentage of games that are wins: 49%
Average number of rolls in a craps game: 3.4
Explanation / Answer
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
int dice1;
int dice2;
int sum_of_dice;
int point;
int number_of_wins;
int number_of_lose;
double probability_of_winning;
double probability_of_losing;
int number_of_times_played;
int main()
{
srand(time(0));
do
{
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
sum_of_dice = dice1 + dice2;
if (sum_of_dice == 7 || sum_of_dice == 11)
{
cout << "Congratulations you have won!" << endl;
}
else if (sum_of_dice == 2 || sum_of_dice == 3 || sum_of_dice == 12)
{
cout << "Better luck next time the house has one!" << endl;
}
else (sum_of_dice == 4 || sum_of_dice == 5 || sum_of_dice == 6 || sum_of_dice == 8 || sum_of_dice == 9 || sum_of_dice == 10);
{
do {
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
int sum2 = dice1 + dice2;
if( sum2 == sum_of_dice )
{
cout << "Congratulations you have won!" << endl;
break;
} else if( sum2 == 7 )
{
cout << "Better luck next time the house has one!" << endl;
break;
}
} while(true);
}
} while (++number_of_times_played != 1000000);
number_of_wins = number_of_times_played - number_of_lose;
number_of_lose = number_of_times_played - number_of_wins; // It is now telling me that I have never lost.
probability_of_winning = number_of_wins / 10000;
probability_of_losing = number_of_lose / 10000;
cout << "Out of 1 million games you won: " << number_of_wins << endl;
cout << "Out of 1 million games you lose: " << number_of_lose << endl;
cout << "The probability for the Player to win is: " << probability_of_winning << setw(2) << "%" << endl;
cout << "the probability for House to win is: " << probability_of_losing << setw(2) << "%" << endl;
return 0;
}//End Code!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.