Click to See Complete Forum and Search --> : help on algorithm


sammysammy
January 26th, 2011, 06:34 AM
Hi,

I need some help with an algorithm that can do the following.

The method receives 3 input parameters:

1. double sum;
2. double[] shares;
3. int decimals;

Return: double[] values;

For instance if:
sum = 200;
shares = {30.0, 20.0, 50}
decimals = 2;

return: {60.00, 40.00, 100.00};

Point is, the sum of the elements should equal to the input parameter sum.

If the shares array does not sum up to 100% it should calculate the relative sum and use this instead.

Example:
sum = 200;
shares = {30.0, 20.0} //sum is 50... recalculate to get {60,40}
decimals = 2;

return: {120.00, 80.00};


Any help is really appreciated!

Xorlium
February 24th, 2011, 03:16 PM
Don't you just multiply every element in shares[] by sum/(sum of shares[])?