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
#include<iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
usingnamespace std;
int main()
{
// declare variables
constint 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;
}
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?
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]++;
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.
#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
}
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.