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 9 of 9

Thread: Fibonacci Series

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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[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]);
    		}
     
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Fibonacci Series

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

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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fibonacci Series

    Would you be able to explain?

    --- Update ---

    Quote Originally Posted by helloworld922 View Post
    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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Fibonacci Series

    What printed out when you printed the value of numbers[x] ?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Fibonacci Series

    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  9. #9
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default 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.

Similar Threads

  1. Fibonacci sequence
    By ssohpkc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 8th, 2013, 09:29 PM
  2. java code for fibonacci series?
    By javawreker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 1st, 2012, 08:02 AM
  3. Fibonacci series
    By seanman in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 11th, 2011, 12:32 PM
  4. problem to get Fibonacci series- please help.
    By zoala001 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 3rd, 2011, 01:10 AM
  5. Replies: 1
    Last Post: October 19th, 2009, 11:53 PM

Tags for this Thread