Results 1 to 3 of 3
Thread: Stack Overflow work around?
- 03-14-2010, 07:55 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
Stack Overflow work around?
Hey,
Recently my CS teacher (highschool G12 Computer Science) has given out an assignment with a few subparts to it, one of them being to create a fibonacii sequence application. The assignment is worth a good percentage of our mark so I'd like to do well on it.
We are not allowed to use imports and must use the basic java functions to write all our apps.
My current application:
I'm pretty satisfied with it and its pretty efficient, but it has some obvious limmitations.Java Code:public class Part2 { public static void main(String[] args) throws Exception { long fib1 = 0, fib2 = 1; int n1, n2, n; System.out.println("Enter number of fibonacii values to compute: "); n1 = System.in.read(); n2 = System.in.read(); System.in.skip(2); n1 -= 48; n2 -= 48; n = n1 * 10 + n2; for (int i = 0; i < n; i++) { long temp = fib1; fib1 = fib2; fib2 += temp; System.out.print(fib1 + ", "); } } }
1. the nth number entered has to be a 2 digit number or a single digit with a 0 in front of it.
2. Because of the size limmit of the long datatype it cannot compute after around the 93rd sequence number as it stack overflows.
Are they're any ways that I can get around these two problems without using imports? I was thinking of going into the java library and putting the code they use for inputting data directly into my code, but im not sure how to access the library to view the source code.
any help would be appriciated,
~Coukapecker~
- 03-14-2010, 08:08 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,371
- Blog Entries
- 7
- Rep Power
- 17
I don't see how a stack overflow error can occur; I understand that you can have int overflows because those fibonacci numbers can get huge. btw, please get rid of that 48, it's a magic number, use the symbolic '0' instead, the compiler knows how to deal with it and it'll translate it for you. Also what is that 'skip()' doing there?
kind regards,
JosLast edited by JosAH; 03-14-2010 at 08:26 PM.
- 03-14-2010, 08:49 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 2
- Rep Power
- 0
My mistake, int overflow. (the part where it starts giving me the wrong answers in the sequence, as in negative numbers?, how can i fix this?)
The 48 is how we were taught to do it; how do you use the symbolic '0' instead?
The skip(2) is so it doesn't read the "ENTER" button pressed when the user inputs as part of the variable.
Similar Threads
-
heap overflow
By is0dvil in forum Advanced JavaReplies: 1Last Post: 11-17-2009, 10:43 PM -
Java Stack Overflow?
By fullmetaljacket in forum New To JavaReplies: 0Last Post: 05-19-2009, 07:49 PM -
[SOLVED] overflow when calculate factorial
By ravinda in forum New To JavaReplies: 6Last Post: 05-05-2009, 05:07 PM -
Graphics2D: stack overflow error
By rosh72851 in forum New To JavaReplies: 11Last Post: 10-15-2008, 09:01 PM -
How to set a stack size. -Xss doesn't work
By protonus in forum New To JavaReplies: 4Last Post: 06-27-2008, 06:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks