/* File: dice.c Print out the probabilities of rolling two six-sided dice. */ #include #include "genlib.h" #include "simpio.h" main() { int prob[13] ; int die1, die2, sum, i, j ; /* Initialize array to zero */ for (i=2; i<=12 ; i++) { prob[i] = 0 ; } /* Calculate the probabilities */ for (die1 = 1; die1 <= 6; die1++) { for (die2 = 1; die2 <= 6; die2++) { sum = die1 + die2 ; prob[sum] ++ ; } } /* Print out results */ for (i = 2 ; i<=12 ; i++ ) { printf("%d: %d\n", i, prob[i] ) ; } printf("\n") ; /*Print out bar graph */ for (i = 2 ; i <= 12 ; i++) { printf("%2d: ", i) ; for (j = 1 ; j <= prob[i] ; j++) { printf("*") ; } printf("\n") ; } printf("\n") ; /* Print out histogram */ for ( j = 8 ; j >= 1 ; j-- ) { for ( i = 2 ; i <= 12 ; i++) { if ( prob[i] >= j ) { printf(" * ") ; } else { printf(" ") ; } /* else */ } /* for (i = ... */ printf("\n") ; } /* for (j = ... */ /* label the histogram */ for ( i = 2 ; i <= 12 ; i++) { printf("%2d ", i) ; } printf("\n") ; }