Another newbie asking for help!

This is a discussion on Another newbie asking for help! within the C Programming forums, part of the General Programming Boards category; Good morning everyone. I'm very very glad to have found this site. I'm currently enrolled for an associates degree in ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Another newbie asking for help!

    Good morning everyone. I'm very very glad to have found this site.

    I'm currently enrolled for an associates degree in programming (I plan on taking c, c++ and java as my main languages) and jumping into the field afterward!

    Anyway, I'm currently working on my final project for my Intro to C class as well as this last homework assignment.


    This homework assignment is as follows:
    Write a program that runs 1000 games of craps without human intervention and:
    a) keeps track of how many games are won on the first roll, second roll.. twentieth roll and then sums the number of games won on rolls over 20
    b) (same for games lost)


    The code I'm about to post isn't the complete program. The problem I'm having is that my counter (for playing the 1000 games) seems to be resetting for some reason. I've REM'd out the output lines for now just so I can see a clearer picture of what's happening.

    I've looked over the code and I can't seem to figure it out! Anyone give me a nudge?

    Thanks!

    Code:
    #include < stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    enum Status { CONTINUE, WON, LOST };
    
    int rollDice( void );
    
    int main( void )
    {
    	int sum;
    	int myPoint;
    	int plays; /* counter for 1000 games */
    	int wins[] = {0}; /* each address will be the roll, the value of the address will be the number of games won on that roll */
    	int losses[] = {0}; /* each address will be the roll, the value of the address will be the number of games lost on that roll */
    	int roll; /* counter to keep track of the number of rolls per game */
    
    	enum Status gameStatus;
    
    	srand( time( NULL ) );
    
    	for (plays = 1; plays < 1001; plays++) { 
    printf( "%d\n", plays);
    	   roll = 1;
    		sum = rollDice();
    
    		switch( sum ) {
    			case 7:
    			case 11:
    				gameStatus = WON;
    				wins[ 0 ] =+1;
    				break;
    			case 2:
    			case 3:
    			case 12:
    				gameStatus = LOST;
    				losses[ 0 ] =+1;
    				break;
    			default:
    				gameStatus = CONTINUE;
    				myPoint = sum;
    				/* rem printf( "Point is %d\n", myPoint ); */
    				break;
    		} /* end switch */
    
    		while ( gameStatus == CONTINUE ) {
    			++roll;
    			sum = rollDice();
    
    			if ( sum == myPoint ) {
    				gameStatus = WON;
    				wins[ roll ] =+1;
    			} /* end if */
    			else {
    				if ( sum == 7 || sum == 11) {
    					gameStatus = LOST;
    					losses[ roll ] =+1;
    				} /* end if */
    			} /* end else */
    		} /* end while */
    
    		if ( gameStatus == WON) {
    			/* rem printf( "Player wins\n" ); */
    		} /* end if */
    		else {
    			/* rem printf( "Player loses\n" ); */
    		} /* end else */
    	} /* end for */
    	return 0;
    } /* end function main */
    
    int rollDice( void )
    {
    	int die1;
    	int die2;
    	int workSum;
    
    	die1 = 1 + ( rand() % 6 );
    	die2 = 1 + ( rand() % 6 );
    	workSum = die1 + die2;
    
    /* rem	printf( "Player rolled %d + %d = %d\n", die1, die2, workSum ); */
    	return workSum;
    }
    Thank you for any assistance anyone can provide! I've searched the forum with the keyword "craps" but don't really see a well-related question. I suppose you're all sick of these craps games, eh?
    Last edited by ericdabear; 03-15-2010 at 08:05 AM. Reason: to note that I have done a search on the forums

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    Welcome to the forum!

    I can't currently test your code to see exactly what is happening, but from a quick overview of the code, I can't really find anything that would reset the 'plays' counter. I'm assuming that is what you were talking about? If so, what makes you think that the counter is being reset?

    One thing I do notice in your code is that you're declaring 'wins' and 'losses' as single element arrays, but you are going beyond those bounds... It looks as though you were trying to use them as a dynamic arrays, but you're not actually allocating any memory.

    Code:
            int wins[] = {0}; // 1D array with 1 element...
    	int losses[] = {0}; // 1D array with 1 element...
    ...further down in the code.....

    Code:
    			++roll;  // Assume this is after a few loops... this is DEFINITELY greater than 0!
    			sum = rollDice();
    
    			if ( sum == myPoint ) {
    				gameStatus = WON;
    				wins[ roll ] =+1;  // <<<<<< Where is this pointing to? (Random memory)
    			} /* end if */
    Maybe this is where your issue is?

    HTH

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't see your plays being reset, but your wins and losses are, since you wrote "=+1" (meaning, equal to positive one) instead of "+=1" (meaning, increment by 1) in several places.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Feengur View Post
    Welcome to the forum!

    I can't currently test your code to see exactly what is happening, but from a quick overview of the code, I can't really find anything that would reset the 'plays' counter. I'm assuming that is what you were talking about? If so, what makes you think that the counter is being reset?

    One thing I do notice in your code is that you're declaring 'wins' and 'losses' as single element arrays, but you are going beyond those bounds... It looks as though you were trying to use them as a dynamic arrays, but you're not actually allocating any memory.

    Code:
            int wins[] = {0}; // 1D array with 1 element...
    	int losses[] = {0}; // 1D array with 1 element...
    ...further down in the code.....

    Code:
    			++roll;  // Assume this is after a few loops... this is DEFINITELY greater than 0!
    			sum = rollDice();
    
    			if ( sum == myPoint ) {
    				gameStatus = WON;
    				wins[ roll ] =+1;  // <<<<<< Where is this pointing to? (Random memory)
    			} /* end if */
    Maybe this is where your issue is?

    HTH
    Good spot. So put the two together -- change the =+ to +=, put a 21 or a 22 inside those brackets, and off you go.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    when you say it keeps resetting what do you mean? the for loop counter should not reset after it reaches its total, it should just drop out of the loop and thats the end of it.

    for single lines comment your lines // (if that compiler allows.....)

    well i compiled and ran it in debug, you can see the play variable doing some wild stuff,
    i commented out the call to rollDice and the problem was fixed so check that out,

    edit > i was too slow repying anyhow!

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Feengur:

    My goal on the arrays was to use the position in the array as the number of rolls and the value of each position as the number of times a game was won or lost on that roll (such that if win[0] = 4 it would tell me that there were 4 wins out of 1000 on roll 1 (being element 0 of the array)

    tabstop thank you thank you thank you, I have problems with those assignment operators (is that the right word?) This fixed it!


    To all - the reason I know the plays counter was resetting was because in an effort to debug it, I added the printf statement directly below the for to see what the plays counter was set to in my output and it was looping all funky. fixing the =+ to a += fixed this.


    I fear that I'm treading on "don't ask people to do your work for you" by further asking a question, but... now I have the code as follows:

    Code:
    #include < stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    enum Status { CONTINUE, WON, LOST };
    
    int rollDice( void );
    
    int main( void )
    {
    	int sum;
    	int myPoint;
    	int plays; /* counter for 1000 games */
    	int wins[] = {0}; /* each address will be the roll, the value of the address will be the number of games won on that roll */
    	int losses[] = {0}; /* each address will be the roll, the value of the address will be the number of games lost on that roll */
    	int roll; /* counter to keep track of the number of rolls per game */
    	int rollcount;
    	int sumrollswins = 0;
    	int sumrollslosses = 0;
    
    	enum Status gameStatus;
    
    	srand( time( NULL ) );
    
    	for (plays = 1; plays < 1001; plays++) { 
    	   roll = 1;
    		sum = rollDice();
    
    		switch( sum ) {
    			case 7:
    			case 11:
    				gameStatus = WON;
    				wins[ 0 ] =+1;
    				break;
    			case 2:
    			case 3:
    			case 12:
    				gameStatus = LOST;
    				losses[ 0 ] =+1;
    				break;
    			default:
    				gameStatus = CONTINUE;
    				myPoint = sum;
    				/* rem printf( "Point is %d\n", myPoint ); */
    				break;
    		} /* end switch */
    
    		while ( gameStatus == CONTINUE ) {
    			++roll;
    			sum = rollDice();
    
    			if ( sum == myPoint ) {
    				gameStatus = WON;
    				wins[ roll ] +=1;
    			} /* end if */
    			else {
    				if ( sum == 7 || sum == 11) {
    					gameStatus = LOST;
    					losses[ roll ] +=1;
    				} /* end if */
    			} /* end else */
    		} /* end while */
    
    		if ( gameStatus == WON) {
    			/* rem printf( "Player wins\n" ); */
    		} /* end if */
    		else {
    			/* rem printf( "Player loses\n" ); */
    		} /* end else */
    	} /* end for */
    
    	printf( "# of rolls\tWins\tLosses\n" );
    	for ( rollcount = 0; rollcount < 21; rollcount++) {
    		printf( "   %d   \t %d \t  %d\n", rollcount, wins[ rollcount ], losses[ rollcount ] );
    	}
    	printf( "  >20	\t %d \t %d\n", sumrollswins, sumrollslosses);
    
    	return 0;
    } /* end function main */
    
    int rollDice( void )
    {
    	int die1;
    	int die2;
    	int workSum;
    
    	die1 = 1 + ( rand() % 6 );
    	die2 = 1 + ( rand() % 6 );
    	workSum = die1 + die2;
    
    /* rem	printf( "Player rolled %d + %d = %d\n", die1, die2, workSum ); */
    	return workSum;
    }
    and I get a "Run-time Check Failure #2 - stack around the variable 'wins' was corrupted" error.


    Does this come back to me trying to manipulate my arrays the way i want to (and being incorrect in that)?

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    yep. you still haven't setup your wins/losses arrays correctly. it's just not gonna work like that. :P

    either use malloc if you really want to use dynamic arrays OR for just setup the arrays so they are large enough to fit the max number of rolls.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    We realize that's what you want to do with the array. The point is, you don't have any array elements in your array to change. When you declare an array, you have to say how big it's supposed to be.

    Good declarations:
    Code:
    int wins[21] = {0};
    int losses[42] = {0};

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    brilliant!

    Thank you all for the help! It's working just fine now.


    (I haven't learned anything called 'malloc' yet so I chose to declare them as
    Code:
    array[1000] = {0}
    and let the compiler zero-fill as necessary. It seems rather inefficient but within the constraints of what I've been taught in class it's probably the best I can do.

    Thanks again!

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    It seems rather inefficient but within the constraints of what I've been taught in class it's probably the best I can do.
    quite right, what would your tutor think if he saw you fluent with malloc all of a sudden....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  4. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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