More Arrays

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");
 }
Line 19: You're subtracting 1 here (from 2-12), so your subscript is 1-11 (0 is unused). This is okay, but inconsistent with how you use it later.

Line 22: You're calculating averages on 0-11. Not a problem, but inconsistent usage since [0] is unused.

Line 23. Your calculation is incorrect. Your numerator and denominator are reversed.

Line 26: Same comment as line 22.

Line 27: You're adding 2 to i. This will give you the wrong subscript since you only subtracted 1 at line 19. i runs from 0 - 11, i+2 will print 2-13 and your reference to rollCount won't match the correct entry.

Line 28: Same comment as line 27.

It might be easier to allocate your two arrays as containing 13 elements (ignoring 0 and 1), then you can use your points value directly as your index.

Last edited on
I made the changes that you recommended but the only thing bothering me is the average function. I had them switched cause the results became too low and did not seem right.

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[11] = { 0 };
	int diceRoll1, diceRoll2, total = 0;
	double average[11] = { 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 - 2]++;
	}

	for (i = 0; i < 11; i++) {
		average[i] = (double) rollCount[i] / ROLLS;
 	}

	for (i = 0; i < 11; 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");
 }


so that is the only thing not right at this point
Topic archived. No new replies allowed.