Results 1 to 7 of 7
 02122014, 09:01 PM #1Member
 Join Date
 Feb 2014
 Posts
 6
 Rep Power
 0
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[i2]+numbers[i1];
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!
 02122014, 09:03 PM #2
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  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 02122014, 09:06 PM #3Member
 Join Date
 Feb 2014
 Posts
 6
 Rep Power
 0
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.
 02172014, 03:17 PM #4
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  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 02172014, 03:45 PM #5Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 5,104
 Rep Power
 8
Re: Fibonacci sequence using Dynamic Arrays
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
 02172014, 05:05 PM #6
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  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!
 02172014, 05:26 PM #7Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 5,104
 Rep Power
 8
Similar Threads

need some help doing loops with fibonacci sequence
By barqcider in forum New To JavaReplies: 6Last Post: 09252013, 11:03 PM 
Fibonacci sequence program that only prints the n'th digit in the sequence
By erichfx in forum New To JavaReplies: 1Last Post: 01232013, 07:06 PM 
Fibonacci Sequence Problem
By Zigster in forum New To JavaReplies: 10Last Post: 07062012, 09:54 PM 
Using forloop and arrays to calculate factorials and fibonacci sequence
By baumboards in forum New To JavaReplies: 8Last Post: 03102011, 04:16 AM 
Fibonacci sequence
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 6Last Post: 03252010, 07:59 AM
Bookmarks