Normalize Array w/ C++ & Max of 60 x's

In summary, the code creates an array of six integers, each representing a face on a six-sided die, and then loops through the array, randomly selecting a value from the array and adding 1 to the corresponding die face's value. If the value is 1, it increases the value of the die face by 1; if it is 6, it decreases the value of the die face by 1. The code then stores the randomly selected value in a variable called DieFace[Random - 1].
  • #1
Colton0117
5
0
I need to normalize an array, I find the max value which can be any integer. I have to output this number using cout 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; cout << "Enter number of times dice will be rolled. "; //output to user
cin >> Rolls; //get rolls
cout << endl;cout << "Enter desired seed number. "; //output to user
cin >> Seed; //get seed
cout << 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++) {
cout << i + 1 << " Was Rolled: " << DieFace[i] << " Times." << endl;
}
cout << 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:
Technology news on Phys.org
  • #2
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;
}
 
  • #3
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.
 
  • Like
Likes Greg Bernhardt

FAQ: Normalize Array w/ C++ & Max of 60 x's

What is the purpose of normalizing an array?

Normalizing an array means scaling all the values in the array to fit within a specific range. This can be useful for data analysis and machine learning algorithms that require all values to be within a certain range for optimal performance.

How is normalization done in C++?

In C++, normalization of an array can be done by dividing each value in the array by the maximum value in the array. This will result in all values being within the range of 0 to 1.

Why is the maximum value limited to 60 in this case?

This is because the normalization process involves dividing each value by the maximum value, and having a maximum value of 60 ensures that the resulting normalized values will still be within the range of 0 to 1. If the maximum value was higher, the normalized values may exceed 1.

Can normalization be done for arrays with negative values?

Yes, normalization can be done for arrays with negative values by using the absolute value of the maximum value in the array. This will ensure that all values will be within the range of 0 to 1.

Are there any other methods for normalizing an array?

Yes, there are other methods for normalizing an array such as min-max normalization, z-score normalization, and decimal scaling normalization. The method used will depend on the specific needs and requirements of the data being analyzed.

Similar threads

Replies
5
Views
2K
Replies
23
Views
2K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
10
Views
2K
Replies
23
Views
2K
Back
Top