Thread: i need real badly

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

    i need real badly

    i have written the code, but for some reason it wont work. ill attach the assignment and the code will be in the mssg....please if someone can help me out ill be really greatful.
    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);
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    okay. There were a lot of small errors that all came together to break your program... I'm running this in VC 6 and it works well. First of all, when you call functions that you pass an array to, you don't pass an index. use win instead of win[21] . In the assignment, it said to generate a random number from 1 to 7. Although this is different from the real games of craps, I changed your rollDice function to generate 2 numbers from 1 to 7 instead of 1 to 6. One of your bigger problems was in the analyzeWinLoses procedure. First of all, the first if statement (the one that determines if it goes into the "above 20 rolls" slot of the array, if(k>21), isn't right. The way you have it, all games after the 20th will go into the over 20 rolls slot, even if it had under 20 rolls. It should have been if(abs(game[k])) . This is because the absolute value of game[k] was the number of rolls in the game. Also, I fixed the analyzeWinLoses last part, that had you putting values into a negative array index if the game was losing. Your averaging procedure didn't quite work, so I changed it to something that does. Also, you had a few places where you converted floats to ints and lost some data. Anyway, here is the fixed code.
    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 avgLengthOfGames(int []);
    void printResults(int [], int [], float, float, 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!=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);
    
    }
    
    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,7)+randomInt(1,7);
    	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(abs(game[k])>21)
    		{
    			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])
    {
    	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 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], float twins, float 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);
    }

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Lots of good help. I wouldn't do the homework however. Most of the learning is in the doing.

    kashifk, make sure you understand the what's and the why's of da'hippo's changes. Give another post if you need anything cleared up.

    gg

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    thanks alot for your help....i had done most of the code and updated it too...its just that last thing that was not working.....thanks alot for your help.
    thanks alot

  5. #5
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Code:
    printf("  Num-Rolls           Games-Won             Games-Lost\n\n");
    printf("     1                     %d                     %d   \n",win[0],lose[0]);
    I know this isn't wrong at all but that huge list of code bothers me. You should be able to do a small loop and get the same output. Instead of that huge list try this:

    Code:
    printf("  Num-Rolls           Games-Won             Games-Lost\n\n");
    for(randint=1;randint<20;randint++)
    {
    printf("     %d                     %d                     %d   \n",randint,win[randint-1],lose[randint-1]);
    }
    printf("Over20                     %d                     %d   \n",win[20],lose[20]);
    Sorry for being so picky

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    The main() function returns always an integer, so, you should change your main() to int main() and then, return an integer value at the end of the program with return.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Factorial
    By foxman in forum Contests Board
    Replies: 27
    Last Post: 07-11-2008, 06:59 PM
  2. %16 with double
    By spank in forum C Programming
    Replies: 11
    Last Post: 03-05-2006, 10:10 PM
  3. Why am I getting 'undelcared identifier' ???
    By Bill83 in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2006, 01:00 PM
  4. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM
  5. Replies: 7
    Last Post: 12-12-2001, 10:28 AM