I need to populate a vector so that it holds a sum of weights, where the sum must be 100. In other words, the number of items is equal to the divisor, and its values are the quotient, to ensure (force) the sum of the vector to equal 100.
Something like this: 100/3=3.333333…
vector[0]=33.33
vector[1]=33.34
vector[2]=33.33
The sum of this needs to be exactly 100 (some sort of selective rounding?)
Another example: 100/6 = 16.66666667
vector[0]=16.67
vector[1]=16.67
vector[2]=16.66
vector[3]=16.67
vector[4]=16.67
vector[5]=16.66
I’ve seen something like this done in grocery stores where something on sale might be 3 for $11, so the register displays the prices like 3.67, 3.66, and so on.
The values must add up to exactly 100 though I was thinking of doing this with an epsilon but that wouldn’t work.
const int divisor = 6;
const int dividend = 10;
std::vector<double> myVec;
myVec.resize(6);
for (int i = 0; i < divisor; ++i)
{
...some magic that I don't know how to do
}
EDIT: The client wants the values stored (and displayed) in values fixed at two decimal places to visually see they add to 100.
You can divide into whatever is left as you go, subtracting the last value from the remaining amount.