Lab Assignment Help

I currently need help with a C++ lab assignment and it would be greatly appreciated.

The assignment states:

You are provided a function (RollDice) that will give the result of rolling 1 dice. You are to develop a function that will roll n dice many times and gather information about the results. You are to keep a count of how many times each possible value of summing the n dice (n to 6*n) occurs. You should use an array to keep track of the counts. So, if rolling 1 dice, you will keep in the array how many times each value 1 through 6 occurred. If 2 dice were rolled, you will keep in the array how many times each value 2 through 12 occurred. It is suggested to use the value on the dice as the index to the array where the count is kept. Note that this means there will be wasted entries in the array as not all values are
possible, for instance you can never roll a 0.

Requirements:

1. Create a function to run the experiment that accepts as parameters: the array where the results will be placed, the number of dice, and the number of times to run the experiment. This allows us to use the computer to run a large number of experiments rather than manually rolling the dice. The function should 1st initialize all entries in the results array to 0. It should then run the experiment n times, each time calling the RollDice time for each dice in a roll, so if there are 2 dice, it should be called 2 times. Those values are summed to get the value of the roll. The value should then be used to update the count in the array of how many times that value has
been rolled.

2. Create a function that prints the results to a file. The function should take as parameters the array holding the results, the number of dice, and a string containing the name of the output file. It should write the results out in 2 columns separated by a comma. The 1st column should have the roll value (only print valid values for the number of dice used). The second column should have the number of times that value was observed.

3. Main should set up and run an experiment. You need to declare a constant defining the number of dice to be rolled each time. Using this constant, declare an array to hold the results of the experiment, making sure you have an appropriate number of entries in the array. Then you should call your function to run the experiment.

Also, I am to test the lab with 1, 2, 3, 4, and 10 dice using a large number of rolls for each experiment (like 1,000,000 for example). Then, plot the resulting data in Excel as a bar graph.


I have initialized the array to zero and started the experiment function, however, I am stuck and do not know where to go from there.

This 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
#include <iostream>
#include <random>
#include "RollDice.h"

using namespace std;

int RollDice()
{
	static std::default_random_engine generator;
	static std::uniform_int_distribution<int> distribution(1, 6);

	return distribution(generator);
}

void Zero(int results[], int utmost) //Initializing the array 
{
	for (int i = 0; i <= utmost; i++)
	{
		results[i] = 0;
	}
}

void Exp(int results[], int NoD, int reps) //Experiment  


Lastly, for anyone who contributes, I thank you and appreciate it greatly!

Last edited on
> for (int i = 0; i <= utmost; i++)
Well the usual convention for indexing arrays would use < rather than <=
That 1 extra step risks trashing something.

As for the rest, consider
1
2
3
4
5
6
for ( int r = 0 ; r < reps ; r++ ) {
    for ( d = 0 ; d < NoD ; d++ ) {
        // sum some dice
    }
    // can you figure out what goes here?
}




http://www.catb.org/~esr/faqs/smart-questions.html#urgent
@salem c

Would it be this?

1
2
3
4
5
6
7
8
9
10
11
void Exp(int results[], int NoD, int reps) //Experiment 
{
	for (int r = 0; r < reps; r++)
	{
		for (int d = 0; d <= NoD; d++)
		{
			sum = sum + RollDice();
		}
		results[sum]++;
	}
}
I'm curious about your fascination with using <= in your loops.

And what is sum, and what is it initialised to?

It's heading in the right direction though.
Sorry salem c, my apologies. Here is my corrected code:

1
2
3
4
5
6
7
8
9
10
11
12
13
void Exp(int results[], int NoD, int reps) //Experiment 
{
	for (int r = 0; r < reps; r++)
	{
		int sum = 0;
		for (int d = 0; d < NoD; d++)
		{
			sum = sum + RollDice();
			results[sum]++;
		}
		
	}
}
Are you really counting the sum of n dice?
The assignment in the original post states: You are to keep a count of how many times each possible value of summing the n dice (n to 6*n) occurs.

So, I'm guessing you are supposed to count the sum of n dice, and also keep count. I may be wrong.
Topic archived. No new replies allowed.