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 []);
void avgLengthOfGames(int [], float *, float *, float *);
void printResults(int [], int [], int, int, float *, float *, float *);
void checkDice(int [][7]);
void DisplayArray(int [][7]);
int main()
{
int i=0,status=0,twins=0,tloses=0;
int win[21], lose[21], game[NUM_GAMES], dice[7][7];
float *avgAll=0,*avgWin=0,*avgLose=0;
srand(7);
for(i=0;i!=21;i++)
{
win[i]=0;
lose[i]=0;
}
for(i=0;i<=6;i++)
{
dice[i][i]=0;
}
for(i=0;i<=NUM_GAMES-1;i++)
{
status=playOneGameOfCraps();
game[i]=status;
}
analyzeWinsLoses(win, lose, game);
twins=totalWins(win);
tloses=totalLoses(lose);
avgLengthOfGames(game,&avgAll,&avgWin,&avgLose);
checkDice(dice);
printResults(win,lose,twins,tloses,&avgAll,&avgWin,&avgLose);
DisplayArray(dice);
system("PAUSE");
return 0;
}
int randomInt(int low, int high) //this functions return a random int i such that low<=i<=high
{
int k;
double d;
d=(double)rand()/(double)(RAND_MAX+1);
k=d*(high-low+1)+low;
return k;
}
int rollDice(void)//calls on the function random int twice, adds the numbers and returns the sum
{
return randomInt(1,6)+randomInt(1,6);
}
int playOneGameOfCraps(void)//plays on round of craps and returns the result until the player wins or loses
{
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;
}
}
//goes through game array and increments the counts in the win/lose arrays
void analyzeWinsLoses( int win[21], int lose[21], int game[NUM_GAMES])
{
int k=0 ;
for(k=0;k<=NUM_GAMES-1;k++)
{
if(abs(game[k])>20)
{
if(game[k]>0)win[20]+=1;
else lose[20]+=1;
}
else
{
if(game[k]>0) win[game[k]-1]+=1;
if(game[k]<0) lose[-game[k]-1]+=1;
}
}
}
int totalWins(int win[21])//computes and return the the total number of wins
{
int wins=0, y=0;
for(y=0;y<=21-1;y++)
{
wins+=win[y];
}
return wins;
}
int totalLoses(int lose[21])//computes and returns the total number of loses
{
int loses=0, y=0;
for(y=0;y<=21-1;y++)
{
loses+=lose[y];
}
return loses;
}
void avgLengthOfGames(int games[], float *avgAllp, float *avgWin, float *avgLose)
{
int i=0;
for(i=0;i!=NUM_GAMES;i++)
{
*avgAllp+=abs(games[i]);
}
*avgAllp/=(float)NUM_GAMES;
for(i=0;i!=NUM_GAMES;i++);
{
if(games[i]>=0)
{
*avgWin+=games[i];
}
}
*avgWin/=(float)NUM_GAMES;
for(i=0;i!=NUM_GAMES;i++)
{
if(games[i]<0)
{
*avgLose+=abs(games[i]);
}
}
*avgLose/=(float)NUM_GAMES;
}
void checkDice(int dices[7][7])
{
int i=0,j=0,k=0;
for(i=0;i!=NUM_GAMES;i++)
{
j=randomInt(1,6);
k=randomInt(1,6);
dices[j][k]++;
}
}
void printResults(int win[21], int lose[21], int twins, int tloses, float *avgAll, float *avgWin, float *avgLose)//prints out the final result on the screen
{
double ratio;
ratio=(double)twins/tloses;
printf(" Num-Rolls Games-Won Games-Lost\n\n");
printf(" 1%22d%22d \n",win[0],lose[0]);
printf(" 2%22d%22d \n",win[1],lose[1]);
printf(" 3%22d%22d \n",win[2],lose[2]);
printf(" 4%22d%22d \n",win[3],lose[3]);
printf(" 5%22d%22d \n",win[4],lose[4]);
printf(" 6%22d%22d \n",win[5],lose[5]);
printf(" 7%22d%22d \n",win[6],lose[6]);
printf(" 8%22d%22d \n",win[7],lose[7]);
printf(" 9%22d%22d \n",win[8],lose[8]);
printf(" 10%22d%22d \n",win[9],lose[9]);
printf(" 11%22d%22d \n",win[10],lose[10]);
printf(" 12%22d%22d \n",win[11],lose[11]);
printf(" 13%22d%22d \n",win[12],lose[12]);
printf(" 14%22d%22d \n",win[13],lose[13]);
printf(" 15%22d%22d \n",win[14],lose[14]);
printf(" 16%22d%22d \n",win[15],lose[15]);
printf(" 17%22d%22d \n",win[16],lose[16]);
printf(" 18%22d%22d \n",win[17],lose[17]);
printf(" 19%22d%22d \n",win[18],lose[18]);
printf(" 20%22d%22d \n",win[19],lose[19]);
printf("Over20%22d%22d \n",win[20],lose[20]);
printf("total%23d%22d \n",twins,tloses);
printf("\nRatio of wins/loses= %.4f\n",ratio);
printf("Avg Length of winning games=%.4f\n",*avgWin);
printf("Avg Length of losing games=%.4f\n", *avgLose);
printf("Avg Length of games= %.4f\n", *avgAll);
}
void DisplayArray( int dices[7][7])
{
printf("\nNumber of rolls of two dice out of 1000 rolls that came up \n");
printf(" 1 2 3 4 5 6\n");
printf("1 %d %d %d %d %d %d\n",dices[1][1],dices[1][2],dices[1][3],dices[1][4],dices[1][5],dices[1][6]);
printf("2 %d %d %d %d %d %d\n",dices[2][1],dices[2][2],dices[2][3],dices[2][4],dices[2][5],dices[2][6]);
printf("3 %d %d %d %d %d %d\n",dices[3][1],dices[1][2],dices[3][3],dices[3][4],dices[3][5],dices[3][6]);
printf("4 %d %d %d %d %d %d\n",dices[4][1],dices[4][2],dices[4][3],dices[4][4],dices[4][5],dices[4][6]);
printf("5 %d %d %d %d %d %d\n",dices[5][1],dices[5][2],dices[5][3],dices[5][4],dices[5][5],dices[5][6]);
printf("6 %d %d %d %d %d %d\n",dices[6][1],dices[6][2],dices[6][3],dices[6][4],dices[6][5],dices[6][6]);
}