Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   HORNER rule to calculate value of a polynomial (http://www.go4expert.com/forums/horner-rule-calculate-value-polynomial-t25430/)

teacher 4Apr2011 19:20

HORNER rule to calculate value of a polynomial
 
HI to all .may be you all are asked to calculate the value of a polynomial at a given x.for eg let y=f(x)=1+2*x + 3*x*x .Just stay aside your regular technique to do this and try to understand this.May be you will find this useful.Search google for better defination of horner rule

This is another way to calculate the value at a given x.This solution is called HORNER's rule.no limit on the degree of polynomial

a0 + x0 *(a1 +x0*(a2+x0*(a3+x0*a4)))
as you can see it is a recursive function.


Code: cpp

int horner_rule(int poly[],int n,int x,int i){
    if(i==n)
    return poly[i];
    else
    return poly[i]+x*horner_rule(poly,n,x,i+1);
}
/*for equ like 2*x
*poly[]={0,2};
*/

int poly[]={1,2,3};// polynomial in the form 1+2*x+3*x*x....
int x0;
printf("enter value at which you want to calculate this polynomial :");
scanf("%d",&x0);
printf("%d",horner_rule(poly,(sizeof(poly))/sizeof(poly[0])-1,x0,0));
return 0;
}



All times are GMT +5.5. The time now is 13:21.