1

I'm trying to recursively compute the fibonacci sequence to 100, store those returned values into an array using a the buildArray method, then print values stored in the array. I am getting a "cannot be resolved to a variable" compilation error when I try to print A[N] in the main method. I'm using longs because I'm computing the series up to 100, although I don't know if it's necessary to use longs.

If I substitute F(N) for A[N] the code works, but I need to put the values into an array and print that array. Does this code even store the values in an array? I'm just starting java, thanks.

public class MyFibonacci {
    public static final int MAX = 100;

    public static long[] buildArray(int MAX, int N) {
        long[] A = new long[MAX];

        A[0] = 0;
        A[1] = 1;

        for(N = 2; N < MAX; N++)
            A[N] = F(N);
        return A;
    }

    public static long F(int N) {
        if(N == 0)
            return 0;
        if(N == 1)
            return 1;
        return F(N - 1) + F(N - 2);
    }

    public static void main(String[] args) {
        for(int N = 0; N < MAX; N++)
            System.out.println(N + " " + A[N]);
    }
}
1
  • just a tip: 1. java fields should begin with a lowercase letter as long as they aren't final and 2. doesn't the fibonacci sequence start with 1? set A[0]=1 and if(N==0)return 1; :) Commented Apr 6, 2013 at 19:33

4 Answers 4

1

You have declared A[] within the scope of buildArray(int MAX, int N). As a result, A[] is not accessible outside of buildArray. You need to move your declaraction of long A[] to a class variable.

Additionally, you actually need to run buildArray for the array to be constructed.

For future reference, I highly recommend using proper tabbing structures. It makes it much easier to see what's happening. I've edited your code (though it will have to be approved) to include this.

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

1 Comment

Also they need to actually execute : "buildArray" in the main method
1

Here's the code for what you need, I think:

public class MyFibonacci{

    public static final int MAX = 100;
    long[] A = new long[MAX];
    public static long[] buildArray(int N){
    A[0] = 0;
    A[1] = 1;

    for (N = 2; N < MAX; N++){
        A[N] = F(N);
        }
    return A;
    }

    public static long F(int N)
    {
   if (N == 0) return 0;
   if (N == 1) return 1;
   return F(N-1) + F(N-2);
    }


    public static void main(String[] args)
    {
    buildArray(<some number - not sure where you get it from? N by the way in buildArray()>);
   for (int N = 0; N < MAX; N++)
      StdOut.println(N + " " + A[N]);
    }
 }

10 Comments

I've added long[] A = buildArray(); but the error message I'm getting is "Could not find or load main class." I figured buildArray() doesn't receive any passed values. Any ideas? Thanks so much for the help.
where did you put that precisely?
I put it in the same line you suggested in the code. Also, note that I declared int N in the buildArray method in the for loop, rather than pass in the value int N. I don't think that's my problem, though.
I see it's just that you said "long[] A = buildArray();"... is that what you put somewhere?
Yeah...so my main method is: public static void main(String[] args) { long[] A = buildArray(); for(int N = 0; N < MAX; N++) System.out.println(N + " " + A[N]);
|
0

The main problem is that you're never calling the buildArray function.

To get your code to work, you only need to add this to main:

long[] A = buildArray(MAX, 0);

Some other things:

You can remove the parameter N and just declare it in the function (or remove it all-together, see below).

You already have access to MAX, no need to pass it to the function.

The for-loop in buildArray is rather inefficient, you can set up the array inside F.

Given the below, A as a class variable is cleaner than passing it around.

Finally, the code:

static int MAX = 100;
static long[] A;
public static void buildArray()
{
  A = new long[MAX+1];
  F(MAX);
}

public static long F(int N)
{
  long val;
  if (N < 2)
    val = N;
  else if (A[N] != 0) // HEY! It's already calculated! Awesome! Just return it.
    return A[N];
  else
    val = F(N-1) + F(N-2);
  A[N] = val;
  return val;
}
public static void main(String[] args)
{
  buildArray();
  for (int N = 0; N <= MAX; N++)
    System.out.println(N + " " + A[N]);
}

3 Comments

Why is it OK to remove the for loop in buildArray? Don't you have to iterate through the range (0,MAX) placing in the values of F(N)? Thanks.
@MC9 Because in the process of calculating F(n), you also calculate F(n-1), F(n-2), F(n-3), ..., F(0) because of the recursive calls. So simply calculate F(MAX) and store these intermediate values as you go.
Thanks, this works awesome. I'm going to chip away at my original code to see if I can get it to work another way but this is great.
0

Since you can allocate array memory, it makes good sense to utilize it during calculation. Consider this method:

public static long[] f_a(int n) {
    long[] a = new long[n];
    a[1] = 1;
    for (int i = 2; i < n; i++)
        a[i] = a[i-1] + a[i-2];
    return a;
}

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.