Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horner's rule in C++

Tags:

c++

math

While trying to evaulate polynomials using Horner's Rule I have a sample code segment like so:

int Horner( int a[], int n, int x )
{
    int result = a[n];
    for(int i=n-1; i >= 0 ; --i)
        result = result * x + a[i];
    return result;
}

I understand that a is an array of coefficients and that x is the value that I would like to evaluate at. My question is what is the n for?

like image 730
Raisins Avatar asked Dec 03 '22 07:12

Raisins


2 Answers

n is the degree of the polynome (and a polynome of degree n, aside from 0 which is kind of special, has n+1 coefficients, so size of array = n+1, n = size of array - 1)

like image 71
Vincent Lascaux Avatar answered Dec 24 '22 11:12

Vincent Lascaux


n is the size of the array

like image 38
catwalk Avatar answered Dec 24 '22 10:12

catwalk