Could anywhone help me to write sum for S=(1+x+x^i)+(1+x+x^(i+1) ) + ... + (1+x+x^n) ? Exponent i i staking values from i=1 to i=n . User gives n.
Thank You very much in advance .
This is a discussion on How to sum (1+x+x^1)+....(1+x+x^n) in C within the C Programming forums, part of the General Programming Boards category; Could anywhone help me to write sum for S=(1+x+x^i)+(1+x+x^(i+1) ) + ... + (1+x+x^n) ? Exponent i i staking values ...
Could anywhone help me to write sum for S=(1+x+x^i)+(1+x+x^(i+1) ) + ... + (1+x+x^n) ? Exponent i i staking values from i=1 to i=n . User gives n.
Thank You very much in advance .
What have you tried?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ive tried for loop but it gives me incorect value
Code:for (i=1;i<=n;i++) { Sum=(1+x+pow(x,i)); } printf("sum : %.2f\n",SumB);
There are two ways to compute this:
1) The way you did it, only YOU ARE NOT ADDING TO SUM just initializing sum to the last term computed.
2) Get a pen and paper and compute the sum.
Ill give you a hint:
The result is n*(x+1) + (x^(n+1) - 1)/(x-1)
Well Claudia, I appreciated your answer, but after 12h and counting in font of the monitor, ill try to resolve this problem ,but i would be very gratefull too you, if you could tell me a little bit more. Thank you so much
Change this to:
Try that.Code:double sum = 0.0; for (i=1;i<=n;i++) { sum= sum + (1+x+pow(x,i)); } printf("sum : %.2f\n",sum);
You were not adding anything to sum, insteand just re-initializing sum to the current term in the iteration. Secondly, you were not even printing the results of sum but the results of sumB some variable we know nothing about given your posting.
As for the math behind it, I recommend you brush up your algebra by reading online about geometric series.
http://en.wikipedia.org/wiki/Geom
etric_series
Also, think I miscalculated in the previous post, the corect result i believe is what i wrote there minus 1.
Thank you VERY much, im so grateful for your help. The reason for sumB is my mistake cause ive tried so many things to do the job,and forget to delete it. The program originaly is typed for region with non english speaking population so many variables is writen in diferent languange with english alphabet. Sory for any inconveniace.
Here is the task and the problem in it,i dont know whats wrong
Code:////////////////////////////////////////////// //How to calculate Sum /////////////////////// //when you have x betwen values/////////////// //xmin and xmax/////////////////////////////// //with step Dx /////////////////////////////// //and exponent i is taking//////////////////// //value betwen 1 and n /////////////////////// // S=(1+x+x^i)+(1+x+x^(i+1))+...+(1+x+x^n)//// ////////////////////////////////////////////// #include <stdio.h> #include <math.h> main() { double x,xMIN,xMAX,Dx,Sum; int i,n; printf("Enter the first border for x : "); scanf("%lf",&xMIN); printf("Enter the second border for x : "); scanf("%lf",&xMAX); printf("Enter step for x (Dx) : "); scanf("%lf",&Dx); printf("Enter top exponent : "); scanf("%d",&n); for (x=xMIN;x<=xMAX;x+=Dx) { printf("X is :%.2f",x); for (i=1;i<=n;i++) { Sum=(1+x+pow(x,i)); } printf("sum : %.2f\n",Sum); } }
i works like a charm id just added sum=0; after printf of the sum thank you Very much !!!