garbage values even after initialization

This is a discussion on garbage values even after initialization within the C Programming forums, part of the General Programming Boards category; why does this code spit out garbage values even after being initialized Code: #include<stdio.h> #include<stdlib.h> #define NUM_GAMES 1000 //functions int ...

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

    garbage values even after initialization

    why does this code spit out garbage values even after being initialized
    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]);
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    here is the output so that you can get a better idea

    Num-Rolls Games-Won Games-Lost

    1 216 115
    2 77 120
    3 59 83
    4 38 52
    5 19 49
    6 20 35
    7 13 19
    8 9 20
    9 5 6
    10 7 3
    11 2 6
    12 1 6
    13 3 3
    14 1 3
    15 1 3
    16 0 3
    17 1 0
    18 0 1
    19 0 0
    20 0 0
    Over20 0 1
    total 472 528

    Ratio of wins/loses= 0.8939
    Avg Length of winning games=0.1150
    Avg Length of losing games=1.9950
    Avg Length of games= 3.3060

    Number of rolls of two dice out of 1000 rolls that came up
    1 2 3 4 5 6
    1 25 -858993429 -858993431 -858993440 -858993427 -858993434
    2 -858993438 37 -858993432 -858993438 -858993424 -858993428
    3 -858993427 -858993429 29 -858993429 -858993431 -858993425
    4 -858993439 -858993427 -858993431 22 -858993436 -858993431
    5 -858993429 -858993433 -858993428 -858993431 22 -858993435
    6 -858993430 -858993428 -858993447 -858993436 -858993439 25
    Press any key to continue . . .

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This doesn't init the array properly:
    Code:
    	for(i=0;i<=6;i++)
    	{
    		dice[i][i]=0;
    	}
    As its a 2d array, you need two loops (nested). Or, you can init it by doing this:
    >>int dice[7][7] = {0,0};
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    thanks dude,
    solved my problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 01:37 PM
  2. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  4. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  5. Garbage values
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-03-2002, 05:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21