# need help

• February 14th, 2014, 08:11 AM
need help

Code :

```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)); }```
• February 14th, 2014, 08:14 AM
GregBrannon
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.
• February 14th, 2014, 08:32 AM
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.
• February 14th, 2014, 08:56 AM
GregBrannon
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?
• February 14th, 2014, 10:09 AM
Re: need help
The original code is:

Code :

```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:

Code :

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