Results 1 to 7 of 7
Thread: please help I am new
- 11-12-2010, 10:22 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 1
- Rep Power
- 0
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) ); } } }Thanks for your help!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; } }Last edited by soccermaniac88; 11-12-2010 at 10:35 PM.
- 11-13-2010, 12:47 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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, 12:56 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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, 01:17 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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, 03:17 AM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 11-13-2010, 08:45 AM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 11-13-2010, 12:02 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
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) ); } } }Happy coding...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; } }
ErikLast edited by venerik; 11-13-2010 at 12:04 PM. Reason: typo
I'm new to Java but I like to help where ever I can. :)


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks