Write a recursive function for the following To evaluate a p

Write a recursive function for the following: To evaluate a polynomial of degree n (use a vector) at a value m using Horner\'s method.

Solution

The code is as follows.:

#include <iostream>

using namespace std;

// returns value of poly[0]x(n-1) + poly[1]x(n-2) + .. + poly[n-1]

int horner(int polynomial[], int n, int m)

{

    int result = polynomial[0]; // Initialize result

    // Evaluating horners method

    for (int i=1; i<n; i++)

        result = result*m + polynomial[i];

return result;

}

int main()

{

    // Let us evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3

    int polynomial[] = {2, -6, 2, -1};

    int x = 3;

    int n = sizeof(polynomial)/sizeof(polynomial[0]);

    cout << \"the value of polynomial is: \" << horner(polynomial, n, x);

    return 0;

}

#include <iostream>

using namespace std;

// returns value of poly[0]x(n-1) + poly[1]x(n-2) + .. + poly[n-1]

int horner(int polynomial[], int n, int m)

{

    int result = polynomial[0]; // Initialize result

    // Evaluating horners method

    for (int i=1; i<n; i++)

        result = result*m + polynomial[i];

return result;

}

int main()

{

    // Let us evaluate value of 2x3 - 6x2 + 2x - 1 for x = 3

    int polynomial[] = {2, -6, 2, -1};

    int x = 3;

    int n = sizeof(polynomial)/sizeof(polynomial[0]);

    cout << \"the value of polynomial is: \" << horner(polynomial, n, x);

    return 0;

}

 Write a recursive function for the following: To evaluate a polynomial of degree n (use a vector) at a value m using Horner\'s method.SolutionThe code is as fo
 Write a recursive function for the following: To evaluate a polynomial of degree n (use a vector) at a value m using Horner\'s method.SolutionThe code is as fo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site