C++ Dice Rolling Using Arrays and Functions

Hi there, I'm trying to make a solution for this problem. Can someone please help me in writing this program. Following is the exact question that I found:
Write a program to investigate the probabilities of obtaining different results when rolling a pair of six-sided dice by simulating it using the C++ random number generator. This time, you will use an array to collect the statistical information. You will also use functions as follows:

* Write an int-valued function called getRepeti tions with no parameters. The function should ask the user for the number of dice rolls to simulate in the program, and ensure it is positive. The function should return this value.

* Write an int-valued function called rollDie with one intparameter. The function should use the rand () function to pick a random number between J and the given parameter. It should then return this number to the calling function. This will simulate the rolling of an n-sided die, where n is the value of the parameter.

* Write a void function called doRolling with three parameters: an array of ints to keep track of the number of times each number is rolled (where the elements hold the number of2's rolled, the number of3's rolled, and so on); an

* int to indicate the number of rolls required; an int to indicate the number of sides for the dice. The function should simulate the required number of rolls, and keep track of the number oftimes each value is rolled using the array.

* Write a void function called showsummary to display the results of the simulation in a neat table. This should include the number of times each value (2 through 12) was rolled, what percentage of the time each value was rolled, the theoretical percentage for each roll, and the theoretical number of times each value should have been rolled for the current test run. This function should have the same three parameters as doRo11ing had.

The main program should seed the random number generator and then use the functions to perform the task. The main program should declare the required variables as well: the number of repetitions, the array containing the counts, and any other necessary variables. Provide the output from at least three test runs.


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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//This is all I can understand as of now.

#include<iostream>
#include<ctime>

using namespace std;

int getRepetitions();
int rollDie(int);
void doRolling(int[], int, int);
void showSummary(int[], int, int);

double i; // Variables Declared


int main()
{
	int uInput;
	uInput = getRepetitions();
	rollDie(uInput);
	doRolling();
	showSummary();
	
	system("pause");
	return 0;
} // End Main

int getRepetitions()
{ 
	int userInput = 1;
	while(userInput > 0)
	{
		cout << "How many times do you want to roll the dice? "<< endl;
		cin >> userInput;
	}
	return userInput;

}

int rollDie(int userInput)
{
	int counts[Max_number_of_rolls]={0};
	srand(time(NULL));
	int dice;
	dice= 1+ rand()%6;
	return dice;
}

void doRolling(int rollTimes[], int rollsRequired, int NumberOfSides)
{
	cout << "----------------Rolls of the first and second dice--------------" << endl;
	for(int i=0; i<rollDie; i++)
	{
		srand(time(NULL));
		int dice;
		dice= 1+ rand()%6;
		counts[i]=dice;
		cout << "Roll # " << i+1 << " first dice is: " << dice << endl;
		dice= 1+ rand()%6;
		counts[i]=counts[i]+dice; // First and Second Dice Adds Together
		cout << "Roll # " << i+1 << " second dice is: " << dice << endl;
		cout << "The sum is: " << counts[i] << endl;
	}
	cout<< "-------------Results/Statistics----------------" << endl;
	// To Calculate the Percentage
	for(i=2;i<13;i++)
	{
		int c =getRepetitions(counts,rollDie,i);
		cout << "The total value of " << i <<" is repeated " << c << " out of " << rollDie << " times." << endl;
		percentage= ((double)c/(double)rollDie)*100;
		cout << "Percentage of the time the total value was rolled is: " << percentage << "%" << endl;
	}
	cout << endl << endl;
}

void showSummary(int[], int, int)
{
	cout<< "-------------Results/Statistics----------------" << endl;
	// To Calculate the Percentage
	for(i=2;i<13;i++)
	{
		int c =getRepetitions(counts,rollDie,i);
		cout << "The total value of " << i <<" is repeated " << c << " out of " << rollDie << " times." << endl;
		percentage= ((double)c/(double)rollDie)*100;
		cout << "Percentage of the time the total value was rolled is: " << percentage << "%" << endl;
	}
	cout << endl << endl;
}
© 2015 Microsoft Terms Privacy & cookies Developers English (United States)
    
Last edited on

Please do not get upset but this is not a homework site. Try something and somebody will help you...
Last edited on
I have updated the code, this is where I got so far. :( Please help me.
Thanks.
I think you only use srand(time(0)); at the start of your program not with every call. This means each run of the program itself will give a different set of random numbers.

As a novice c++ programmer myself I find it too exhausting to debug other people's code.
My suggestion is you start in easy steps testing and debugging it as you go. If a bug appears it will involve the last bit of code you wrote.

For example this is how I might have started solving the assignment.
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<ctime>		// for the time() function
#include<cstdlib>	// for the srand() and rand() functions
using namespace std;

int getRepetitions()
{
    int nReps = 0;
    cout << "ENTER NUMBER OF REPETITIONS REQUIRED:";
    cin  >> nReps;
    while (nReps <=0)
    {
        cout << "MUST BE A POSITIVE NUMBER:" << endl;
        cout << "ENTER NUMBER OF REPETITIONS REQUIRED:";
        cin  >> nReps;
    }
    return nReps;
}

int rollDie()
{
    return (rand() % 6 ) + 1;
}

void doRolling(int v[],int n)
{
    int die1 = 0;
    int die2 = 0;
    cout << endl << "rolls of the dice" << endl;
    for (int i=0; i<n; i++)
    {
        die1 = rollDie();
        die2 = rollDie();
        cout << die1+die2 << " ";
        v[die1+die2]++;
    }
    cout << endl << "===========" << endl << endl;
}

int main()
{
    // declare variables
    int nRep = 0;
    int c[13];  //  sum each count

    // intialize c array
    for (int i=1; i<13; i++)
    {
        c[i] = 0;  //
    }

    srand(time(0));
    //nRep = getRepetitions();
    doRolling(c,13);  // roll 12 times


    // display the results
    cout << endl << "Frequency of each total 2 to 12" << endl;
    for (int i=2; i<13; i++)
    {
        cout << i << " " << c[i] << endl;
    }


    return 0;
}
Last edited on
Topic archived. No new replies allowed.