Write a c++ program that simulates a million of games in craps. This is the code
ID: 641277 • Letter: W
Question
Write a c++ program that simulates a million of games in craps. This is the code so far. I have the output at the bottom of my code.
#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
OUTPUT
Wins:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.