Stuck trying to loop multiple dice multiple times

For this assignment, "Your program will prompt the user to enter the number of dies in a set (4 through 6) that will be rolled together. The sum of the faces on the set will be the index to an array that holds the number of times this sum has occurred. You must also prompt your user for the number of times the set will be rolled. (2500, 3000, or 5000 times)."

I am currently stuck trying to loop the number of dice 2500 times. My output doesn't make sense as it outputs the sum of the number of rolls, which has a low value considering the number of times rolled. I can't figure out a way using for loops to roll the dice multiple times.

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
#include <iostream> 
#include <iomanip>
#include "die.h" 

using namespace std;

int main()
{
	die dieOne;
	die dieTwo;
	die dieThree;
	die dieFour;
	die dieFive;
	die dieSix;

	int getDieNumber;
	int dieRoll;
	int sum;
	int score;

	cout << "Enter the number of Dies to use (4, 5, or 6)\n";
	cout << "or press enter to default to 6" << endl;
	cin >> getDieNumber;

	cout << "Enter the number of times the " << getDieNumber << " " << endl;
	cout << "dice are to be rolled: (2500, 3000, or 5000)\n or press enter to default to 5000" << endl;
	cin >> dieRoll;

	if (dieRoll == 2500 && getDieNumber == 4) {
		for (int i = 0; i < dieRoll; i++) {
			dieOne.roll();
			dieTwo.roll();
			dieThree.roll();
			dieFour.roll();
			sum = dieOne.getNum() + dieTwo.getNum() + dieThree.getNum() + dieFour.getNum();
		}
		cout << sum;

	}
	else if (dieRoll == 3000 && getDieNumber == 5) {
		dieOne.roll();
		dieTwo.roll();
		dieThree.roll();
		dieFour.roll();
		dieFive.roll();
		sum = dieOne.getNum() + dieTwo.getNum() + dieThree.getNum() + dieFour.getNum() + dieFive.getNum();
		cout << sum;
	}
	else {
		cout << "You are rolling 5000 times" << endl;
	}

	system("pause");
	return 0;
}


Here is the code for the die.h class we are required to use.

https://pastebin.com/ZFdi1Yes
Calculating the sum is just the first step.
The sum of the faces on the set will be the index to an array that holds the number of times this sum has occurred.
Actually you need an array like this:

int sum_occurred[max_number_of_dies * max_number_of_one_die] = {0};

Which would be accidentally 6 for both. After line 35:

++(sum_occurred[sum]);

Hint: Instead of having six dies you could make a inner loop for calculating sum of the rolls. That way you wouldn't need different cases.
You have an array of die[6]. You need an outer for loop of 0 while less dieRoll. Within that loop you then have a loop iterating die from 0 while less getDieNumber. You sum the totals of the die and increment the sum_occurred.

Note that using cin >> you can't just press enter without entering a value.

Something like:

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
#include <ctime>
#include <cstdlib>

class die
{
public:
	die();
	void roll();
	int getNum() const;

private:
	int num; //The value of this instance ( 1 through 6variable num.
};

die::die()
{
	num = 1;
	srand(time(0));
}

void die::roll()
{
	num = rand() % 6 + 1;
}

int die::getNum() const
{
	return num;
}

#include <iostream>
using namespace std;

constexpr int MAXSUM = 6 * 6; // max dies (6) * max number each die (6)

int main()
{
	int dieNumber = 0, dieRoll = 0;

	while ((cout << "Enter the number of Dies to use (4, 5, or 6): ") && (!(cin >> dieNumber) || (dieNumber < 4 || dieNumber > 6))) {
		cout << "Invalid number entered\n";
		cin.clear();
		cin.ignore(1000, '\n');
	}


	while ((cout << "Enter the number of times the " << dieNumber << " dice are to be rolled: (2500, 3000, or 5000): ") && (!(cin >> dieRoll) || (dieRoll != 2500 && dieRoll != 3000 && dieRoll != 5000))) {
		cout << "Invalid number entered\n";
		cin.clear();
		cin.ignore(1000, '\n');
	}

	int dieSums[MAXSUM] {0};

	die dice[6] {};

	for (int r = 0; r < dieRoll; ++r) {
		int sum = 0;

		for (int d = 0; d < dieNumber; ++d) {
			dice[d].roll();
			sum += dice[d].getNum();
		}

		++dieSums[sum];
	}

	cout << endl;
	for (int s = 0; s < MAXSUM; ++s)
		if (dieSums[s] != 0)
			cout << "Sum " << s << " occurred " << dieSums[s] << " time" << (dieSums[s] > 1 ? "s\n" : "\n");
}


Also note that the assignment statement is incorrect - "that will be rolled together." You can't easily roll them together (well you can but the knowledge to do that is beyond your current knowledge). The die will be rolled sequentially.
Last edited on
Thanks for the explaination! For the last part of the assignment, there is something similar to what you've done for displaying the sum and number of times it has been rolled. The only difference is that it must be output into a bar graph.
"Once the rolls are completed the program will display a bar graph, like the one below, showing the sum value, the number of times it was rolled, and a bar of asterisks each one representing 2% of the highest count recorded in this run. In the example below, the highest value is 464, therefor, each asterisk is 2% of 464 which, rounded down, is 9. So, for the highest value of 464 there are 49 asterisks."

Given to us was the start of the bar graph which is,
1
2
3
4
5
6
7
cout << "For " << # of dice << " dice, rolled " << # of rolls << " times" << endl;
cout << "Count %:---   0    10   20   30   40   50   60   70   80   90   100" << endl;
cout << "Sum    count  |....|....|....|....|....|....|....|....|....|....|" << endl;

cout << " "   << setw(2) << sum of dice faces ;          // Formatting so Sum and count columns
cout << "  (" << setw(4) << # of times rolled << ")   "; // are neat and tidy.


I've tried to work this code into what I already have, but I cannot get the correct amount of asterisks to display for each sum. What am I doing wrong? We haven't had any experience with making such graphs, so I'm completely lost.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
	cout << "For " << dieNumber << " dice, rolled " << dieRoll << " times" << endl;
	cout << "Count %:---   0    10   20   30   40   50   60   70   80   90   100" << endl;
	cout << "Sum    count  |....|....|....|....|....|....|....|....|....|....|" << endl;
	for (int s = 0; s < MAXSUM; ++s) {
		double v = 0.02;
		double b;
		double n;
		b = v * dieSums[s];
		n = dieSums[s] / b;

		if (dieSums[s] != 0) {
			cout << "\n";
			cout << " " << setw(2) << s;          // Formatting so Sum and count columns
			cout << "  (" << setw(4) << dieSums[s] << ")   "; // are neat and tidy.
			cout << " " << setw(n) << setfill('*');


		}
	}


Here is the output the program must match
https://imgur.com/a/0acDgg4
The first thing you need is the highest value. You can do this in the roll loop:
1
2
3
4
5
6
7
8
int max_sum = 0;
for (int r = 0; r < dieRoll; ++r) {
...
		++dieSums[sum];

if(dieSums[sum] > max_sum)
  max_sum = dieSums[sum];
	}


I actually don't know how they come to the value 49. Iin the graph they have 51 asterisks.

However you need the ratio of dieSums[s] and max_sum. With this ratio you can calculate the number of asterisks.
So I'm still a bit confused. After
1
2
if(dieSums[sum] > max_sum)
  max_sum = dieSums[sum];

What exactly would I do next? Would I continue with
1
2
cout << " "   << setw(2) << sum of dice faces ;          // Formatting so Sum and count columns
cout << "  (" << setw(4) << # of times rolled << ")   "; // are neat and tidy. 

How would I create a function that outputs 2% of the highest value in asterisks?
If you were to do this using paper and pen to produce a graph, how would you do it? How would you tell some else who didn't know anything about graphs how to do it? Write down the steps that you would tell someone else how to do it using pen & paper. That is an algorithm. Once you have that, then translate that into a program design. Then convert the design into code. The last thing you do is to start to code without an algorithm/design.
Last edited on
How would I create a function that outputs 2% of the highest value in asterisks?
Don't get confused because of the 2%. You don't need it. The chart is 100% but you have only 50(49?) asterisks for the chart. Hence 1 asterisk represents 2%.

So your loop looks good (without test), you just need the correct calculation for n:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	cout << "For " << dieNumber << " dice, rolled " << dieRoll << " times" << endl;
	cout << "Count %:---   0    10   20   30   40   50   60   70   80   90   100" << endl;
	cout << "Sum    count  |....|....|....|....|....|....|....|....|....|....|" << endl;
	for (int s = 0; s < MAXSUM; ++s) {
...
		n = (dieSums[s] / (double) max_sum) * 50; // Note: 50 or 49 asterisks. Rounding?

		if (n != 0) {
...
			cout << " " << setw(n) << setfill('*') << '\n'; // Note: '\n'


		}
	}
Topic archived. No new replies allowed.