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.

# Thread: Fibonacci Series

1. ## 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.

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

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

}

}```  Reply With Quote

3. ## Re: Fibonacci Series

`numbers = new int[x+1]; //Increases array size`
What is the above supposed to do? What happens to the previous contents of the array?  Reply With Quote

4. ## 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.  Reply With Quote

5. ## Re: Fibonacci Series

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

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

6. ## 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.  Reply With Quote

7. ## Re: Fibonacci Series

Would you be able to explain?

--- Update --- Originally Posted by helloworld922 `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.  Reply With Quote

8. ## Re: Fibonacci Series

What printed out when you printed the value of numbers[x] ?  Reply With Quote

9. ## Re: Fibonacci Series  Reply With Quote

10. ## 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
`numbers = new int[x + 1];`
is equivalent to
```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,
`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.  Reply With Quote

#### Tags for this Thread 