Horner's Algorithm

This is a discussion on Horner's Algorithm within the C++ Programming forums, part of the General Programming Boards category; Hello, I am a college student, and I got a question wrong on the last test. I was wondering if ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    Horner's Algorithm

    Hello,

    I am a college student, and I got a question wrong on the last test. I was wondering if anyone could give me the answer, as I am stumped and frustrated.

    HERE IT IS:

    Horner's Algo can be used to evaluate a polynomial at a point O(n) time.

    SEE ATTACHMENT (COULDN"T GET FORMATTING IN THIS WINDOW)
    MANY SUPER AND SUBSCRPITS
    Attached Files Attached Files

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    (COULDN"T GET FORMATTING IN THIS WINDOW)
    That's what [code] [/code] tags are for.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    double routineC(double coeffs[], int size, double x) {
          int I;  // intended to be i?
          double y = 0;
          for(i=0;i<=size;i++){  // i is undeclared
    	y += coeffs[i] * pow(x,i);
         }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    might want to return y after the for loop too.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    And come to think of it
    Code:
    for(i=0;i<=size;i++){
    you probably want < there, since you're using it to access the array.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    Tru

    You are correct with your changes, sorry I guess I typed too fast...But it still leaves the rest of the problem.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    The rest of the problem? You mean the code that you haven't written yet?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    That's not Horner's algorithm. This is.

    Code:
    double horners(double * poly, int size, double x) {
    	double ret = 0.0;
    
    	double * end = poly + size;
    
    	while (poly != end) {
    		ret *= x;
    		ret += *poly;
    		++poly;
    	}
    
    	return ret;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21