3

I'm trying to solve a homework problem for an algorithms class and I keep getting an out of bounds index array for the code I've written below. I've been trying to play around with it in Python since I'm pretty comfortable with that, but I seem to be getting a similar exception. Can anyone give me a hint as to where I'm going wrong with this?

public class Fibonacci1 {
    public static long F(int N) {
        long a[]  = new long [100];
        a[0] = 0; /*sets up first 2 digits in the sequence*/
        a[1] = 1;
        if (N<2) {   
            return N;
        }
        a[N] = a[N-1] + a[N-2]; /*appends F num for next number in the list*/
        N++; 
        return a[N]; /*should return the last number*/
    }
    public static void main(String[] args) {
        for (int N = 0; N<100; N++)
            StdOut.println(N+" " + F(N));
    }
}
5
  • blog.paulvargas.org/numeros-fibonacci Commented Jan 15, 2013 at 16:19
  • 3
    and the algorithm is wrong. you'd better google and find a correct one Commented Jan 15, 2013 at 16:21
  • 1
    @gefei, I was going to point out the same, but you were faster! Commented Jan 15, 2013 at 16:22
  • One of the many resources: Fibonacci numbers (Java) Commented Jan 15, 2013 at 16:31
  • You don't need an array. You can calculate it iteratively. Commented Jan 15, 2013 at 16:56

3 Answers 3

10

when N == 99, you do an N++ in method F, then you call return a[N], which means return a[100]

Sign up to request clarification or add additional context in comments.

1 Comment

To make this clearer: An array that can hold 100 elements has indexes 0 to 99, so index 100 is out of bounds.
5

The code needs to changed a bit. Your not getting the right sequence printed out because the array is a local variable and should be a static variable. Also the n++ should be removed. The code below is not pretty but it works.

public class Fibonacci1 {
    static long a[] = new long[100];

    public static long F(int N) {
        a[0] = 0; /* sets up first 2 digits in the sequence */
        a[1] = 1;
        if (N < 2) {
            return N;
        }
        a[N] = a[N - 1] + a[N - 2]; /* appends F num for next number in the list */
        return a[N]; /* should return the last number */
    }

    public static void main(String[] args) {
        for (int N = 0; N < 100; N++)
            System.out.println(N + " " + F(N));
        }
}

2 Comments

Yep, that did it. Sorry to bother the SO community with a Fib question, I really tried to spend some time on this before I asked anyone. I know programmers hate rudimentary questions, especially Fib!
No problem. We have all been there before staring at our code. Good luck.
0

Remove the N++ statement from the F-Function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.