Array with dice

So I've been having issues with array's and this last problem really has me. Basicly I need to provide a define constant for how many dice to use and with that being the only thing to change it should make the rest of the program work.

assignment:
Repeat part 5 using any number of dice. (well not an infinite number, let’s say, up to 10 dice)

Hints:

Arrays cannot be declared using a variable size (yet  ) so just make them as big as you will ever need and don’t use the parts that you don’t need. That’ snot supposed to be the interesting part of the problem.

Define a constant DICE that holds how many you will throw in this simulation.

The values rolled will range between DICE (all 1’s) => DICE * 6 (all 6’s).

Suppose we choose 5 dice. ( it must work for any number )

#define DICE 5 // the rest of the code should work if you change this to any number

The lowest number we can roll is 5, and the largest is 30

That is 26 different values. 5 => 30,
or (5 * 6) – (5 – 1)

DICE * 6 - (DICE – 1)

In general, the range is DICE => DICE * 6 (all 6’s).

This program is really the same as in part 5, but all of the loops will need to start and end at calculated places. In the previous part you could hard code the loops to run from 2 – 12, for example. When the number of dice will vary, you will need to calculate where the loops start and stop. Rolling the dice will really be rolling one die in a loop DICE times.

Demonstrate that your program works for one die, 5 dice, and 10 dice. Post these probabilities on the discussion board.

The probabilities will be very small. For example if you throw 10 dice, the chance you will get all 6’s is very small. Use all of the places past the decimal that you have, and increase the number of throws, to get accurate answers.

He does give use hints and I though I was starting to understand but got lost in the dice rolling counter.

Here is what I have 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
33
34
35
36
37
38
39
40
41
42
  #include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROLLS 10000 /*Defined Constant*/
#define DICE 5

main()  {

	/*Variables*/
	int rollCount[(DICE * 6) - (DICE - 1)] = { 0 };
	int diceRoll1, diceRoll2, diceRoll3, diceRoll4, diceRoll5, total = 0;
	double average[(DICE * 6) - (DICE - 1)] = { 0 };
	int i;

	/*Random number function*/
	srand(time(0));

	/*Dice rolling counter*/
	for (i = 0; i < ROLLS; i++) {
		for (i = 0; i <= DICE; i++){
			diceRoll1 = rand() % (6 + 1);
			rollCount[total - 2]++;
		} 
		rollCount[total - 2]++;
		total = diceRoll1;
	}


	/*Average per number counter*/
	for (i = 0; i < 11; i++) {
		average[i] = (double)(rollCount[i] * 100) / ROLLS;
	}

	/*Output to show how many times each number showed up, and the average*/
	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]);
	}

	/*To stop system before closing out*/
	system("pause");
}
See comments in code:
1
2
3
4
5
6
7
8
for (i = 0; i < ROLLS; i++) {
  for (i = 0; i <= DICE; i++){
    diceRoll1 = rand() % (6 + 1); // Zwilu commented about this in your last thread - lose the parentheses
    rollCount[total - 2]++; // You should be summing here not incrementing the array
  }  
  rollCount[total - 2]++;	// "- 2" is for two die 
  total = diceRoll1;	// You've incremented your array, so you need to zero total for the next roll 
}
Topic archived. No new replies allowed.