| | |
Fibonacci sequence with arrays
![]() |
•
•
Join Date: Apr 2008
Posts: 10
Reputation:
Solved Threads: 0
Hi:
I have to write a program generating the fibonacci sequence based on the length desired by the user. I'm using arrays and methods too. So I have to do one method to generate the sequence and another to print the other method. So far so good, except the last value is repeated. For example if I input 5, this is what it should come out
0 1 1 2 3
except this comes out
0 1 1 2 3 3
Here's what I done
Any tips would be appreciated
I have to write a program generating the fibonacci sequence based on the length desired by the user. I'm using arrays and methods too. So I have to do one method to generate the sequence and another to print the other method. So far so good, except the last value is repeated. For example if I input 5, this is what it should come out
0 1 1 2 3
except this comes out
0 1 1 2 3 3
Here's what I done
java Syntax (Toggle Plain Text)
import java.util.Scanner; //import scanner public class TestFibArray { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //create new scanner //Prompt user for length System.out.print("How many fib terms would you like to display? "); int lenght = sc.nextInt(); int[] p = new int[lenght]; //set new array p equal the defined user length printTerms( p, lenght ); //method to print terms }//main end //method to populate and calculate array and fib sequence public static int getNTerms( int[] p, int lenght ) { //print first two terms p[1] = 0; p[2] = 1; int newTerm = 0; //new value to calculate new terms on the sequence other that the first two if ( lenght == 1 ) System.out.println(p[1]); else if ( lenght == 2 ) System.out.println( p[1] + " " + p[2]); else { //print rest of terms System.out.print( p[1] + " " + p[2] + " "); //for loop to calculate the rest of the terms for ( int index = 3; index <= lenght; index++) { newTerm = p[1] + p[2]; System.out.print( newTerm + " "); p[1] = p[2]; p[2] = newTerm; } } return newTerm; } //nterms end //print terms public static void printTerms ( int[] p, int lenght ) { //print on console System.out.print(getNTerms ( p, lenght )); } //print end } // file end
Any tips would be appreciated
•
•
Join Date: May 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Hi:
I have to write a program generating the fibonacci sequence based on the length desired by the user. I'm using arrays and methods too. So I have to do one method to generate the sequence and another to print the other method. So far so good, except the last value is repeated. For example if I input 5, this is what it should come out
0 1 1 2 3
except this comes out
0 1 1 2 3 3
Here's what I done
java Syntax (Toggle Plain Text)
import java.util.Scanner; //import scanner public class TestFibArray { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //create new scanner //Prompt user for length System.out.print("How many fib terms would you like to display? "); int lenght = sc.nextInt(); int[] p = new int[lenght]; //set new array p equal the defined user length printTerms( p, lenght ); //method to print terms }//main end //method to populate and calculate array and fib sequence public static int getNTerms( int[] p, int lenght ) { //print first two terms p[1] = 0; p[2] = 1; int newTerm = 0; //new value to calculate new terms on the sequence other that the first two if ( lenght == 1 ) System.out.println(p[1]); else if ( lenght == 2 ) System.out.println( p[1] + " " + p[2]); else { //print rest of terms System.out.print( p[1] + " " + p[2] + " "); //for loop to calculate the rest of the terms for ( int index = 3; index <= lenght; index++) { newTerm = p[1] + p[2]; System.out.print( newTerm + " "); p[1] = p[2]; p[2] = newTerm; } } return newTerm; } //nterms end //print terms public static void printTerms ( int[] p, int lenght ) { //print on console System.out.print(getNTerms ( p, lenght )); } //print end } // file end
Any tips would be appreciated
p[1] p[2] newTerm
0 1 1
1 1 2
1 2 3 (for loop terminates here!)
And then you return newTerm (3) to printTerms(), which displays the number again. I would delete the printTerms() method, rename the getNTerms() to printNTerms(), and remove the "return newTerm;" line and change the method type from "int" to "void".
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Desperately Needing Help in Hangman Game
- Next Thread: compareTo and Priority Queue problems.....
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows





