- #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.
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.
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: