HORNER rule to calculate value of a polynomial

Discussion in 'C' started by teacher, Apr 4, 2011.

  1. teacher

    teacher New Member

    Joined:
    Mar 27, 2011
    Messages:
    66
    Likes Received:
    8
    Trophy Points:
    0
    Occupation:
    i hate jobs
    Location:
    india,agra
    Home Page:
    http://www.crazylearner.in
    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:
    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;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice