Hello everyone, i want to do the summation below:

x1= a0x0
x2= x1+ a1
x1
x3= x1 + x2 + a2*x2 and so on............

supose we know x0 = 2; a0=1 ; a1 = 3; a2 = 2;

how do i use for loop to do this?
should i keep it in array?

## All 4 Replies

how do you seperate your different parts? is there always a space between the 'value' and the operators or not?

What you can do for instance is put all your coefficients (aka a0,a1, etc) in an array, then make a for loop through that array.

Just keep a value say x, and every time you enter the for loop, add x+ai*x

EDIT: Didn't read your third line for the summation thoroughly, instead of adding x+aix, you get in fact (n-1)x1+(n-2)a1x1+(n-3)a2*x2 etc, just write the full sum down for say x4 and you'll see what I mean, the approach is the same, the formula just changes a bit. Or you could just put all the x's in a different array as well, and then in the for loop, also loop through the previous x's

Although you can look at the iterative definition and convert it to an explicit formula, this gives a solution, no matter how efficient, that hides rather than exposes the original definition. For that reason I would go with the second approach - two arrays, x and a. Array a must be fully populated, and x must be given a value, then you can have a simple loop that fills in the remaining values for the x array.

Thanks so much sir .it's really useful ^_^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.