Thread: polynomials

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    9

    Question polynomials

    I'm new to C programming and I have an assignment that's really been bugging me. I have to write a function
    double eval(double p[], double x, int n)
    {
    }
    that returns the value of polynomial p evaluated at x using Horner's Rule which is p(x) = a0 + x(a1 + (a2 + x(a3 + x(a4 + x(a5 )))))

    Here's what I've written so far:

    #include <stdio.h>
    #include <math.h>
    #define N 5
    double eval(double p[], double x, int n);

    int main() {

    double polynomial; /* Used for return value from eval function */

    double p[N + 1];

    /* Print results of polynomial evaluation */
    printf("Polynomial evaluated at x: %f\n\n", polynomial);

    return 0;
    }

    /* Polynomial evaluation function */
    double eval(double p[], double x, int n) {
    double result;
    int i;
    if (n <= 0)
    {
    return 0.0;
    }
    result = p[0];

    for (i=1; i <= n; i++)
    {
    result = result + p[i] * pow(x, i);
    }

    return result;
    }
    Am I heading in the right direction? Comments???

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    >>result = result + p[i] * pow(x, i);

    Below i have added a picture taken from google for that guys rule. I would say, yes you are in the right direction, are there any problems?

    --edit--
    Question: What is x? What do you pass? and where is the function call in main?
    Last edited by stumon; 04-09-2003 at 05:04 PM.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    ... but notice that you are not using Homer's Rule (D'oh!). You have correctly described it at the top of your post. Think about a for-loop that evaluates each of the brackets in turn. The resulting code will be more efficient than repeatedly using pow(a,b).

    actually you almost got the algorithm right (probably just a typo) it should be:

    a0 + a1 * x + a2 * x^2 + a3 * x^3 + a4 * x^4 + a5 * x^5 =
    a0 + x * (a1 + x * (a2 + x * (a3 + x * (a4 + x * (a5)))))

    Good luck,
    Last edited by DavT; 04-10-2003 at 06:30 AM.
    DavT
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Multiplying Two Polynomials
    By CaptainMorgan in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2006, 02:34 PM
  3. Factoring Polynomials in C++
    By MethSnax in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 01:51 PM
  4. Polynomials with linked Lists
    By shaheedpak in forum C++ Programming
    Replies: 0
    Last Post: 09-26-2001, 11:10 AM
  5. Generating Polynomials using Linked Lists
    By shaheedpak in forum C++ Programming
    Replies: 0
    Last Post: 09-25-2001, 01:22 PM