I am just having a round time wrapping my head around these arrays. The problem I am having is I can get the 2 dice to work correctly and print out (except now i'm getting a roll of 13 which i'm not sure why. Anyways I can not figure the percentage of the numbers per ROLLS. Any help would be nice to guide me in the right direction
Assignment:
Let’s use 10,000 this time instead of 1000. You don’t need to store the values that you roll this time, you can just count them as you roll them. That will allow us to roll an unlimited number of times (almost) without using up all the memory storing the individual rolls.
Each die can be from 1 - 6, so the total of 2 dice must be in the range 2 – 12.
Your output will be in the range of 2 – 12.
Notice that it is much less likely to roll a 2 than it is to roll a 7, since a 2 can only be rolled a 1 + 1. But a 7 can be 6 + 1, 5 + 2, 4 + 3, 3 + 4, 2 + 5, 1 + 6.
Count how many times each value occurs and output that count as in the previous part.
Calculate what percentage of the time you roll a 2, 3, 4, etc. up to 12. Output the percentages with an accuracy of one place past the decimal.
Here is what i have put together so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROLLS 10
main() {
int rollCount[12] = { 0 };
int diceRoll1, diceRoll2, total = 0;
double average[12] = { 0 };
int i;
srand(time(0));
for (i = 0; i < ROLLS; i++) {
diceRoll1 = rand() % 6 + 1;
diceRoll2 = rand() % 6 + 1;
total = diceRoll1 + diceRoll2;
rollCount[total - 1]++;
}
for (i = 0; i < 12; i++) {
average[i] = (double)ROLLS / rollCount[i];
}
for (i = 0; i < 12; i++) {
printf("%i occurs %i times\n", i + 2, rollCount[i]);
printf("%i is rolled %.1lf percent of the time\n\n", i + 2, average[i]);
}
system("pause");
}
|