Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

programming csc 1350 https://moodle3.lsu.edu/pluginfile.php/903150/mod_resource/

ID: 3805123 • Letter: P

Question

programming csc 1350

https://moodle3.lsu.edu/pluginfile.php/903150/mod_resource/content/26/csc1350proj03s17.pdf

In this project, you will write a program to evaluate a polynomial using Horner’s method and without the use of the standard Java Math library Math.pow method. The naive way to evaluate a dense polynomial, one with relatively many non-zero coefficients, is to compute the power of each term and multiply it by the coefficient and then sum all the products. A much more efficient approach for evaluating a dense polynomial is Horner’s method.

Explanation / Answer

public class Horner {

public static double eval(double a, double[] p) {
double result = 0;
for (int i = p.length - 1; i >= 0; i--)
result = p[i] + (a * result);
return result;
}

public static void main(String[] args) {
int n = Integer.parseInt(args[0]);

double[] p = new double[n];
p[0] = 1;
for (int i = 1; i < n; i++) {
p[i] = p[i-1] / i;
}

while (!StdIn.isEmpty()) {
double a = StdIn.readDouble();
StdOut.println(eval(a, p));
StdOut.println(Math.exp(a));
StdOut.println();
}
}
}