• 11-12-2010, 11:22 PM
soccermaniac88
Hello everyone! I have just started working with java (my first programming language) and I am having some difficulties. I am trying to create a program that will generate the first 20 numbers of the fibonacci series but my code will not work. I have listed it below:
Code:

```public class Fib {     public static void main(String[] args)     {         DynamicArrayofInt numbers;         int a=0;         int ct = 1;         int i=1;         numbers = new DynamicArrayofInt();         while (true)         {             ct++;             if (ct >= 20)             break;             numbers.put(i, a);             a = numbers.get(i)+ numbers.get(a);             System.out.println(a);         }         System.out.println("\nFibonacci series:\n");         for (int b = ct - 1; b >= 0; b--)         {             System.out.println( numbers.get(ct) );         }     } }```
Code:

```public class DynamicArrayofInt {     private int[] data;     public DynamicArrayofInt()     {         data = new int[1];     }     public int get(int position)     {         if (position >= data.length)         return 0;         else         return data[position];     }     public void put(int position, int value)     {         if (position >= data.length)         {             int newSize = 2 * data.length;             if (position >= newSize)             newSize = 2 * position;             int[] newData = new int[newSize];             System.arraycopy(data, 0, newData, 0, data.length);             data = newData;             System.out.println("Size of dynamic array increased to" + newSize);         }         data[position] = value;     } }```
• 11-13-2010, 01:47 AM
al_Marshy_1981
I remember implementing a fibonachi sequence (spellling?) when I first started programming but have no idea what it is now so maybe giving an explanation of what a fibo sequence is will help you get more help here, I personally cannot be bothered researching....
• 11-13-2010, 01:56 AM
pbrockway2
Quote:

but my code will not work.

THis doesn't tell us anything. What runtime behaviour do you observe when you run this code? And what behaviour did you expect to see?

-----

If I were you I would forget the DynamicArrayofInt and just use a List: that's what they are meant for.
• 11-13-2010, 02:17 AM
al_Marshy_1981
yup exactly what pbrockway2 has said and myself...

1st. do describe what a fibonachi sequence is and
2nd please do be specific what part of your program is not working

Your help will be much faster coming if you do so...
• 11-13-2010, 04:17 AM
Singing Boyo
I think marshy is a bit annoyed today :p

Anyway, fibonacci sequence is {1,1,2,3,5,8,13,21,34,55,89,<end excessive mental math>}. Basically, the next number is the sum of the previous two numbers, always starting with 1.

@OP:start by incrementing/decrementing your variables. You'll probably also want to switch to a List or even a fixed-size array with a size of 20, instead of your 'DynamicArrayOfInt' class. Besides this, we need you to offer more about what you expect, and what you get, which is what pbrockway said. We're here because we're nice people, not because it pays well, so we try to refrain from tiring our brains figuring out someone else's code.
• 11-13-2010, 09:45 AM
m00nchile
Also, have you learned about recursion yet? Defining the fibo sequence recursively is a very simple task.
• 11-13-2010, 01:02 PM
venerik
Since you are completely new to programming here are some improvements for your code. They do not solve your Fibonacci problem but make you a better coder in the long run. The orange parts is your original code, the green parts are my improvements.

Code:

```public class Fib {     public static void main(String[] args)     {         [COLOR="Orange"]// DynamicArrayofInt numbers;[/COLOR]         int a=0;         int ct = 1;         int i=1;         [COLOR="Orange"]//numbers = new DynamicArrayofInt();[/COLOR]         [COLOR="YellowGreen"]DynamicArrayofInt numbers = new DynamicArrayofInt();[/COLOR] [COLOR="Orange"]         /* this is basically a for loop...         while (true)         {             ct++;             if (ct >= 20)             break;         */ [/COLOR] [COLOR="YellowGreen"]         // like thise         for (ct = 1; ct < 20; ct++)         { [/COLOR]             numbers.put(i, a);             a = numbers.get(i)+ numbers.get(a);             System.out.println(a);         }         System.out.println("\nFibonacci series:\n");         for (int b = ct - 1; b >= 0; b--)         {             System.out.println( numbers.get(ct) );         }     } }```
Code:

```public class DynamicArrayofInt {     private int[] data;     public DynamicArrayofInt()     {         data = new int[1];     }     public int get(int position)     { [COLOR="Orange"]         /*         if (position >= data.length)         return 0;         else         return data[position];         */ [/COLOR] [COLOR="YellowGreen"]         // make yourself accustomed to always use braces in if statements.         // It WILL bite you eventually if you don't.         if (position >= data.length) {                 return 0;         } else {                 return data[position];         } [/COLOR]     }     public void put(int position, int value)     {         if (position >= data.length)         {             int newSize = 2 * data.length; [COLOR="Orange"]             /*             if (position >= newSize)             newSize = 2 * position;             */ [/COLOR] [COLOR="YellowGreen"]             // same here             if (position >= newSize) {                         newSize = 2 * position;             } [/COLOR]             int[] newData = new int[newSize];             System.arraycopy(data, 0, newData, 0, data.length);             data = newData;             System.out.println("Size of dynamic array increased to" + newSize);         }         data[position] = value;     } }```
Happy coding...
Erik