### Arrays

I have generated 5 random dice value. That I have generated. Let say the 5 dice values are 2 2 4 5 6. I then add both of the similar value, i will then cin the number I want to add in . e.g cin 2. The program will auto calculate how many 2's are there and add them together before allowing the user to choose where he/she wants to store that value in. In this case I wish to store the value into my int b[size]. Please help thanks alot guys !

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

int main()
{
const int size = 1;
int dices[5] = {0};
int a[size];// all 1 value add together put in this array
int b[size];// all 2 value add together put in this array
int c[size];// all 3 value add together put in this array
int d[size];// all 4 value add together put in this array
int e[size];// all 5 value add together put in this array
cout <<" 5 random dice value will appear " << endl;

for(int m=0; m<6; m++)
{
dices[m]=(rand()%6 + 1);
}
cout<<"Dices"<<dices[0]<<""<<dices[1]<<""<<dices[2]<<""<<dices[3]<<""<<dices[4]<<endl ;

return 0;
}
Last edited on
I don't quite understand what you want to do with the user input. But leaving that aside for now, there is at least one error and some unusual design in the code.

First, the loop
 `` `` `` for(int m=0; m<6; m++)``
will repeat six times.
`m` has the values 0, 1, 2, 3, 4, 5. Unfortunately the array only has room for five values - the last one is an out of bounds access of the array, giving undefined results - the program could crash or behave unpredictably, anything could happen.

Secondly, defining five separate arrays, each with a size of just one, that seems somehow a bit perverse. Wouldn't a single array containing all five values make more sense?

Also, judging from the comments in the code, you want to accumulate the total score from each of the five dice in one of these locations. You might do that like so.
 ``123456789101112131415161718192021222324252627282930313233`` ``````#include #include #include #include using namespace std; int main() { const int size = 5; int dices[size] = {0}; int total[size] = {0}; cout << " " << size << " random dice values will appear \n"; for (int m = 0; m < size; m++) { dices[m] = rand() % 6 + 1; cout << "m = " << m << " dices[m] = " << dices[m] << '\n'; total[m] += dices[m]; } cout << "Totals: "; for (int m = 0; m < size; m++) { cout << setw(4) << total[m]; } cout << endl ; return 0; } ``````

Of course, the total is the same as the individual dice score, but if you were to repeat the five throws more than once, it would accumulate all of the scores for each die.

Or - question - did you want to add all five dice together to get the total of the five?
I am actually creating this game;
When the dice has been randomly roll;
2 2 3 4 3
Straight 1:
Straight 2: 4 ( I then choose the category straight 2, the program would auto calculate how many 2 are there and store the total value into the straight 2 array)
Straight 3:
Straight 4:
Straight 5:
Straight 6:

2nd roll at random roll I got:
2 3 3 2 4

Straight 1:
Straight 2: 4
Straight 3: 6 ( I then choose the category straight 3, the program would auto calculate how many 3 are there and store the total value into the straight 2 array)
Straight 4:
Straight 5:
Straight 6:

On the final throw my dice came out with a result of 2 2 3 5 6
With only straight 4 left I have no choice but to store a dice value of zero into my straight 4 array.

Straight 1: 2
Straight 2: 4
Straight 3: 6
Straight 4:
Straight 5:10
Straight 6: 12
Last edited on
Topic archived. No new replies allowed.