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

AI Thread Summary
The discussion focuses on normalizing an array of dice roll outcomes to a maximum representation of 60 'x's, based on the highest roll count. The user initially presents code that counts the occurrences of each die face after rolling a dice a specified number of times. The key point is to normalize the maximum count to 60, using a formula that multiplies each count by 60 and divides by the actual maximum count. Concerns about potential integer overflow are raised, suggesting the use of a long data type instead of int for the array to accommodate larger values safely. Additionally, suggestions for simplifying the code are provided, particularly in how to increment the counts for the dice faces, recommending a more efficient approach using array indexing instead of multiple conditional statements. Overall, the conversation emphasizes both the normalization process and code optimization for clarity and efficiency.
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 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
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 Greg Bernhardt
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top