Given the following function that evaluates a polynomial whose coefficients are
ID: 3875547 • 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. 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.
Explanation / Answer
Answer:
When the evaluate() function is executed once there will be two multiplications, and one addition performed.
For a given length of the array 5 (i.e n=5), 10 multiplication operations, and 5 addition operations can be done in worst case.
For any n value (In worst case), In each iteration 2 multiplications and 1 addition can be performed, In n iterations 2n multiplications, and n additions can be performed.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.