Code:
#include<stdio.h>
#include<stdlib.h>
#define NUM_GAMES 1000
//functions
int randomInt(int, int);
int rollDice(void);
int playOneGameOfCraps(void);
void analyzeWinsLoses(int [], int [], int []);
int totalWins(int []);
int totalLoses(int []);
float averageLengthOfGames(int [], int []);
void printResults(int [], int [], int, int, float);
main()
{
int i=0,status=0,twins=0,tloses=0;
int win[21],lose[21],game[NUM_GAMES];
float length=0;
srand(7);
for(i=0;i<=NUM_GAMES-1;i++)
{
status=playOneGameOfCraps();
game[i]=status;
}
analyzeWinsLoses(win[21], lose[21], game[NUM_GAMES]);
twins=totalWins(win[21]);
tloses=totalLoses(lose[21]);
length=avgLengthOfGames(win[21], lose[21]);
printResults(win[21], lose[21], twins, tloses, length);
}
int randomInt(int low, int high)
{
int k;
double d;
d=(double)rand()/(double)(RAND_MAX+1);
k=d*(high-low+1)+low;
return k;
}
int rollDice(void)
{
int stat;
stat=randomInt(1,6)+randomInt(1,6);
return stat;
}
int playOneGameOfCraps(void)
{
int gameStatus=0, sum=0, Mypoint=0, rolls=0;
sum=rollDice();
rolls++;
switch(sum)
{
case 7: gameStatus=1;
break;
case 11: gameStatus=1;
break;
case 2: gameStatus=2;
break;
case 3: gameStatus=2;
break;
case 12: gameStatus=2;
break;
default: gameStatus=0;
Mypoint=sum;
break;
}
while(gameStatus==0)
{
sum=rollDice();
rolls++;
if(sum==Mypoint)gameStatus=1;
else if(sum==7)gameStatus=2;
}
if(gameStatus==1)return rolls;
else
{
rolls=rolls*(-1);
return rolls;
}
}
void analyzeWinsLoses( int win[21], int lose[21], int game[NUM_GAMES])
{
int k=0 ;
for(k=0;k<=NUM_GAMES-1;k++)
{
if(k>21)
{
if(game[k]>0)win[20]+=1;
else lose[20]+=1;
}
else if(game[k]>0)win[game[k]]+=1;
else lose[game[k]]+=1;
}
}
int totalWins(int win[21])
{
int wins=0, y=0;
for(y=0;y<=21-1;y++)
{
wins+=win[y];
}
return wins;
}
int totalLoses(int lose[21])
{
int loses=0, y=0;
for(y=0;y<=21-1;y++)
{
loses+=lose[y];
}
return loses;
}
float avgLengthOfGames(int win[21], int lose[21])
{
int i=0;
float total=0;
for(i=0;i<=21-1;i++)
{
total=total+win[i]*i+lose[i]*i;
}
return total;
}
void printResults(int win[21], int lose[21], int twins, int tloses, float length)
{
float ratio;
ratio=twins/tloses;
printf(" Num-Rolls Games-Won Games-Lost\n\n");
printf(" 1 %d %d \n",win[0],lose[0]);
printf(" 2 %d %d \n",win[1],lose[1]);
printf(" 3 %d %d \n",win[2],lose[2]);
printf(" 4 %d %d \n",win[3],lose[3]);
printf(" 5 %d %d \n",win[4],lose[4]);
printf(" 6 %d %d \n",win[5],lose[5]);
printf(" 7 %d %d \n",win[6],lose[6]);
printf(" 8 %d %d \n",win[7],lose[7]);
printf(" 9 %d %d \n",win[8],lose[8]);
printf(" 10 %d %d \n",win[9],lose[9]);
printf(" 11 %d %d \n",win[10],lose[10]);
printf(" 12 %d %d \n",win[11],lose[11]);
printf(" 13 %d %d \n",win[12],lose[12]);
printf(" 14 %d %d \n",win[13],lose[13]);
printf(" 15 %d %d \n",win[14],lose[14]);
printf(" 16 %d %d \n",win[15],lose[15]);
printf(" 17 %d %d \n",win[16],lose[16]);
printf(" 18 %d %d \n",win[17],lose[17]);
printf(" 19 %d %d \n",win[18],lose[18]);
printf(" 20 %d %d \n",win[19],lose[19]);
printf("Over20 %d %d \n",win[20],lose[20]);
printf("\nRatio of wins\loses= %.4f\n",ratio);
printf("Avg Length of games= %.4f\n", length);
}