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

  • Context: C/C++ 
  • Thread starter Thread starter Colton0117
  • Start date Start date
  • Tags Tags
    Array C++ Normalize
Click For Summary
SUMMARY

This discussion focuses on normalizing an array in C++ to represent the frequency of dice rolls as a maximum of 60 'x's. The key solution involves multiplying the actual maximum value of the array by 60 and dividing by the maximum observed value to achieve the desired normalization. The provided code snippet demonstrates how to count the occurrences of dice rolls and suggests optimizing the counting loop. Additionally, it highlights the importance of using the 'long' data type to prevent overflow when dealing with larger numbers.

PREREQUISITES
  • C++ programming fundamentals
  • Understanding of arrays and their manipulation
  • Knowledge of random number generation in C++
  • Basic concepts of normalization in data representation
NEXT STEPS
  • Implement normalization techniques for different datasets in C++
  • Explore the use of 'long' data types in C++ for larger numerical ranges
  • Learn about advanced random number generation methods in C++
  • Study optimization techniques for loops and array manipulations in C++
USEFUL FOR

Software developers, particularly those working with C++ and interested in data normalization techniques, as well as educators teaching programming concepts related to arrays and random number generation.

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:
Technology 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.
 
  • Like
Likes   Reactions: Greg Bernhardt

Similar threads

  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
12
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K