I'm having a problem with my HW "Dice Roll"

I'm supposed to write a code using an array in order to tally up how many times each face of the die shows up using the variables (they're from a pseudocode). I'm confused with the part where I need to "populate array_size..." and how to tally them. I found another source actually on this forum: http://www.cplusplus.com/forum/beginner/113742/.
But when I showed the "populate" part to my prof. He said he wanted two different loops in order to obtain the info. Regardless the debug contained all 0's for the reference link above when attempted. Sorry for the long post.



// Simulates the rolling of a die 6000 times and displays in a
// table format the number of occurrences of each side of the die

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
#include<iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
         
int main()
{
                        // declare variables
        const int array_size = 6000;  
        int die;                
        int num_array[array_size];      // array (num_array)of length array_siz
        int tally_array[7];             // array (tally_array) to hold thetots.
                        // set the seed first
        srand(time(0));
                        // Populate array_size random numbers into array     //num_array

       


                // Test part of the array for correct data
                // Leave this debug statement in final app

cout << "debug for num_array[]" << endl;
        for (int i=100; i<200; i++) {
                cout << num_array[i] << " ";
        }
        cout << endl;

/*	Tally the result - this is tricky - understand it fully:
			Declare an array, tally_array with 7 elements - only indexes 
			1-6 (not 0) will be used to hold the total of each 
			die face values - index 1 will hold the sum of all rolls of 1's, 
			index 2 wil hold the sum of all rolls of 2's, etc..
			In order to achieve this, create a loop, cycling through each of the
			6,000 random values, where each value (1 through 6) becomes the 
			index of tally_array and that index element will get 
			incremented by one with the ++ operator.
			
	*/



        // Test tally_array for correct data
    

cout << "debug for tally_array" << endl;
    for (int i=1; i<=6; i++) {
        cout << "i: " << i << " " << tally_array[i] << endl;
    }
    cout << endl;

		// display the results
		// duplicate this format:
			Display Result for 6000 Rolls
			Die Face    Occurance
			===========================
			1                1017
			2                1033
			3                 949
			4                1026
			5                 987
			6                 988		






	return 0;
}
Last edited on
So where do you roll the die and populate num_array?
Where do you iterate num_array and increment tally_array.

Free hint. You need to initialize tally_array to zeroes.

Here's what I've come up with:
[code]
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<iomanip>
using namespace std;

int main()
{

const int array_size = 6000;
int die = 0;
int num_array[array_size];
int tally_array[7]={0};

srand((int)time(0));
for(int i=0;i<array_size;i++)
num_array[rand()%6]++;
//This is where I'm stuck and what I have so far. Is this what you mean iterate num_array?

.
.
.
.


}

return 0;
}
.\[code]
Last edited on


Why would you have an array set to 6000? Holy gamblers anonymous batman....


I wonder if it would be so simple that.... nah... couldn't be...


for (int toss=0;toss <6000;toss++) loop #1

die=int rnd 6

for (int i=1;i<7;i++) (loop #2)

if (die==i){ tally[i]++}

}

}
Last edited on
so I tried that, and what its being printed is all 0's.
I used:
for (int toss=0;toss <array_size;toss++)
{
die = (rand()%6);
for (int i=1;i<7;i++)
if (die==i)
tally_array[i]++;

}

and my result was:
debug for num_array[]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 was rolled 0 times.
2 was rolled 0 times.
3 was rolled 0 times.
4 was rolled 0 times.
5 was rolled 0 times.
6 was rolled 0 times.

When the 100 0's should be random numbers

#include<iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

int main()
{

const int array_size = 6000;
int die;
int num_array[array_size];
int tally_array[7]={0};


srand(time(0));

for (int toss=0;toss <array_size;toss++)
{
die = (rand()%6);

for (int i=1;i<7;i++)

if (die==i)

tally_array[i]++;

}

// Test part of the array for correct data? This part I don't know how to do either.
// Leave this debug statement in final app

cout << "debug for num_array[]" << endl;
for (int i=100; i<200; i++) {
cout << num_array[i] << " ";
}
cout << endl;

for(int i=0;i<6;i++)
cout<<i+1<<" was rolled "<<num_array[i]<<" times."<<endl;
return 0;
}


This is the result:

debug for num_array[]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 was rolled 0 times.
2 was rolled 0 times.
3 was rolled 0 times.
4 was rolled 0 times.
5 was rolled 0 times.
6 was rolled 0 times.
Last edited on
1
2
3
4
5
6
  int roll;
  for (int i=0;i<array_size;i++) 
  {  roll = rand()%6+1;    // roll the dice (1-6)
      num_array[i] = roll;   // Save the roll in the array
      tally_array[roll]++;    // Increment the tally for the roll
  }


You still need to work on your code tags. You too pearlyman.
My teacher keeps emphasizing that -__-. I'll try that, thank you
So this gives me: 5 4 6 3 6 3 4 6 2 1 2 3 2 6 6 2 5 2 5 2 2 3 2 2 2 1 5 2 2 4 2 6 1 5 1 6 1 4 6 6 2 5 2 3 4 1 2 6 3 4 1 4 1 6 4 2 1 6 1 6 3 2 5 4 4 3 1 4 6 6 3 2 2 5 2 5 5 4 3 5 1 3 1 1 1 2 6 1 1 5 4 1 4 2 4 2 5 3 3 2

I want to clarify that these are the rolls? roll#1, #2, etc.

Oh and when I set int tally_array[7]={0} I get an error, but when I do: = [1,2,3,4,5,6,7] it compiles.
Last edited on
Thank you so much @AbstractionAnon !
Here's my final product:

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<iomanip>
using namespace std;

int main()
{ //Variable declaration
const int array_size = 6000;
int die;
int num_array[array_size];
int tally_array[7]={0};

srand(time(0)); //Seed

for (int i=0;i<array_size;i++)
{ die = rand()%6+1; // Roll the dice (1-6)
num_array[i] = die; // Save the roll in the array
tally_array[die]++; // Increment the tally for the roll
}

cout << "debug for num_array[]" << endl; //num_array[] debug
for (int i=100; i<200; i++) {
cout << num_array[i] << " ";
}
cout << endl;

cout << "debug for tally_array" << endl; //tally_array debug
for (int i=1; i<=6; i++) {
cout << "i: " << i << " " << tally_array[i] << endl;
}
cout << endl;

{ //What is being printed
cout << "Display Result for 6000 Rolls" << endl;
cout << "Die Face Occurance" << endl;
cout << "=========================" << setw(28) << " " << endl;
for (int i=1; i<=6; i++) {
cout << i << setw(21) << tally_array[i] << endl;
}
}

return 0;
}


Thank you guys for helping me out!
Last edited on
You've been asked multiple times to use code tags.
PLEASE DO SO.
Hey AbstractionAnon,

You've mentioned it only once.
And I'm confused on what you are asking. Are you referring to the comments I've included in my code? Or the [ code] [/ code] that only shows up in the initial question upon creation of the question? I've attempted to include them in the 2nd reply above but nothing happened. Maybe I'm doing it wrong, I'm not sure. Please explain, thank you.

EDIT: Ah I see what you're saying. I see what I did wrong. I wasn't sure of what you were asking. As you can see my confusion in saying my teacher keeps emphasizing it. I thought you were talking about the comments in my code. I will include them from now on.
Last edited on
You've mentioned it only once.

Sorry. You did have multiple messages without code tags.

Here's a reference on how to use code tags properly.
http://www.cplusplus.com/articles/jEywvCM9/

As you can see my confusion in saying my teacher keeps emphasizing it.

Not sure what you're referring to by that.
Topic archived. No new replies allowed.