so i wrote this recursion program that uses memoization to calculate the fibonacci numbers. it's supposed to scan a user inputted number, call the method, check to see if the number is in the array, if not store it, ad increment the counter. so i should end up with the value plus the number of calls. i hope i'm on the right track.... thanks

public class memofib
{
    static int[] list;
    static int count= 0;
    public static void main(String[] args)
    {

        int fib;
        int n;
        Scanner scan = new Scanner(System.in);
        System.out.println("input a number");
        n = scan.nextInt();
        System.out.println("result: " + calcFib(n) + " calls: " + count);
    }
    public static int calcFib (int n)
    {
        list = new int[100];

        count++;
        if(n<2)
            {
                int ans = n;
                return ans;
            }

        else
            {
                int ans = calcFib(n-1) + calcFib(n-2);
                if(list.contains (ans))
                    {
                        return ans;
                    }

                else
                    {
                        list[ans] = ans;
                        return ans;
                    }
 if(n<2)
            {
                int ans = n;
                return ans;
            }

        else
            {
                int ans = calcFib(n-1) + calcFib(n-2);
                if(list.contains (ans))
                    {
                        return ans;
                    }

                else
                    {
                        list[ans] = ans;
                        return ans;
                    }
            }
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

nevermind the first post, i had things out of order... i thinki put them i nthe correct order but i am still getting a compile error that says missing return statement. can anyone please help me out???

import java.util.Scanner;
public class memofib
{
    static int[] list = new int[100];
    static int count= 0;
    public static void main(String[] args)
    {

        int fib;
        int n;
        Scanner scan = new Scanner(System.in);
        System.out.println("input a number");
        n = scan.nextInt();
        System.out.println("result: " + calcFib(n) + " calls: " + count);
    }
    public static int calcFib (int n)
    {

        count++;
        if(n<2)
            {
            int ans= n;
            return ans;
            }

        else
            {
            if(n>2)
                {
                int ans= n;
                if(list[ans]== ans)
                return list[ans];
                 }


            else
                    {
                        int ans = calcFib(n-1) + calcFib(n-2);
                        list[ans] = ans;
                        return ans;
                    }
            }
 }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I believe that you should write a return statement at the end of your "public static int calcFib (int n)" method. From what I have seen you have return statements in all of your if () { }, but I think that in some versions of the java you need to have a return outside of the if. Try this and If I was wrong, send the line that the error happened. And try to write using code tags.

This article has been dead for over six months. Start a new discussion instead.