0

I have a task where I should create fibonacci iterative using loop and array. How can I check negative values for fibonacciNumberInOrder? My tutor told me that I don't need allocate N elements in array. I can reduce it to 3element array. And I should point this array to last three numbers of fibonacci. Now I receive an error that: Index 3 out of bounds for length 3. I guess it's because I've change this:

long [] fibonacci = new long [fibonacciNumberInOrder];

to:

long [] fibonacci = new long [] {1,1,2};

What should I do to make it work and keep my tutor's requirements?

    public class FibonacciIterative {

    public static void main(String[] args) {
        int fibonacciNumberInOrder = 5;
        fibonacci(fibonacciNumberInOrder);
    }

    public static long[] fibonacci(int fibonacciNumberInOrder) {
        long [] fibonacci = new long [] {1,1,2};
        for (int i = 2; i < fibonacciNumberInOrder; i++) {
            fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
            System.out.print(Arrays.toString(fibonacci) + ", ");
        }
        return fibonacci;
    }
}
anabanana
  • 49
  • 8
  • What is the method expected to return? If it needs to return the full Fibonacci sequence, then an array of 3 is not enough, since you'd need an array of size `fibonacciNumberInOrder` *(weird, confusing parameter name)*. If it needs to return the n'th Fibonacci number, then you only need a 2-element array, and the return value should be `long`, not `long[]`. I find it hard to believe that the method is expected to return the last 3 values of the Fibonacci sequence. – Andreas May 27 '19 at 23:43
  • I want it to print "21, 34, 55" when I ask for 10, or if I ask for 5, then I want it: "2, 3, 5". Name of size is not my choice. It was already in that task. I was also told to reduce array to 3 elements. But how do I point it to the last 3? – anabanana May 28 '19 at 06:02

2 Answers2

2

The problem is that you're trying to store all the elements in the Array. However, since your Array is only of size three, you very quickly are trying to access an index that is out of bounds. Your tutor is correct that you only need three spots. You can shift the elements once you run out of space and overwrite the previous entries. (You only need the two previous numbers in the sequence, and then the third spot in the Array will be for the sum of the previous two)

So it'll go something like:

Iterations:

  1. [1, 1, 2]

  2. [1, 2, 3]

  3. [2, 3, 5]

  4. [3, 5, 8]

And so on. To do this, you can shift every element left one with a simple for loop:

public static long[] fibonacci(int fibonacciNumberInOrder) {
    long [] fibonacci = new long [] {1,1,2};
    for (int i = 0; i < fibonacciNumberInOrder; i++) {
        System.out.print(Arrays.toString(fibonacci) + ", ");
        for(int j = 1; j < 3; j++) {
            fibonacci[j-1] = fibonacci[j];
        }
        fibonacci[2] = fibonacci[1] + fibonacci[0];         
    }
    return fibonacci;
}

If you're trying to print the Array returned by the method, then you need to print the results of the method instead of ignoring the return value, and you need to remove the print statement in the method:

public static void main(String[] args) {
    int fibonacciNumberInOrder = 5;
    System.out.println(Arrays.toString(fibonacci(fibonacciNumberInOrder)));
}
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • Doesn't that depend heavily on what the method is *expected* to return? If the method is expected to return the [Fibonacci *sequence*](https://en.wikipedia.org/wiki/Fibonacci_number), then it starts at 1, so an array of only 3 values is not in any way part of the solution. If the method is expected to return the n'th number in the sequence, then the return type is wrong and you only need an **array of 2 values**. I find it hard to believe that the method is *expected* to return the last 3 values of the Fibonacci sequence of the given length. – Andreas May 27 '19 at 23:38
  • Now it prints: "[1, 1, 2], [1, 2, 3], [2, 3, 5], [3, 5, 8], [5, 8, 13]," How can I change that code to print "[2, 3, 5]"? – anabanana May 28 '19 at 06:27
  • @anabanana Why would it print `[2, 3, 5]`? Are you trying to print the entire sequence or just the last three numbers of the `n` numbers of the fibonacci sequence? – GBlodgett May 28 '19 at 15:00
  • 2
    @GBlodgett OP meant that if you pass in value 5, the result is `[2, 3, 5]` (see [comment](https://stackoverflow.com/questions/56333302/index-3-out-of-bounds-for-length-3-in-fibonacci-iterative-error-after-reducing-t?noredirect=1#comment99277595_56333302)), because OP's loop starts at 2, and because those are the last 3 numbers of a sequence of length 5. To print only that, you fix the loop, remove the print statement in the method, and then print the result in the caller (in `main` method). – Andreas May 28 '19 at 15:07
0

The exception occurred because you are trying to access an element with index that is out of bounds. In your scenario you are trying to access the third element of an array using an index value of 3 when it should be a value of 2 instead. Here is a short excerpt from Java documentation about arrays:

Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the preceding illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

Matthew
  • 1,905
  • 3
  • 19
  • 26