i wrote this code(with some help of ppl from here) and my friend wrote some code to do the same code. Both of them are identical, but even if they are compiled on the same machine the their output is diff. I am posting both the code....could please someonne have a look.
[code]
/* Craps program that simulates a game of craps 1000 times and then prints out the results*/
#include<stdio.h>
#include<stdlib.h>
#define NUM_GAMES 1000
//functions declarations
int randomInt(int, int);
int rollDice(void);
int playOneGameOfCraps(void);
void analyzeWinsLoses(int [], int [], int []);
int totalWins(int []);
int totalLoses(int []);
float avgLengthOfGames(int []);
void printResults(int [], int [], int, int, float);
int 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!=21;i++)
{
win[i] = 0;
lose[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);
length=avgLengthOfGames(game);
printResults(win, lose, twins, tloses, length);
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
{
int stat;
stat=randomInt(1,6)+randomInt(1,6);
return stat;
}
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;
}
float avgLengthOfGames(int games[])//computes and returns the average length(number of rolls)of the games
{
int i=0;
float total=0;
for(i=0;i!=NUM_GAMES;i++)
{
total+=abs(games[i]);
}
total/= (float)NUM_GAMES;
return total;
}
void printResults(int win[21], int lose[21], int twins, int tloses, float length)//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 games= %.4f\n", length);
}
[\code]
and here is my friends code
Code:#include<stdio.h> #include<stdlib.h> #define NUM_GAMES 1000 int randomInt(int,int); int rollDice(void); int playOneGameOfCraps(void); void analyzeWinsLoses(int [], int win[], int lose[]); int totalWins(int []); int totalLoses(int []); float averageLengthOfGames(int []); void printResults(int,int,float,int[],int[]); main() { int i=0, k=0,g=0,status=0,twins=0,tloses=0; float length=0; int win[21],lose[21],game[NUM_GAMES]; srand(7); for(i=0;i<=20;i++) { win[i]=0; lose[i]=0;} for(i=0;i<(NUM_GAMES-1);i++) game[i]=0; for(i=0;i<(NUM_GAMES-1);i++) { status=playOneGameOfCraps(); game[i]=status; } /*for(k=0;k<(NUM_GAMES-1);k++) { g=abs(game[k]); if(game[k]>20||game[k]<-20) { if(game[k]<0)lose[20]++; else win[20]++; } if(game[k]<0&&game[k]>-20) lose[g-1]++; else win[game[k]-1]++; }*/ analyzeWinsLoses(game,win,lose); twins=totalWins(win); tloses=totalLoses(lose); // printf("\nTotWins: %d\nTotLosses: %d\n",twins,tloses); length=averageLengthOfGames(game); //printf("\nLength: %f\n",length); printResults(twins,tloses,length,win,lose); system("PAUSE"); return 0; } int playOneGameOfCraps(void) { int gameStatus=0,sum=0,Mypoint=0,rolls=0; sum=rollDice(); rolls++; switch(sum) { case 7: case 11: gameStatus=1; break; case 2:case 3: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 return (rolls*(-1)); } 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) { return (randomInt(1,6)+randomInt(1,6)); } void analyzeWinsLoses(int game[], int win[], int lose[]) { int k=0, g=0; for(k=0;k<=(20);k++) { win[k]=0; lose[k]=0;} for(k=0;k<=(NUM_GAMES-1);k++) { g=abs(game[k]); //printf("\ng=%d",g); if(game[k]>20||game[k]<-20) { if(game[k]<0)lose[20]++; else win[20]++; } if(game[k]<0&&game[k]>-20)lose[g-1]++; else win[game[k]-1]++; } } int totalWins(int win[]) { int cwins=0,y=0; for(y=0;y<21;y++) { cwins+=win[y]; } return cwins; } int totalLoses(int lose[]) { int closes=0,y=0; for(y=0;y<21;y++) { closes+=lose[y]; } return closes; } float averageLengthOfGames(int game[]) { int i=0,g=0,total=0; float length=0; for(i=0;i<=NUM_GAMES;i++) { g=abs(game[i]); total+=g; } // printf("\ntotRolls: %d\n",total); length=(float)total/NUM_GAMES; return length; } void printResults(int twins, int tloses, float length, int wins[], int loses[]) { double ratio=0; printf("Simulation of 1000 Games of Craps.\n\n"); printf("Num-Rolls Games-Won Games-Lost\n"); printf(" 1%22d%18d\n",wins[0],loses[0]); printf(" 2%22d%18d\n",wins[1],loses[1]); printf(" 3%22d%18d\n",wins[2],loses[2]); printf(" 4%22d%18d\n",wins[3],loses[3]); printf(" 5%22d%18d\n",wins[4],loses[4]); printf(" 6%22d%18d\n",wins[5],loses[5]); printf(" 7%22d%18d\n",wins[6],loses[6]); printf(" 8%22d%18d\n",wins[7],loses[7]); printf(" 9%22d%18d\n",wins[8],loses[8]); printf("10%22d%18d\n",wins[9],loses[9]); printf("11%22d%18d\n",wins[10],loses[10]); printf("12%22d%18d\n",wins[11],loses[11]); printf("13%22d%18d\n",wins[12],loses[12]); printf("14%22d%18d\n",wins[13],loses[13]); printf("15%22d%18d\n",wins[14],loses[14]); printf("16%22d%18d\n",wins[15],loses[15]); printf("17%22d%18d\n",wins[16],loses[16]); printf("18%22d%18d\n",wins[17],loses[17]); printf("19%22d%18d\n",wins[18],loses[18]); printf("20%22d%18d\n",wins[19],loses[19]); printf("over 20%17d%18d\n",wins[20],loses[20]); if(tloses!=0) ratio=(double)twins/tloses; else ratio=0; printf("Ratio of wins/loses = %.4f\n",ratio); printf("Avg Length of games = %.4f\n",length); }