How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the C programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the C programming language
Table of Contents
Problem Statement
A fast scheme for evaluating a polynomial such as: when is to arrange the computation as follows: And compute the result from the innermost brackets outwards as in this pseudocode: Task Description Cf. Formal power series
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the C programming language
The provided C code is an implementation of Horner's method to evaluate a polynomial. It takes an array of coefficients, the number of coefficients, and a value for the variable and returns the value of the polynomial.
Here's a detailed breakdown of the code:
-
horner
Function:- This function takes three parameters:
coeffs
: An array of doubles containing the coefficients of the polynomial, in decreasing order of their powers.s
: The number of coefficients in thecoeffs
array.x
: The value of the variable to evaluate the polynomial at.
- It initializes a variable
res
to 0.0, which will store the result of the polynomial evaluation. - The function enters a loop that iterates through the coefficients in reverse order (from the highest power to the constant term).
- Inside the loop, it updates the
res
variable using the Horner's method formula:res = res * x + coeffs[i]
. - Finally, the function returns the value of
res
, which is the result of evaluating the polynomial atx
.
- This function takes three parameters:
-
main
Function:- This is the entry point of the program.
- It defines an array
coeffs
of doubles, which contains the coefficients of the polynomial-19x³ + 7x² - 4x + 6
. - It calls the
horner
function with thecoeffs
array, its size (sizeof(coeffs)/sizeof(double)
), and the value3.0
forx
. - The result of the
horner
function is printed to the standard output with a precision of one decimal place usingprintf
.
When you execute this program, it will output -132.0
, which is the value of the polynomial -19x³ + 7x² - 4x + 6
when x
is 3.0.
Source code in the c programming language
#include <stdio.h>
double horner(double *coeffs, int s, double x)
{
int i;
double res = 0.0;
for(i=s-1; i >= 0; i--)
{
res = res * x + coeffs[i];
}
return res;
}
int main()
{
double coeffs[] = { -19.0, 7.0, -4.0, 6.0 };
printf("%5.1f\n", horner(coeffs, sizeof(coeffs)/sizeof(double), 3.0));
return 0;
}
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Ruby programming language
You may also check:How to resolve the algorithm Undefined values step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Even or odd step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Set puzzle step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Diversity prediction theorem step by step in the Jsish programming language