Normalize dice roll array to fit 60-character width limit

  • Context: C/C++ 
  • Thread starter Thread starter Colton0117
  • Start date Start date
  • Tags Tags
    Array C++ Normalize
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 4K views
Colton0117
Messages
5
Reaction score
0
I need to normalize an array, I find the max value which can be any integer. I have to output this number using count and represent it as 'x' how ever many times. The catch is I can only have a max amount of 60 x's. If my max is 500 I need to display it as 60 'x' and normalize my whole array to that.The code i have written is below.

C++:
void RollDice() {  
int DieFace[6];
int Rolls = 0;
int Seed = 0;
int Random = 0; 
int i = 0; 
char X = 'x';

DieFace[0] = 0;
DieFace[1] = 0;
DieFace[2] = 0;
DieFace[3] = 0;
DieFace[4] = 0;
DieFace[5] = 0; count << "Enter number of times dice will be rolled. "; //output to user
cin >> Rolls; //get rolls
count << endl;count << "Enter desired seed number. "; //output to user
cin >> Seed; //get seed
count << endl;
srand(Seed);for (int i = 0; i < Rolls; ++i) { //loop for RNG
Random = 1 + rand() % 6;

// boolean to get Face
if (Random == 1)
    DieFace[0] = DieFace[0] + 1;
else if (Random == 2)
    DieFace[1] = DieFace[1] + 1;
else if (Random == 3)
    DieFace[2] = DieFace[2] + 1;
else if (Random == 4)
    DieFace[3] = DieFace[3] + 1;
else if (Random == 5)
    DieFace[4] = DieFace[4] + 1;
else if (Random == 6)
    DieFace[5] = DieFace[5] + 1;
} 

for (i = 0; i < 6; i++) {
count << i + 1 << " Was Rolled: " << DieFace[i] << " Times." << endl;
}
count << endl;

int Max = std::max({DieFace[0], DieFace[1], DieFace[2], DieFace[3], DieFace[4], DieFace[5] });   // find max value

This was a suggested solution.

Say that your Max is 500. If you multiply that by 60 and then divide by 500, the result is precisely 60. If you have another value that's half of max (250), multiplying by 60 gives 15000 and then dividing by 500 gives 30.

So the simple solution is: Multiply all outcomes by the desired maximum, and then divide by the actual maximum.

You don't need double, even though a comment claims so. 500*60 cannot overflow. It might be wise to use a long though, because using int is cutting it rather close. 600*60 might overflow an int (>32767) but a long goes to 2 billion.

Now can I easily convert int DieFace[6] to long DieFace[6]? I've never seen an array using the long type.
 
Last edited by a moderator:
Physics news on Phys.org
I'm not sure what your question is, but the posted code could be simplified a fair amount.
Instead of doing this:
C:
for (int i = 0; i < Rolls; ++i) { //loop for RNG
    Random = 1 + rand() % 6;

    // boolean to get Face
    if (Random == 1)
        DieFace[0] = DieFace[0] + 1;
    else if (Random == 2)
        DieFace[1] = DieFace[1] + 1;
    else if (Random == 3)
        DieFace[2] = DieFace[2] + 1;
    else if (Random == 4)
        DieFace[3] = DieFace[3] + 1;
    else if (Random == 5)
        DieFace[4] = DieFace[4] + 1;
    else if (Random == 6)
        DieFace[5] = DieFace[5] + 1;
}

You could do this:
C:
for (int i = 0; i < Rolls; ++i) { //loop for RNG
    Random = 1 + rand() % 6;
    Dieface[Random - 1] += 1;
}
 
This looks like it's supposed to make an ASCII histogram of the outcomes of a series of dice rolls, normalizing it so the longest bar has length 60.
 
Reply
  • Like
Likes   Reactions: Greg Bernhardt