5 Replies - 829 Views - Last Post: 14 April 2011 - 05:10 AM Rate Topic: -----

#1 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,499
  • Joined: 16-March 11

Problem with my dynamic array

Posted 13 April 2011 - 07:49 PM

I'm quite the noob with Java, and I've also been given the task of simulating the fibonacci series with a dynamic array. I've got my array resizing properly, but for some odd reason when I use a get method that I created it's returning -3 repeatedly.

DynamicArray Class:
public class DynamicArray
{
	private int[] fib;

    public DynamicArray()
    {
    	fib = new int[1];
    }

    public int get(int position)
    {
    	if (position >= fib.length)
    		return 0;
    	else
    		return fib[position];
    }

    public void put(int position, int value)
    {
    	if (position >= fib.length)
    	{
    		int newSize = 4 * fib.length;
    		if(position >= newSize)
    			newSize = 4 * position;
    			int[] newData = new int[newSize];
    			System.arraycopy(fib, 0, newData, 0, fib.length);
    			fib = newData;

    			System.out.println("Dynamic aray size changed to " + newSize);
    	}
    	fib[position] = value;

    }
    }



Fibonacci Series Class:
public class FibonacciSeries
{

    public static void main(String[] args)
    {

        DynamicArray newDyn = new DynamicArray();
        newDyn.put(0, 0);
        newDyn.put(1, 1);

        for(int i = 2; i <= 5; i++)
        {
        	int a = newDyn.get(i);
        	int a1 = a - 1;
        	int b = newDyn.get(i);
        	int b1 = b - 2;
        	int c = a1 + b1;
			newDyn.put(i, c);
			int d = newDyn.get(i);
			System.out.println(d);
        }
    }
}



Figured maybe a fresh pair of eyes could spot the problem. For now I'm going to sleep on it, and see what I come up with in the morning. Any help is appreciated thanks. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with my dynamic array

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with my dynamic array

Posted 13 April 2011 - 08:01 PM

I you indent your code correctly it seems OK
I would use position * 2 thow

    public void put(int position, int value)
    {
    	if (position >= fib.length)
    	{
    		int newSize = 4 * fib.length;
    		if(position >= newSize)
    			newSize = 2 * position;
    		int[] newData = new int[newSize];
    		System.arraycopy(fib, 0, newData, 0, fib.length);
    		fib = newData;
    		System.out.println("Dynamic aray size changed to " + newSize);
    	}
    	fib[position] = value;
    }




The problem is with your main method the first
int a = newDyn.get(i); will return 0
you need a put(2,2); before starting
Was This Post Helpful? 0
  • +
  • -

#3 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,499
  • Joined: 16-March 11

Re: Problem with my dynamic array

Posted 13 April 2011 - 08:36 PM

I tried exactly what you said, and despite the changes, I'm still getting a repeated -3 whilst my array changes sizes. When I read the code I can't seem to put it together in my mind how that would be. I tried a put(2, 2) method before it started, and I'm still receiving the same error. It just doesn't make sense to me where and how this went wrong.

   DynamicArray newDyn = new DynamicArray();
        newDyn.put(0, 0);
        newDyn.put(1, 1);
        newDyn.put(2, 2);

        for(int i = 2; i <= 15; i++)
        {
        	int a = newDyn.get(i);
        	int a1 = a - 1;
        	int b = newDyn.get(i);
        	int b1 = b - 2;
        	int c = a1 + b1;
			newDyn.put(i, c);
			int d = newDyn.get(i);
			System.out.println(d);


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem with my dynamic array

Posted 13 April 2011 - 08:42 PM

first you should get(i) and get(i-1)
and put the sum to i+1

That should do the trick
for(int i = 2; i < 15; i++)
   newDyn.put(i+1, newDyn.get(i-1) + newDyn.get(i));



Happy coding :^:
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10396
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: Problem with my dynamic array

Posted 13 April 2011 - 08:45 PM

Your problem is that you are subtracting 1 and 2 from the elements you retrieve, not their indices. You want to add dynArr.get(i-1) + dynArr.get(i-2), not dynArr.get(i)-1 + dynArr.get(i)-2.

This post has been edited by macosxnerd101: 13 April 2011 - 08:46 PM
Reason for edit:: Ninja'd

Was This Post Helpful? 1
  • +
  • -

#6 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,499
  • Joined: 16-March 11

Re: Problem with my dynamic array

Posted 14 April 2011 - 05:10 AM

Much appreciated gentlemen, I gave it my best shot, got an output similar to the fibonacci series, tired of fiddling with it for now. Thanks for all the help though! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1