Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: need help

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help

    Hi, I need to change the program below to an "array" please help.

    public class RecursiveFibonacci {
     
    	public int fibonacci(int n) {
    		int result; 
    		if (n == 1 || n == 2) 
    			result = 1;
    		else {
    			int n1 = fibonacci(n-1);
    			int n2 = fibonacci(n-2);
    			result = n1 + n2;
    			//System.out.println("\tfibonacci(" + n + ") = " + result + " = " + n1 + " + " + n2);
    		}
     
    		return result;
    	}
     
    	public static void main(String[] args) {
     
           	RecursiveFibonacci rf = new RecursiveFibonacci();
    	System.out.println(rf.fibonacci(8));
    	}

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,781
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: need help

    Welcome to the Forum! Thank you for taking the time to learn how to post code correctly.

    What will the array do in your program? Please try to add an array and let us know when you need help by posting your errors or asking specific questions.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help

    int[] intArray = new int[8];
    intArray[0] = 0;
    intArray[1] = 1;

    System.out.println("fibonacci["+(0)+"] = "+intArray[0]);
    System.out.println("fibonacci["+(1)+"] = "+intArray[1]);

    for (int count=2;count<intArray.length;count++){
    intArray[count] = intArray[(count-1)]+intArray[(count-2)];
    System.out.println("fibonacci["+(count)+"] = "+intArray[count]);
    }
    }

    The code above i get no errors however i want to know if is the right way to do it??

    the instuctions are as follow:

    the recursive fibonacci method ends up calling itself twice with the same argument. For example,

    fibonacci(4) = fibonacci(3) + fibonacci(2)

    and

    fibonacci(3) = fibonacci(2) + fibonacci(1).

    This is inefficient, since the Fibonacci number is calculated twice. Fix this inefficiency by creating & initializing an array, and updating the fibonacci method so that it first checks to see if the value is stored in this array before recurring. If the value is there, it simply displays the appropriate text. If the value is not in the array yet, then fibonacci is called & stores the value in the array.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,781
    My Mood
    Mellow
    Thanks
    236
    Thanked 717 Times in 703 Posts

    Default Re: need help

    So much for posting code correctly.

    Per the instructions (in bold above), I would expect the fibonacci() method to check for the existence of values in an array, and I don't see that. In fact, I don't see an array. Is it off camera somewhere?

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need help

    The original code is:

    public class RecursiveFibonacci {
     
    	public int fibonacci(int n) {
    		int result; 
    		if (n == 1 || n == 2) 
    			result = 1;
    		else {
    			int n1 = fibonacci(n-1);
    			int n2 = fibonacci(n-2);
    			result = n1 + n2;
    			//System.out.println("\tfibonacci(" + n + ") = " + result + " = " + n1 + " + " + n2);
    		}
     
    		return result;
    	}
     
    	public static void main(String[] args) {
     
           	RecursiveFibonacci rf = new RecursiveFibonacci();
    	System.out.println(rf.fibonacci(8));
    	}

    I had to change it so it can be an array so it wont calculated twice. so the modified program is:


    public class RecursiveFibonacci {
     
    	public int fibonacci(int n) {
    		int result; 
    		if (n == 1 || n == 2) 
    			result = 1;
    		else {
    			int n1 = fibonacci(n-1);
    			int n2 = fibonacci(n-2);
    			result = n1 + n2;
    			//System.out.println("\tfibonacci(" + n + ") = " + result + " = " + n1 + " + " + n2);
    		}
     
    		return result;
    	}
     
    	public static void main(String[] args) {
     
            int[] intArray = new int[8];
    intArray[0] = 0;
    intArray[1] = 1;
     
    System.out.println("fibonacci["+(0)+"] = "+intArray[0]);
    System.out.println("fibonacci["+(1)+"] = "+intArray[1]);
     
    for (int count=2;count<intArray.length;count++){
    intArray[count] = intArray[(count-1)]+intArray[(count-2)];
    System.out.println("fibonacci["+(count)+"] = "+intArray[count]);
    }
    }
     
           	//RecursiveFibonacci rf = new RecursiveFibonacci();
    	//System.out.println(rf.fibonacci(8));
    	}