Thread: Issue with Horner's polynomial c program

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    Issue with Horner's polynomial c program

    Code:
    #include<stdio.h>
    #include<conio.h>
    int horner(int,int);
    int count=0;
    void main()
    {
         /*Horner's rule for evaluating a polynomial */
         /* let us take a0=0,a1=1,a2=2.. and so on */
         
         int n,x,h=0; //n is order, x is value of X in polynomial.
         scanf("%d %d",&n,&x);
         h=horner(n,x);
         printf("%d",h);
         getch();
    }
    
    int horner(int n, int x)
    {
        if(count!=n)
        {
                    count++;
                    printf("%d+%d*(",count-1,x);
                    return (count-1 + x*horner(n,x));
        }
        else
        {
            printf("%d))=",count);
        return count;
        
    }
         
    }
    Can anybody help me in finding the issues with above program of evaluating a polynomial with horner's rule? I think the program is correct but it shows errornous output

  2. #2
    Registered User
    Join Date
    May 2011
    Posts
    4
    Additional Info: this program uses Recursive mechanism. 'n' is the order of polynomial.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You may get a hint just by looking at your output. If I type "4 1", I'm looking for
    Code:
    (((0*1+1)*1+2)*1+3)*1+4
    Your code prints, and evaluates,
    Code:
    0+1*(1+1*(2+1*(3+1*(4))
    Notice how your + and * are in the wrong place, and you've got your grouping going the wrong way.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by tabstop View Post
    You may get a hint just by looking at your output. If I type "4 1", I'm looking for
    Code:
    (((0*1+1)*1+2)*1+3)*1+4
    Your code prints, and evaluates,
    Code:
    0+1*(1+1*(2+1*(3+1*(4))
    Notice how your + and * are in the wrong place, and you've got your grouping going the wrong way.
    From the definition of Horner's Rule(Horner's Rule -- from Wolfram MathWorld), if I type "4 1", the output should be:
    ((((4)*1+3)*1 + 2)*1 +1)*1 +0
    which is exactly the same (in reverse direction).
    Thnx for considering.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Oh so you want the coefficients to go the other way. Well then, your answers are correct, or at least I've not been able to come up with one that isn't.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    4
    The issue with above problem is finally resolved.
    It was the global variable "count" which was getting changed even before the original return expression was evaluated. Hence we take a local variable 'i' and store in it the instantaneous value of "count". Here is the solution:

    Code:
    #include<stdio.h>
    #include<conio.h>
    int horner(int,int);
    int count=0;
    void main()
    {
         /*Horner's rule for evaluating a polynomial */
         /* let us take a0=0,a1=1,a2=2.. and so on */
         
         int n,x,h=0; //n is order, x is value of X in polynomial.
         scanf("%d %d",&n,&x);
         h=horner(n,x);
         printf("%d",h);
         getch();
    }
    
    int horner(int n, int x)
    {
        int i;
        if(count!=n)
        {
                    i=count;
                    count++;
                    return (i + x*horner(n,x));
        }
        else
        {
            return count;
        }
         
    }
    Thank you all for your valuable suggestion. And thank you Mr. Puneet Jindal for providing useful hint.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd issue with program
    By TigerTank77 in forum C Programming
    Replies: 8
    Last Post: 05-12-2009, 12:00 AM
  2. Is it just me, or is the polynomial program popular?
    By CaptainMorgan in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-05-2006, 08:10 PM
  3. Newbie - cubic polynomial program - help!
    By jaffa in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 05:52 AM
  4. Horner's Algorithm
    By swanley007 in forum C++ Programming
    Replies: 7
    Last Post: 10-15-2005, 01:45 PM
  5. Cubic polynomial root solver program...
    By hayai32 in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 07:58 AM