Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default please help I am new

    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:
    Java 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) );
            }
        }
    }
    Java 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;
        }
    }
    Thanks for your help!
    Last edited by soccermaniac88; 11-12-2010 at 11:35 PM.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

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

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

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

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Also, have you learned about recursion yet? Defining the fibo sequence recursively is a very simple task.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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.

    Java 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) );
            }
        }
    }
    Java 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
    Last edited by venerik; 11-13-2010 at 01:04 PM. Reason: typo
    I'm new to Java but I like to help where ever I can. :)

Posting Permissions

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