1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

please identify the mistake in the given coding

Discussion in 'C' started by sumedh yadav, Aug 19, 2012.

  1. sumedh yadav

    sumedh yadav New Member

    Joined:
    Aug 18, 2012
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Write a function to find value of sin(x) using Expansion Series given below:
    Code:
    sin(x) = x - x^3/3! + x^5/5! - x^7/7!......
    
    answer-
    #include <stdio.h>
    #include <math.h>
     int factorial(int a) {
     	int i,k;
     	for(i=a;i>0;i--) {
    	 	k=k*i;
    	 }
    	 return k;
     }
     
     float power(float a,float b) {
     	float i,j;
     	j=1;
     	for(i=1;i<=b;i++)  {
    	 	j=j*a;
    	 }
    	 return j;
     }
     
     int main()  {
     	float i,x,y,term,sum=0;
     	printf("Enter the variable x\n");
     	printf("Enter the prescribed precision\n");
     	scanf("%f%f",&x,&y);
     	for(i=1;(term/sum)*100<=y;i++) {
    	 	term=power(-1,(i+1))*power(x,(2*i-1))/factorial((2*i-1));
    	 	sum+=term;
    	 }
    	 printf("sin(%f)=%f",x,sum);
    	 return 0;
     }
     
  2. louies89

    louies89 New Member

    Joined:
    Oct 4, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    In for(i=1;(term/sum)*100<=y;i++) you have done like(term/sum) which is 0/0 which is not valid mathematical operation.
     
  3. louies89

    louies89 New Member

    Joined:
    Oct 4, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    And why are you dividing it and comparing it with "y"?
     
  4. sumedh yadav

    sumedh yadav New Member

    Joined:
    Aug 18, 2012
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    (Term/sum) is the precision term which is compared with 'y' (here y is the max tolerable error)
     

Share This Page