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)?