UMBC CS 201, Fall 06
UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

dice

The Task

Print out the probabilities of rolling a particular value between 2 and 12 with two six-sided dice.

The Method

So, for each of the 36 possible combinations of sides:

The Program

/**************************************** * File: dice.c * Author: R. Chang. * Date: 1994 * Section: 101 * EMail: chang@cs.umbc.edu * * Print out the probabilities * of rolling two six-sided dice. *****************************************/ #include <stdio.h> int main ( ) { int freq[13]; int die1, die2, i, j; /* Initialize frequency array to zero */ for (i = 2; i <= 12; i++) { freq[i] = 0; } /* Calculate the frequencies */ for (die1 = 1; die1 <= 6; die1++) { for (die2 = 1; die2 <= 6; die2++) { freq[die1 + die2]++; } } /* Print out results */ printf(" sum freq prob \n\n"); for (i = 2; i <= 12; i++) { printf("%5d: %2d %4.3f \n", i, freq[i], freq[i]/36.0); } printf("\n"); /*Print out bar graph */ for (i = 2; i <= 12; i++) { printf("%2d: ", i); for (j = 1; j <= freq[i]; j++) { printf("*"); } printf("\n"); } printf("\n"); /* Print out histogram */ for (j = 8; j >= 1; j--) { for (i = 2; i <= 12; i++) { if (freq[i] >= j) { printf(" * "); } else { printf(" "); } } printf("\n"); } /* label the histogram */ for (i = 2; i <= 12; i++) { printf("%2d ", i); } printf("\n"); return 0; }

The Sample Run

sum freq prob 2: 1 0.028 3: 2 0.056 4: 3 0.083 5: 4 0.111 6: 5 0.139 7: 6 0.167 8: 5 0.139 9: 4 0.111 10: 3 0.083 11: 2 0.056 12: 1 0.028 2: * 3: ** 4: *** 5: **** 6: ***** 7: ****** 8: ***** 9: **** 10: *** 11: ** 12: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2 3 4 5 6 7 8 9 10 11 12
Last Modified - Tuesday, 22-Aug-2006 07:13:55 EDT


CSEE | 201 | 201 F'06 | lectures | news | help