Results 1 to 7 of 7
  1. #1
    MylesPollie is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Fibonacci sequence using Dynamic Arrays

    public class Fibonacci
    {
    public static void main(String[] args)
    {
    int[] numbers;
    numbers = new int[20];
    numbers[0] = 0;
    numbers[1] = 1;
    System.out.println("\nFibonacci series:\n");
    System.out.println(numbers[0]);
    for (int i=2;i<20;i++)
    {
    numbers[i]=numbers[i-2]+numbers[i-1];
    System.out.println(numbers[i]);
    }
    }
    }


    I wrote this program for my Java class to print out the first 20 numbers of the Fibonacci series. My assignment then tells me to Rewrite your program using dynamic array. I'm not sure how to do this. I would really appreciate some help. Thanks!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Fibonacci sequence using Dynamic Arrays

    Do you know what a dynamic array is? If not, there's your starting point! If so, what specifically about this is giving you trouble?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    MylesPollie is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Fibonacci sequence using Dynamic Arrays

    I understand that a dynamic array allows you to alter the size of the array during the execution of the program, but I don't understand how to apply it in the program.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Fibonacci sequence using Dynamic Arrays

    The question is, what happens if you want to go beyond the 20th number in the series? With your existing code you would be stuck because your array only has 20 indexes. The answer to that problem is to use a dynamic array that grows when you reach its capacity. What have you tried?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,050
    Rep Power
    6

    Default Re: Fibonacci sequence using Dynamic Arrays

    Quote Originally Posted by MylesPollie View Post
    I understand that a dynamic array allows you to alter the size of the array during the execution of the program, but I don't understand how to apply it in the program.
    There is no such thing as a dynamic array; an array is static sized in Java. You have two basic options;

    1. create a new array that is larger than the first and copy the original array to the new array
    2. use a class which can do this for you

    The assignment is very vague, but I think you have to do #1. You will have to confirm that with the person who gave you this assignment.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Fibonacci sequence using Dynamic Arrays

    Right, I should have clarified that when I said that you'd have to use a dynamic array, what I meant was you'd have to create a data structure that *acts* like a dynamically sized array.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,050
    Rep Power
    6

    Default Re: Fibonacci sequence using Dynamic Arrays

    And now the OP needs to ask what he really needs to do, its simply not clear which option is the correct one.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. need some help doing loops with fibonacci sequence
    By barqcider in forum New To Java
    Replies: 6
    Last Post: 09-25-2013, 11:03 PM
  2. Replies: 1
    Last Post: 01-23-2013, 06:06 PM
  3. Fibonacci Sequence Problem
    By Zigster in forum New To Java
    Replies: 10
    Last Post: 07-06-2012, 09:54 PM
  4. Replies: 8
    Last Post: 03-10-2011, 03:16 AM
  5. Fibonacci sequence
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 6
    Last Post: 03-25-2010, 06:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •