# Iteration Calculation

What is the best/right way to do an iteration calculation in Delphi? For example:

Item1   <Number>
Item2   <Number>
Item3   <Number>
Item4   <15% of Total>
Item5   <Number>
Total   <Total of Above>

James
Epsylon

I'm not sure what you mean but I think you need some expression calculator component:

http://www.torry.ru/expressions.htm

Or use the F1Book component from the ActiveX tab.
Not sure what you mean - can you explain a bit better?

The Neil =:)
I am not sure is this is that you want:

Total=(Item1+Item2+Item3+Item5)*100/(100-Item4)

Ivan

P.S. The question is not very clear.

My apologies...I guess when you're inside the paradigm you can't see it.

Let me try and clarify

Item 1    32,308
Item 2     1,341
Item 3    10,293
Item 4    Has to equal 15% of total of Items 1-5
Item 5    53,231

Total     Total of Items 1 through 5

The problem here is you can't figure out what 15% of the Total is until you figure out what the Total is. I think a human calculating this would keep trying (ie iterate) different numbers in the Item 4 and Total variables until it actually worked.

I was hoping that there was a preferred/correct/best way to do it in the Delphi world.

I hope this clarifies things a bit. If not check out the Iteration setting in MS-Excel.

Thanks
James
So, what is wrong with Ivan's comment...doesn't that satisfy the conditions?  You don't need to do trial-and-error thing here.  Add items 1, 2, 3, and 5, divide that by 0.85 and you get the total.  Multiply that by 0.15 and you will get item 4 (which is what Ivan is saying).

Could you clarify one step further?
Total := Item1 + Item2 + Item3 + Item5 + (0.15 * (Item1 + Item2 + Item3 + Item5 )) / 0.85

This does work as advertised. Thanks. I think what I'm really looking for is an single function/algorithm that can take any number of Items that have any number of Items with a "% of Total" setting and provide the answers for both the Total and all of the % of Total entires.

If the above examples from Ivan (and now Epsylon) will fit the bill then I apologize for belabouring the point.
Lets have :
y1+k1*Total+y2+y3+....+ym+kn*total=total

We know y1+y2+...+ym = Y;
Then :

Y = total*(1-k1-k2-...-kn)  or
total = Y/(1-k1-...-kn)

Replace Total in

k1*Total = k1*Y/(1-k1-k2-...-kn)
......

kn*Total = kn*Y/(1-k1-k2-..-kn)
Epsylon

membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hi. iteration is an engineering problem and, are we having trouble with the iteration algorythm or application of some mathematical techniques to delphi?

i think i've understood the problem but am i wrong? (iteration is good. unless it diverges :)
Might I propose some simplifications to Epsylon's code (the whole reproduced with edits):

procedure TForm1.Calculate(values, percentages: array of Double);
var totval, totperc, total: Double;
i: Integer;
begin
totval := 0;
for i := Low(values) to High(values) do
totval := totval + values[i];
totperc := 0;
for i := Low(percentages) to High(percentages) do
totperc := totperc + percentages[i];

Total := TotVal / (1-totperc)

for i := Low(values) to High(values) do
ListBox1.Items.Add('Item ' + IntToStr(i) + ' : ' + FloatToStr(values[i]));

for i := Low(percentages) to High(percentages) do
ListBox1.Items.Add('ItemP ' + IntToStr(i) + ': ' +
FloatToStr(Total * percentages[i]) +
' (' + FloatToStr(percentages[i] * 100) + '%)');

end;
Good example Eps, but the formula is the same like my.

Ivan
Well Ivan, yes, but he made it a generic solution.
> the formula is the same like my.

My formula doesn't have 'item4' in it so it's not the same....

Or am I missing something?