Print out the probabilities of rolling a particular value between 2
and 12 with two six-sided dice.
The Method
Each combination of sides of the dice is equally likely,
but yields a different sum between 2 and 12.
So, for each of the 36 possible combinations of sides:
compute the sum, and
note that we found another way to get that sum.
The Program
/* File: dice.c
Print out the probabilities
of rolling two six-sided dice.
*/
#include
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");
}