| Author |
Message |
Andy
|
Posted: Mon Dec 04, 2006 6:57 pm Post subject: (No subject) |
|
|
| put the print inside the recursive function |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
gsquare567
|
Posted: Mon Dec 04, 2006 9:20 pm Post subject: (No subject) |
|
|
| 1.5 came out with the Scanner class, much better than the readers. but if this is for school, yeah, its probably still 1.4. damn school's slow updates. |
|
|
|
|
 |
cool dude

|
Posted: Mon Dec 04, 2006 10:56 pm Post subject: (No subject) |
|
|
Andy wrote: put the print inside the recursive function
I've actually been told the exact same thing by ultrahex. Here is the edited code all credit goes to ultrahex! However as you can probably see it works but it displays the fibonacci sequence backwards. I can obviously use a sort but that would be kinda cheating.
| code: |
import java.io.*;
public class Fibonacci
{
public static void main (String[] args) throws Exception
{
int terms = 0;
int ans = 0;
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("Enter number of terms to display");
terms = Integer.parseInt (myInput.readLine ());
System.out.println ("\n" + terms + " terms of Fibonacci numbers are:");
System.out.println (test (terms));
}
public static int test (int z)
{
System.out.println (Fib(z));
if (z > 1)
{
test(z-1);
}
return 0;
}
public static int Fib (int n)
{
if (n <= 2)
{
//System.out.println (1);
return 1;
}
else
{
//System.out.println (Fib (n - 1) + Fib (n - 2));
return Fib (n - 1) + Fib (n - 2);
}
}
} |
|
|
|
|
|
 |
OneOffDriveByPoster
|
Posted: Tue Dec 05, 2006 1:22 am Post subject: (No subject) |
|
|
If you are going to loop over with another function, it should be easy to fix...; however you can actually print inside Fib().
int fib(int n, bool print = true) {
int const ret = n <= 1 ? n == 1 : fib(n - 2, false) + fib(n - 1, print);
if (print) cout << ret << endl;
return ret;
}
|
|
|
|
|
 |
Aziz

|
Posted: Tue Dec 05, 2006 9:26 am Post subject: (No subject) |
|
|
| add each number onto a string perhaps |
|
|
|
|
 |
cool dude

|
Posted: Tue Dec 05, 2006 5:55 pm Post subject: (No subject) |
|
|
Aziz wrote: add each number onto a string perhaps
umm not sure what good that would do. only more work.
anyways i managed to get it in the right order and all. the only problem now is i keep getting a zero displaying at the end of my fibonacci sequence. It is because of this in my test method.
| code: |
import java.io.*;
public class Fibonacci
{
public static void main (String[] args) throws Exception
{
int terms = 0;
int ans = 0;
int start = 0;
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("Enter number of terms to display");
terms = Integer.parseInt (myInput.readLine ());
System.out.println ("\n" + terms + " terms of Fibonacci numbers are:");
System.out.println (test (terms, start));
}
public static int test (int z, int start)
{
System.out.println (Fib(start));
if (z > (start + 1))
{
return test(z,start + 1);
}
return 0;
}
public static int Fib (int n)
{
if (n < 2)
{
//System.out.println (1);
return 1;
}
else
{
//System.out.println (Fib (n - 1) + Fib (n - 2));
return Fib (n - 1) + Fib (n - 2);
}
}
} |
|
|
|
|
|
 |
gsquare567
|
Posted: Tue Dec 05, 2006 6:38 pm Post subject: (No subject) |
|
|
| im not 100% sure, but i think after an int reaches its max value, it goes to the negatives where it is at its lowest point, and your method goes on infinitaly until that, so it eventually returns 0 once it reaches its max since u keep adding 1 to the variable. |
|
|
|
|
 |
cool dude

|
Posted: Tue Dec 05, 2006 9:31 pm Post subject: (No subject) |
|
|
gsquare567 wrote: im not 100% sure, but i think after an int reaches its max value, it goes to the negatives where it is at its lowest point, and your method goes on infinitaly until that, so it eventually returns 0 once it reaches its max since u keep adding 1 to the variable.
nope not at all! read my post i actually told you where my mistake is. i just don't know how to fix it. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
OneOffDriveByPoster
|
Posted: Wed Dec 06, 2006 12:44 am Post subject: (No subject) |
|
|
cool dude wrote: | code: |
System.out.println (test (terms, start));
|
Interesting, no? |
|
|
|
|
 |
cool dude

|
Posted: Wed Dec 06, 2006 3:29 pm Post subject: (No subject) |
|
|
OneOffDriveByPoster wrote: cool dude wrote: | code: |
System.out.println (test (terms, start));
|
Interesting, no?
hmm. not sure? |
|
|
|
|
 |
Andy
|
Posted: Wed Dec 06, 2006 4:51 pm Post subject: (No subject) |
|
|
| he was pointing out the vagueness of your variable names |
|
|
|
|
 |
cool dude

|
Posted: Wed Dec 06, 2006 5:16 pm Post subject: (No subject) |
|
|
Andy wrote: he was pointing out the vagueness of your variable names
ahh yes. the variable names are pretty bad. i'm going to be edditting a lot of the code format and putting in comments. i was just stuck on this problem so i kinda started making test methods and not caring about the format which i know is wrong to do.  |
|
|
|
|
 |
|