1

I wrote this program:

public class FunctionEvaluator {
    public static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        int degree;
        System.out.print("What degree would you like your polynomial to be? ");
        degree = console.nextInt();
        int a[] = new int[degree + 1];
        int coefficient;

        for (int i = 0; i <= degree; i++) {
            System.out.print("Coefficient of the x^" + (degree - i) + " term: ");
            coefficient = console.nextInt();

            a[i] = coefficient;
        }

        System.out.print("f(x) = ");

        for (int i = 0; i < degree + 1; i++) {
            System.out.print(a[i] + "x^" + (degree - i));

            if (a[i] == degree) {
                System.out.println(" ");
            } else if (a[i + 1] >= 0 && a[i + 1] < degree) {
                System.out.print(" + ");
            } else if (a[i] < 0) {
                    System.out.print(" - ");
            } else {
                    System.out.print(" ");
            }
        }

        System.out.println();

        int x;
        int yN = 0;
        double fOfX = 0;
        double sum1;

        do {
            System.out.print("Give a value for x: ");
            x = console.nextInt();
            int deg = degree;
            for (int i = 0; i <= degree; i++) {
                sum1 = a[i] * Math.pow(x, deg);
                deg--;
                fOfX = fOfX + sum1;
            }

            System.out.println("f(" + x + ") = " + fOfX);

            System.out.print("Do you want to go again (1 for yes and 0 for no)? ");
            yN = console.nextInt();
        } while (yN == 1);

        System.out.println("Done.");

    }

And there's a problem with this code:

System.out.print("f(x) = ");

        for (int i = 0; i < degree + 1; i++) {
            System.out.print(a[i] + "x^" + (degree - i));

            if (a[i] == degree) {
                System.out.println(" ");
            } else if (a[i + 1] >= 0 && a[i + 1] < degree) {
                System.out.print(" + ");
            } else if (a[i] < 0) {
                    System.out.print(" - ");
            } else {
                    System.out.print(" ");
            }
        }

The main code is supposed to ask the user for a degree of polynomial and the coefficients and then do some math. If I comment out the above segment of code, the program works fine. However, when I leave the above code in (it's supposed to print out the function), the program crashes. I suspect it has something to do with the limits on the for loop but no matter what I change or modify, the program still crashes. Could anybody tell me what's wrong and why the program won't run? IntelliJ is telling me that the problem is on the first else if line or the nested if statement in the for loop if that helps.

ch1maera
  • 1,369
  • 5
  • 20
  • 42
  • What is the Exception with which it crashes? – ostrichofevil Feb 09 '16 at 22:22
  • ArrayIndexOutOfBoundsException i'd guess. a[i] is the highest you can go. – GoGoCarl Feb 09 '16 at 22:23
  • `the program crashes` -> stacktrace, and look at it while you're at it. it will tell you what's wrong – njzk2 Feb 09 '16 at 22:26
  • @Gavriel it's on the first else if statement in the second piece of code – ch1maera Feb 09 '16 at 22:26
  • @ostrichofevil Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 – ch1maera Feb 09 '16 at 22:27
  • I see the problem in the a[i+1] because let us say that the boundary "degree+1" equals 5, then in the last iteration "i" would equal to 4, at that iteration you are writing in the code a[i+1] which is a[5], but actually your last index of an array of 5 elements is 4 not 5, thus you would have the ArrayIndexOutOfBoundsException . – AMH9 Feb 09 '16 at 22:28
  • They already answered while i am writing the comment so I hope it is clear for you now :) – AMH9 Feb 09 '16 at 22:29
  • why you are doing the condition based on the value of a[i] ? values of a[i] has been entered by user, it is not suitable to use them to track in which order you are. – AMH9 Feb 09 '16 at 22:37

2 Answers2

2

You're indexing a[i+1], but a is int[degree+1], so at the end of the loop you are trying to reach a[degree+1], and there's no such item, the last one is a[degree]

Probably you need:

} else if (i < degree && a[i + 1] >= 0 && a[i + 1] < degree) {

BTW you have another illogical parts in your code. For example:

if (a[i] == degree) {

You compare a[i] with degree, but it has nothing to do with the degree. You probably want to compare i == degree. See this example:

degree = 2
a[0] = 7, a[1] = 2, a[2] = 3 // 7 * x^2 + 2 * x + 3

As you see you should compare degree with the index, and not with the value of the array item.

I would suggest you to rewrite the code with the following tip in mind: try using the indexes in the array not the "other-way-around". It'll be much more natural, and each index will be exacty the exponent:

a[2] = 7, a[1] = 2, a[0] = 3 // note: 3 * x^0 = 3 * 1 = 3

Since you anyway fill in all the elements in the array, it doesn't matter if you loop on it in a decreasing order.

Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • That's what I figured but I can't find another way to print out the function because I ordered the coefficients backwards in the array i.e. if it's a quartic function, the coefficient of x^4 is in spot 0, x^3 in spot 1 and so on. – ch1maera Feb 09 '16 at 22:29
  • 1
    Then just add i < degree && at the BEGINNING of the condition – Gavriel Feb 09 '16 at 22:33
0
else if (a[i + 1] >= 0 && a[i + 1] < degree) 

This seems to be your issue. You're going to the size of the array + 1. My guess would be your error is ArrayIndexOutOfBoundsException

Eric
  • 2,008
  • 3
  • 18
  • 31
  • Yeah, the error is ArrayIndexOutOfBoundsException: 5 – ch1maera Feb 09 '16 at 22:25
  • You're going to have to rethink the way you do that for loop. Whenever you use + 1 or - 1 in an array, you have to take extra precaution to be sure that you do not have an ArrayIndexOutOfBoundsException. – Eric Feb 09 '16 at 22:29
  • Ok, thank you. I'll try to see if I can rewrite it to avoid the error. – ch1maera Feb 09 '16 at 22:31