# sum of the series - loop?

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-23-2009
jacek
sum of the series - loop?
Hi guys.

I have to calculate the sum of the following series: MathBin.net - j with the precision of 10^-8

I need to use the following approximation: MathBin.net - approx while the stopping criterion should be: MathBin.net - stopping criterion

The next partial sum should be calculated from the previous one.

The program should be pretty similar to To find the sum of the sine series - free C source codes however here it's the user who enters the number of terms.

How to do this with the other way?

Thanks!
• 10-23-2009
Epy
The answer is right in front of you with that free source code right there, we're not here to do your homework for you. It doesn't help the learning process if you just search for existing code, either.
• 10-23-2009
jacek
Yes, you're right. But that program totally differs from mine - I have no idea how to make it without entering the number of terms by myself.
• 10-23-2009
tabstop
So change the condition that causes the program to stop looping.
• 10-24-2009
jacek
OK, so far i have written this:

Code:

```#include <stdio.h> #include <math.h> int main()     {     float sum,epsilon,x,elem;     int k;     sum=0;             do     {     printf("Enter the value of x");     scanf("%d", &x);     if (x<(3.14)) &&  (x>(3.14));     {     printf("X must be between -pi and pi");     }```
Could you help me and tell what to do next? :)
• 10-24-2009
tabstop
Fix the errors in what you have written. (Compile it, run it, see what happens.)
• 10-24-2009
jacek
I made some changes, but still there's some error.

Code:

```#include <stdio.h> #include <math.h> int main()     {     float sum,epsilon,x,elem;     int k;     sum=0;             do     {     printf("Enter the value of x");     scanf("%d", &x);     }     if x<(3,14) || x>(3,14)     {     printf("X must be between -pi and pi");     }     return 0;     }```
• 10-24-2009
Obelisk
Quote:

Originally Posted by jacek
I made some changes, but still there's some error.

Code:

```#include <stdio.h> #include <math.h> int main()     {     float sum,epsilon,x,elem;     int k;     sum=0;             do     {     printf("Enter the value of x");     scanf("%d", &x);     }     if x<(3,14) || x>(3,14)     {     printf("X must be between -pi and pi");     }     return 0;     }```

ok

Code:

`  scanf("%d", &x);`
x---> is a float and why are you using "%d" for a float?

shouldnt it be do with while loop.

instead

Quote:

%c -- characters
%d -- integers
%f -- floats
what exactly do you mean by this?
Code:

`    if x<(3,14) || x>(3,14)`
I think what you meant was "3.14" instead of "3,14"
• 10-25-2009
jacek
I have been working a few hours on this. I think that it shows what I want to get. Help will be appreciated.

Code:

```#include <stdio.h> #include <math.h> int main()     {     float sum,x,element;     int n;         n=1;     sum=0;         while ((x < 3.14) || (x>3.14))     {     printf("Enter the value of x. It must be between -pi and pi");     scanf("%f", &x);     }         element=sin(x); //first element     sum=element;         while (element>1e-8)     {     n+1     element=(sin(n*x))/(n^3);     printf("Elements %d: %.10f sum:%.10f n:%d", n,element,sum,n);     sum=element+sum;     }             return 0;     }```
• 10-25-2009
tabstop
You can't just add 1 to n. You then have to put that new result somewhere.

To raie something to a power, you need to use pow.
• 10-25-2009
jacek
ok. what about this?

Code:

```#include <stdio.h> #include <math.h> int main()     {     float sum,x,element;     int n;         n=1;     sum=0;         do     {     printf("Enter the value of x. It must be between -pi and pi");     scanf("%f", &x);     }     while (x < -atan(1)*4 || x>atan(1)*4);        //atan(1)*4=3.14...         element=sin(x);        //first element     sum=element;         while (element>1e-8 || element<-1e-8)     {     n=n+1;     element=(sin(n*x))/pow(n,3);     printf("Elements %d: %.10f sum:%.10f n:%d\n", n,element,sum,n);     sum=element+sum;     }             return 0;     }```
• 10-25-2009
tabstop
What about it?
• 10-25-2009
jacek
I guess it jus adds next elements to each other. But it should substract and add alternately. How to do it?
• 10-25-2009
tabstop
That's because you forgot the (-1) raised to the n power.
• 10-25-2009
jacek
Do you mean switching
Code:

`element=(sin(n*x))/pow(n,3);`
to
Code:

`element=(sin(n*x)*pow(-1,n))/pow(n,3);`
?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last