arrays
May 31, 2020 at 11:37am UTC
Write a program to perform an analysis of the rolling of two dice. Your
program should use rand()%6 + 1 to roll the first dice and again to roll the second dice.With each dice roll,the outcome of the rolls should be tallied in a6x6 two-dimensional array that represents all possible combinations of dice rolls(as seen below). Repeat this dice roll process 10,000 times. On completion,display a report that shows the total counts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
int randdie;
int randdie2;
int die[6];
int die2[6];
int result[10000][10000];
for (int i = 0; i < 10000; i++)
{
for (int j = 0; i < 10000; i++)
{
randdie = rand() % 6 + 1;
randdie2 = rand() % 6 + 1;
die[randdie];
die2[randdie2];
result[die[randdie]][die2[randdie2]];
}
if (i == 10000) {
cout << i << endl;
}
}
system("pause" );
return 0;
this is what i have done so far but i dont get the proper dice values inside the arrays :( where should i change?
May 31, 2020 at 12:24pm UTC
With each dice roll,the outcome of the rolls should be tallied in a6x6 two-dimensional array
Why do you create an array[10000][10000] ?
May 31, 2020 at 12:32pm UTC
@GOdliike123
Problems..
Line 8. You have a for loop of j, but then check for i value.
lines 12, 13 and 14. You don't assign a value to the array locations
May 31, 2020 at 1:40pm UTC
thomas to store the results and whitenite idk i dont get the idea of how to use them
May 31, 2020 at 2:00pm UTC
Lets say the first throw of the dice is 1,1.
So you would increment the value of the array[1][1] by 1.
There are only 36 combinations possible so an array of 6 by 6 is enough.
Just be careful. array index is from 0..5 , but dice value 1..6
May 31, 2020 at 2:59pm UTC
die[randdie];
This fetches a value from the array named
die . It fetches the value at the
randdie element.
It doesn't change anything. It just fetches a value (which was never even set). It makes no sense at all.
1 2
die2[randdie2];
result[die[randdie]][die2[randdie2]];
These lines also. Fetching values, that were never set. Changing nothing. Doing nothing. Makes no sense.
Jun 1, 2020 at 4:08am UTC
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
#include <iostream>
int main()
{
int roll[6][6]{0};
int dice_1{0};
int dice_2{0};
for (int i = 0; i < 10000; i++)
{
dice_1 = rand() % 6 + 1;
dice_2 = rand() % 6 + 1;
roll[dice_1 - 1][dice_2 - 1]++;
}
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
std::cout
<< i + 1 << " - " << j + 1 << " occured "
<< roll[i][j] << " times\n" ;
}
}
return 0;
}
Jun 1, 2020 at 4:52am UTC
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
#include <iostream>
#include <iomanip>
int main()
{
const int Reps = 100000,
MaxVal = 6,
Width = 5, // for setw()
MaxBar = 60, // max bargraph bar
MaxCombo = MaxVal + MaxVal;
int roll[MaxVal][MaxVal] {};
int value[MaxCombo - 1] {}; // 2 to MaxCombo stored in 0 to MaxCombo-2
for (int i = 0; i < Reps; i++)
{
int d1 = rand() % MaxVal, d2 = rand() % MaxVal;
++value[d1 + d2];
++roll[d1][d2];
}
std::cout << " " ;
for (int c = 0; c < MaxVal; ++c)
std::cout << std::setw(Width) << c + 1 << ' ' ;
std::cout << '\n' ;
for (int r = 0; r < MaxVal; ++r)
{
std::cout << std::setw(2) << r + 1 << ' ' ;
for (int c = 0; c < MaxVal; ++c)
std::cout << std::setw(Width) << roll[r][c] << ' ' ;
std::cout << '\n' ;
}
int m = value[0];
for (int i = 1; i < MaxCombo - 1; ++i)
if (value[i] > m) m = value[i];
std::cout << '\n' ;
for (int i = 0; i < MaxCombo - 1; ++i)
std::cout << std::setw(2) << i + 2 << ": " << std::setw(Width) << value[i] << ' '
<< std::setfill('*' ) << std::setw(value[i]*MaxBar/m) << ""
<< std::setfill(' ' ) << '\n' ;
}
1 2 3 4 5 6
1 2760 2768 2875 2759 2791 2786
2 2778 2750 2827 2771 2767 2690
3 2763 2828 2841 2802 2816 2798
4 2795 2788 2883 2732 2815 2754
5 2723 2771 2762 2741 2769 2669
6 2777 2672 2752 2916 2793 2718
2: 2760 *********
3: 5546 *******************
4: 8388 *****************************
5: 11209 ****************************************
6: 13914 *************************************************
7: 16786 ************************************************************
8: 13672 ************************************************
9: 11106 ***************************************
10: 8439 ******************************
11: 5462 *******************
12: 2718 *********
Last edited on Jun 1, 2020 at 4:56am UTC
Jun 1, 2020 at 8:15am UTC
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand( time( 0 ) );
const int ROLLS = 10000, SIDES = 6, SIDESSQ = SIDES * SIDES;
int outcome[SIDESSQ]{}, freq[1+SIDES+SIDES]{};
for (int i = 0; i < ROLLS; i++ )
{
int n = rand() % SIDESSQ;
int die1 = n / SIDES + 1, die2 = n % SIDES + 1;
outcome[n]++;
freq[die1+die2]++;
}
cout << "Outcomes:\n" ;
for ( int n = 0; n < SIDESSQ; n++ ) cout << outcome[n] << ( ( n + 1 ) % SIDES ? '\t' : '\n' );
cout << "\nSum frequencies:\n" ;
for ( int n = 2; n <= SIDES + SIDES; n++ ) cout << n << ": " << freq[n] << '\n' ;
}
Outcomes:
297 291 268 292 274 265
279 281 279 307 274 264
321 280 301 256 303 242
306 242 274 268 269 278
284 256 280 285 275 286
268 273 260 296 263 263
Sum frequencies:
2: 297
3: 570
4: 870
5: 1157
6: 1408
7: 1593
8: 1388
9: 1056
10: 849
11: 549
12: 263
Last edited on Jun 1, 2020 at 8:23am UTC
Topic archived. No new replies allowed.