Friday, March 16, 2012

Dice Rolling

Program below simulate the rolling of two dice.The dices is rolled 36000 times and the sum of two dice is calculated for each rolled.The numbers of times each possible sum appears is tallied in an array.

Dice rolling outcomes
/*
   title: DiceRolling
   author: aeriqusyairi
   date: Jan26 2012
*/
#include<stdio.h>
#include<stdlib.h>
#define ROLL 36000

int rollDice( void );
void printFreq( const int b[], int size );

int main(){
   int freq[ 13 ] = { 0 };
   int i, x;
   
   for(i = 0; i < ROLL; i++){
      x = rollDice();
      ++freq[ x ];
   }
    
   printFreq( freq, 13 );
      
   system("pause");
   return 0;
}

int rollDice( void ){
   
   return ( 1 + (rand() % 6) ) + ( 1 + (rand() % 6) );
       
}

void printFreq( const int b[], int size ){
   int i;
   
   for(i = 2; i < size; i++){
      printf("%d occured %d times.\n", i, b[ i ] );
   }
}

No comments:

Post a Comment