I'm not sure how I should ask questions about this program, so if this thread frustrates you do to the load of text, I'm sorry >.> Just tell me how to do it. I read the FAQ and it didn't really say anything...
Well, basically, I am in a basic Computer Science class, and we're working on just learning the basics of tokenizing/arrays/etc. I have completed every project with nearly no complications at all except for one. This one project is becoming extremely frustrating because I'm having a hard time comprehending it...
Here is the link to the assignment:
http://www.ecs.csun.edu/~cov/comp110.../project05.pdf
I thought I had it down correctly, but when I "finished" it, everything was completely wrong. Here is what I have so far:
PHP Code:
import java.util.*;
public class PolyAreaThree
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
System.out.print ("Degree of polynomial: ");
int n = scan.nextInt();
double w[] = new double[n+1];
while(n >= 0)
{
System.out.print("Enter coefficient " + n + ": ");
w[n] = scan.nextDouble();
n--;
}
System.out.print("Enter starting value: ");
double a = scan.nextDouble();
System.out.print ("Enter stopping value: ");
double b = scan.nextDouble();
System.out.print("Enter number of steps: ");
int nsteps = scan.nextInt();
double y[] = evalintegral(w);
double first = evalarea(y, a, b, nsteps);
//double second = evalarea(y, a, b, nsteps);
//System.out.println(y);
}
public static double evalpoly(double[] c, double x)
{
int n = c.length - 1;
double r = c[n];
int i = n;
while (i>0)
{
r = r*x + c[i-1];
i--;
}
return r;
}
public static double evalarea(double[] c, double a, double b, int nsteps)
{
double area = 0;
double x = a;
int steps = 0;
int sMult = 0;
double s = (b - a) / nsteps;
while(steps < nsteps)
{
double h = evalpoly(c, a);
area = h + (s * sMult);
//System.out.println("x = " + x + "; f(x) = " + h);
x = x + s;
steps++;
sMult++;
}
area = area * s;
return area;
}
public static double[] evalintegral(double[] c)
{
//create and return a new array of coefficients
int n = c.length - 1;
int m = n+1; // degree of g is m
double[] d = new double[m+1]; // coeffs for g(x) stored in d[]
int i= m;
while (i>0)
{
d[i] = (c[i-1] / i);
System.out.println(d[i]); // d[i] is c[i-1] / i
i--; // decrement i by 1
}
d[0] = 0; // constant term is 0
return d;
}
}
Any suggestions?