Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
958 views

I'm attempting to recursively compute the fibonacci sequence to 100, then store the resulting values in an array using the buildArray function, and then print the array contents.

 

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]);
    }
}

When I try to print A[N] in the main method, I receive a "cannot be resolved to a variable" compilation error. I'm following this article since it says I should use longs because I'm computing the series up to 100, but I'm not sure whether that's essential.

The code works if I replace F(N) for A[N], but I need to put the data into an array and output that array. Is it even possible for this code to save the data in an array? Thank you; I'm just getting started with Java.

in Algorithms and Problems by Expert (650 points) | 958 views

Please log in or register to answer this question.

Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .