[Note: each die can show an integer value from 1 to 6, so the sum will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 the least frequent sum]
The 36 possible combination of the two dice is as follows:
Your program should roll the dice 36000 times. Use a one-dimensional array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable, i.e. there are six ways to roll a 7, so approximately one-sixth of all the rolls should be 7.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main() {
int sums[36000];
int possible[11];
for (int i=0;i<36000;i++){
int value1 = rand() % 6 + 1;
int value2 = rand() % 6 + 1;
sums[i] = value1 + value2;
}
for(int i=0;i<36000;i++) {
possible[sums[i]-2]++;
}
int total = 0;
for(int i=0;i<11;i++) {
total += possible[i];
printf("%d - %d\n", i + 2, possible[i]);
}
if (total == 6 * possible[5]) {
printf("Totals are resonable");
}
else {
printf("Totals are not resonable");
}
}
The 36 possible combination of the two dice is as follows:
Your program should roll the dice 36000 times. Use a one-dimensional array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable, i.e. there are six ways to roll a 7, so approximately one-sixth of all the rolls should be 7.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main() {
int sums[36000];
int possible[11];
for (int i=0;i<36000;i++){
int value1 = rand() % 6 + 1;
int value2 = rand() % 6 + 1;
sums[i] = value1 + value2;
}
for(int i=0;i<36000;i++) {
possible[sums[i]-2]++;
}
int total = 0;
for(int i=0;i<11;i++) {
total += possible[i];
printf("%d - %d\n", i + 2, possible[i]);
}
if (total == 6 * possible[5]) {
printf("Totals are resonable");
}
else {
printf("Totals are not resonable");
}
}
No comments:
Post a Comment