Thread: Evaluating sin series

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    22

    Question Evaluating sin series

    Hello everyone,
    I'm having a little trouble solving this question below and I do not know how to proceed further. And I also do not know that what I have done is correct. Can you kindly assist me with this problem.
    Here's the question:
    http://img10.imageshack.us/img10/5815/questiono.png

    Here's my answer :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>
    
    int main()
    { 
      clrscr();
      double x, a, b, c, m, n, i;
      printf ("Enter the value of sin x : ");
      scanf("%d", &n);
      for(i=1 ; i<=n ; i=i+2)
        {
           a = 2i-1;
           b = pow(x,a);
           for(j=1 ; j < 
           
        }
      printf("\nThe calculated value of sin x is ");
      printf("%d",m);
      getch();
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    22
    I just found the answer to this question on the net but I'm unable to run it.
    Code:
    #include <stdio.h>
    #include <math.h>
     
    double sinEst(double,unsigned);
    typedef double (*func)(double,double);
    double add(double,double);
    double subtract(double,double);
    double factorial(unsigned);
     
    const double PI = 3.1415926535897932384626433832795028841;
     
    typedef struct
      {
       func f[2];
       double k;
    } Op_t;
     
    int main(int argc, char *argv[]) {
       unsigned n;
       double x,xRad,sinX;
     
       if ((argc < 3) ||
           (sscanf(argv[1],"%lg",&x) != 1) ||
           (sscanf(argv[2],"%u",&n) != 1)) {
          printf("usage : %s <x>,<n>\n",argv[0]);
          printf(" where <x> = angle for sin estimate, in degrees\n");
          printf(" <n> = number of terms, a positive integer\n");
          return -1;
       }
       xRad = x * PI / 180.0;
       sinX = sinEst(xRad,n);
       printf("\nnumber of terms in series = %u",n);
       printf("\nestimated sin(%g) = %g",x,sinX);
       printf("\nactual sin(%g) = %g",x,sin(xRad));
       getch();
       return 0;
    }
     
    double sinEst(double x, unsigned numTerms) {
       double sinX = x;
       Op_t op;
       unsigned i;
     
       op.f[0] = add;
       op.f[1] = subtract;
       op.k = 3;
       for (i = 1; i < numTerms; i++, op.k += 2) {
          sinX =
            op.f[i%2]( sinX,pow( x,op.k ) /
                      factorial( (unsigned)op.k ) );
       }
        getch();
        return sinX;
    }
     
    double factorial(unsigned n) {
       if (n == 1) return n;
       return n * factorial(n-1);
    }
    double add(double a,double b) { return a + b; }
    double subtract(double a,double b) { return a - b; }
    This is my output:

    usage : lab5c.exe <x>,<n>
    where <x> = angle for sin estimate, in degrees
    <n> = number of terms, a positive integer

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The program requires you to input command-line parameters for it to do anything. If you're running from an IDE there are typically menu options you can work through to "set" these when running the program. Either that or the values you did provide could not be converted properly. How are you running the program (from an IDE or the command-line) and what options are you providing to the program?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    22
    Quote Originally Posted by hk_mp5kpdw View Post
    The program requires you to input command-line parameters for it to do anything. If you're running from an IDE there are typically menu options you can work through to "set" these when running the program. Either that or the values you did provide could not be converted properly. How are you running the program (from an IDE or the command-line) and what options are you providing to the program?
    I'm running the program through command-line. But the command prompt window closes immediately when I run through Dev C++ Compiler.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by alokrsx View Post
    the command prompt window closes immediately when I run through Dev C++ Compiler.
    That's because you have the return -1 statement. It causes the program to immediately exit which closes the nice window the IDE opened for you. You'd need to put some type of blocking read op in there before the return statement to keep the window open instead of immediately closing.

    Code:
       if ((argc < 3) ||
           (sscanf(argv[1],"%lg",&x) != 1) ||
           (sscanf(argv[2],"%u",&n) != 1)) {
          printf("usage : %s <x>,<n>\n",argv[0]);
          printf(" where <x> = angle for sin estimate, in degrees\n");
          printf(" <n> = number of terms, a positive integer\n");
          return -1;
       }
    Quote Originally Posted by alokrsx
    I'm running the program through command-line.
    So what parameters are you passing to it?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sine series summation
    By kashya in forum C Programming
    Replies: 3
    Last Post: 12-17-2008, 08:00 PM
  2. Sine (Sin) Algorithm Help
    By StaticKyle in forum C Programming
    Replies: 52
    Last Post: 05-13-2008, 08:37 AM
  3. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  4. Sin Tables (and rotations)
    By Epo in forum Game Programming
    Replies: 15
    Last Post: 05-29-2005, 05:41 PM
  5. integral of 1/x
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 03-19-2004, 07:47 AM