Help needed to expand sin(x)

This is a discussion on Help needed to expand sin(x) within the C Programming forums, part of the General Programming Boards category; well i am having a bit problem in expanding sin(x). The question says- Q> Write a program in C to ...

  1. #1
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79

    Help needed to expand sin(x)

    well i am having a bit problem in expanding sin(x).
    The question says-

    Q> Write a program in C to compute sin(x) =x - x^3/3! + x^5/5! -x^7/7! +....continue until the value of the next term becomes smaller than 10^-5 (in magnitude) . Test the program for x=1,x=2,x=3.In each case display the number of terms used to obtain the final answer .

    I don't know what error i am doing with this code -
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    float denominator(int);
    int main()
    {
      float n, x, a, b, sum = 0;
      int i, j, counter = 0;
      printf("Enter the number x for sin(x): ");
      scanf("%f", &x);
    
      for (i = 1, j = 1; i <= 19, j <= 19; i++, j += 2) {
    
        a = pow(-1, i + 1) * pow(x, j);
        b = denominator(j);
        n = a / b;
    
    
    
    
    
        sum = sum + n;
    
        counter++;
    
    
    
    
    
    
    
      }
    
      printf("sin(%f)= %f \n", x, sum);
      printf("No. of terms used = %d", counter);
      getch();
    
    
    }
    
    float denominator(int j)
    {
      int m;
      float h = 1;
      for (m = 1; m <= j; m++)
        h = h * m;
      return (h);
    
    }
    [/I]Please help !!
    Last edited by Salem; 11-04-2011 at 12:52 AM. Reason: remove pointless tags

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Are you ever comparing n with 1E-5?
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    no...cuz whenever i use the code
    Code:
    while (n>0000.1)
    {
    sum = sum +n;
    counter ++;
    }
    I am not getting the proper output...
    Last edited by rajarshi; 11-03-2011 at 11:37 PM.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Two things:
    *You want 0.00001 instead of 0000.1
    *For every second term, n is negative so even if n were -5000 the while loop would stop because -5000 is less than 0.00001. Try fabs(n) instead.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    "error". What tells you that there is an error?

    Please tidy your code up by the way.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    no error in the code sir
    error in the answer (not getting exactly what i want)

    using
    Code:
    while(fabs(n)>0.00001)
               
               {sum=sum+n;
               
            counter++;}
    gives the output :

    Enter the number x for sinx (x) =


    But even i enter the number x as 1,1.57,2...etc.
    I get the output as -
    Enter the number x for sinx (x) = 1.57
    Press any key to continue ....
    Last edited by rajarshi; 11-04-2011 at 12:07 AM.

  7. #7
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    sir, please suggest

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > continue until the value of the next term becomes smaller than 10^-5

    Is
    if ( fabs(n)>0.00001 ) break;

    Oh, and look up what the comma operator does in your for loop.
    It isn't the same as && or ||
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by Salem View Post
    > continue until the value of the next term becomes smaller than 10^-5

    Is
    if ( fabs(n)>0.00001 ) break;

    Oh, and look up what the comma operator does in your for loop.
    It isn't the same as && or ||

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    float denominator (int);
    int main()
    {
    	float f,n,x,a,b,sum=0;
    	int i,j,counter=0;
    	printf ("Enter the number x for sin(x): ");
    	scanf ("%f",&x);
    	
    	for (i=1 , j=1;i<=50 , j<=50;i++ , j+=2)
    	{
       		
       		a=pow(-1,i+1) * pow(x,j) ;
    		b=denominator(j);
    		n=a/b; 
    		f = fabs(n);
    		 
    		
    	   while(f>0.00001)
       		
       	  {
      	   	
      	     sum=sum+n;
       	    
    	    counter++; break; }
    		
    			
    		
    		
    		
    		
    	
    	}
    	
    	printf ("sin(%f)= %f \n",x,sum);
    	printf ("No. of terms used = %d",counter);
    	getch();
    	
    	
    }
    
    
    float denominator (int j)
    
    
    {
    	int m ;
    	float h =1;
    	for (m=1;m<=j;m++)
    	h=h*m;
    	return (h);
    	
    }
    This is the right code !! Thanks a lot !!

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    No - it isn't right at all.

    It stops you adding minor terms, but you're wasting all those loops calculating terms you're not using.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Yeah I see, the break breaks out of the while loop, but not out of the for loop.
    This will give the appearance that his program is doing what is asked, but in reality it isn't quite and may give slightly difference results that was asked for.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expand nightmare!
    By zach48191 in forum C Programming
    Replies: 9
    Last Post: 10-18-2011, 11:07 PM
  2. Expand
    By ASCII in forum C Programming
    Replies: 11
    Last Post: 10-03-2011, 04:01 AM
  3. Expand an array
    By SlyVixsky in forum C Programming
    Replies: 9
    Last Post: 08-01-2009, 11:28 PM
  4. expand(int s[]) 3-3 knr
    By olbas in forum C Programming
    Replies: 2
    Last Post: 04-06-2004, 11:01 AM
  5. CProgramming.com should expand!
    By Fool in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 10-05-2001, 10:22 AM

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