Given the following function that evaluates a polynomial whose coefficients are
ID: 3874723 • Letter: G
Question
Given the following function that evaluates a polynomial whose coefficients are stored in an array:
double evaluate(double[] coefficients, double x) {
double result = coefficients[0];
double power = 1; for (int i = 1; i < coefficients.length; i++)
{ power = power * x; result = result + coefficients[i] * power; }
return result; }
Let n be the length of the array. Determine the number of additions and multiplications that are performed in the worst case as a function of n. Many thanks for the help and for clear responses as I have so much to learn!
Explanation / Answer
To determine number of addition in your polnomial equation.
double evaluate(double[] coefficients, double x) {
double result = coefficients[0];
double power = 1; for (int i = 1; i < coefficients.length; i++)
{ power = power * x; result = result + coefficients[i] * power; }
return result; }
addition will takes place exaclty to the n times (n be the length of the array)
Multiplication 2n times executed
proof:
result = result + coefficients[i] * power; \this condition accepts if the coefficient[i] = 0;so the flow starts from 0+1......n
then
result = result +0*power;
result = result
then the flow will be
=[2n+2n+................+(k-times)+2n] / k
= 2n*k/k
= 2n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.