Thread: same code different output

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    75

    same code different output

    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);
    }

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>srand(7);
    This isn't the best way to do this, from the FAQ
    , you can use the following code
    Code:
    srand(time(NULL));
    And look at that, why you work so hard?
    Code:
    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]);
    Use a loop to print this:
    Code:
    for (i=0; i<20; i++)
        printf("%d%22d%22d\n",i+1,win[i],lose[i]);
    And what you mean different outputs? I think you're threating with random numbers, I didn't check the code, but maybe this is making the differences of the programs.
    Last edited by Vber; 03-18-2003 at 01:12 PM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    srand has been set to 7 bec these were the instructions of the professor....so that everybody has the same output. Now when our programs print the results, they are all the same except where he has one loss i have one win.....that the problem with the output.
    the assignment is there in my previous post
    http://cboard.cprogramming.com/showt...threadid=36036

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by kashifk
    srand has been set to 7 bec these were the instructions of the professor....so that everybody has the same output.
    You need to compile on the same machine the insructor will use then. Not all random number generator implementations will produce identical values.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    I believe this is so, then, because the compilers use different implementations of the rand function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  4. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM