# Fibonacci Series

• March 14th, 2013, 02:25 PM
13cmwest
Fibonacci Series
For an assignment I'm trying to create a dynamic array (I'm not allowed to use an array list) that has an equation write the Fibonacci series to the array and then print that array. The only problem is that it tends to write only numbers (completely unrelated to fibonacci) or a bunch of zeros then a 35 at the end. I'm just beginning in java and I could use some help correcting the code.

Code Java:

```  import java.util.Scanner;   public class Fibonacci {       public static void main(String[] args) {   int[] numbers; Scanner scan = new Scanner(System.in); System.out.println("How many numbers would you like to solve to in the Fibonacci series?");     int num;       num = scan.nextInt();   numbers = new int[2];     numbers[0]=0; numbers[1]=1; for(int x =2; x<num; x++){ numbers = new int[x+1]; //Increases array size numbers[x]=((x-2)+(x-1)); //places Fibonacci value in index }   for(int i = 0; i < numbers.length; i++){ System.out.println(numbers[i]); }     }   }```
• March 14th, 2013, 02:50 PM
Norm
Re: Fibonacci Series
Code :

`numbers = new int[x+1]; //Increases array size`
What is the above supposed to do? What happens to the previous contents of the array?
• March 14th, 2013, 04:43 PM
13cmwest
Re: Fibonacci Series
It increases the size of the array, but as it doesn't copy the contents that's why they are all zeros. Yet that doesn't explain why the last number is 35 when it shld b in the thousands.
• March 14th, 2013, 04:49 PM
helloworld922
Re: Fibonacci Series
Code java:

`numbers[x]=((x-2)+(x-1));`

Take a careful look at this line and you may find your second problem.
• March 14th, 2013, 04:51 PM
Norm
Re: Fibonacci Series
Add a println statement to print out the value of numbers[x] every time the loop goes around so you can see what the code is doing.
• March 14th, 2013, 06:25 PM
13cmwest
Re: Fibonacci Series
Would you be able to explain?

--- Update ---

Quote:

Originally Posted by helloworld922
Code java:

`numbers[x]=((x-2)+(x-1));`

Take a careful look at this line and you may find your second problem.

That is the equation for the Fibonacci series unless I'm mistaken. And since the equations are both in parentheses it shouldn't matter.
• March 14th, 2013, 06:52 PM
Norm
Re: Fibonacci Series
What printed out when you printed the value of numbers[x] ?
• March 15th, 2013, 04:23 AM
Tamilarasi
Re: Fibonacci Series
• March 15th, 2013, 04:39 AM
angstrem
Re: Fibonacci Series
I recommend you to use java.utils.Arrays.copyOf(originalArray, newLength). This will return you a copy of this array, but with newLength size.
The line
Code :

`numbers = new int[x + 1];`
is equivalent to
Code :

```numbers = null; numbers = new int[x + 1];```
that is, number starts to point to a completely new array, created by new int[x + 1]. All new integer arrays automatically initialize all their elements to 0.

Also,
Code :

`numbers[x]=((x-2)+(x-1));`
Is incorrect. x is an index of an element in an array. Hence, (x - 1), for example, is NOT a previous number, but it's index. Use numbers[i] to access a number at the index i.

P.S.: Increasing dynamic array by 1 element at a time is pretty inefficient, because every appending to an array requires duplicating of this array. Instead, I suggest you to double the array when you need extra space.