sum of the series - loop?

This is a discussion on sum of the series - loop? within the C Programming forums, part of the General Programming Boards category; Hi guys. I have to calculate the sum of the following series: MathBin.net - j with the precision of 10^-8 ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    32

    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!

  2. #2
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    951
    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.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    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.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So change the condition that causes the program to stop looping.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    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?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Fix the errors in what you have written. (Compile it, run it, see what happens.)

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    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;
        }

  8. #8
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by jacek View Post
    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

    %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"
    Last edited by Obelisk; 10-24-2009 at 08:38 PM.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    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. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    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;
        }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What about it?

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    I guess it jus adds next elements to each other. But it should substract and add alternately. How to do it?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's because you forgot the (-1) raised to the n power.

  15. #15
    Registered User
    Join Date
    Oct 2009
    Posts
    32
    Do you mean switching
    Code:
    element=(sin(n*x))/pow(n,3);
    to
    Code:
    element=(sin(n*x)*pow(-1,n))/pow(n,3);
    ?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  2. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  3. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-30-2006, 11:59 PM
  4. is my loop badly set up?
    By mabufo in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2006, 02:40 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21