943,955 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 6464
  • Java RSS
May 13th, 2008
0

Fibonacci sequence with arrays

Expand Post »
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)
  1.  
  2. import java.util.Scanner; //import scanner
  3.  
  4. public class TestFibArray {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. Scanner sc = new Scanner(System.in); //create new scanner
  9.  
  10. //Prompt user for length
  11. System.out.print("How many fib terms would you like to display? ");
  12. int lenght = sc.nextInt();
  13.  
  14. int[] p = new int[lenght]; //set new array p equal the defined user length
  15.  
  16. printTerms( p, lenght ); //method to print terms
  17.  
  18.  
  19. }//main end
  20.  
  21. //method to populate and calculate array and fib sequence
  22. public static int getNTerms( int[] p, int lenght ) {
  23.  
  24. //print first two terms
  25. p[1] = 0;
  26. p[2] = 1;
  27. int newTerm = 0; //new value to calculate new terms on the sequence other that the first two
  28.  
  29. if ( lenght == 1 )
  30. System.out.println(p[1]);
  31. else if ( lenght == 2 )
  32. System.out.println( p[1] + " " + p[2]);
  33. else { //print rest of terms
  34.  
  35. System.out.print( p[1] + " " + p[2] + " ");
  36.  
  37. //for loop to calculate the rest of the terms
  38. for ( int index = 3; index <= lenght; index++) {
  39.  
  40. newTerm = p[1] + p[2];
  41. System.out.print( newTerm + " ");
  42. p[1] = p[2];
  43. p[2] = newTerm;
  44. }
  45.  
  46. }
  47.  
  48. return newTerm;
  49.  
  50. } //nterms end
  51.  
  52. //print terms
  53. public static void printTerms ( int[] p, int lenght ) {
  54.  
  55. //print on console
  56. System.out.print(getNTerms ( p, lenght ));
  57. } //print end
  58.  
  59. } // file end

Any tips would be appreciated
Similar Threads
Reputation Points: 17
Solved Threads: 0
Newbie Poster
Karkalash is offline Offline
17 posts
since Apr 2008
May 16th, 2008
0

Re: Fibonacci sequence with arrays

Click to Expand / Collapse  Quote originally posted by Karkalash ...
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)
  1.  
  2. import java.util.Scanner; //import scanner
  3.  
  4. public class TestFibArray {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. Scanner sc = new Scanner(System.in); //create new scanner
  9.  
  10. //Prompt user for length
  11. System.out.print("How many fib terms would you like to display? ");
  12. int lenght = sc.nextInt();
  13.  
  14. int[] p = new int[lenght]; //set new array p equal the defined user length
  15.  
  16. printTerms( p, lenght ); //method to print terms
  17.  
  18.  
  19. }//main end
  20.  
  21. //method to populate and calculate array and fib sequence
  22. public static int getNTerms( int[] p, int lenght ) {
  23.  
  24. //print first two terms
  25. p[1] = 0;
  26. p[2] = 1;
  27. int newTerm = 0; //new value to calculate new terms on the sequence other that the first two
  28.  
  29. if ( lenght == 1 )
  30. System.out.println(p[1]);
  31. else if ( lenght == 2 )
  32. System.out.println( p[1] + " " + p[2]);
  33. else { //print rest of terms
  34.  
  35. System.out.print( p[1] + " " + p[2] + " ");
  36.  
  37. //for loop to calculate the rest of the terms
  38. for ( int index = 3; index <= lenght; index++) {
  39.  
  40. newTerm = p[1] + p[2];
  41. System.out.print( newTerm + " ");
  42. p[1] = p[2];
  43. p[2] = newTerm;
  44. }
  45.  
  46. }
  47.  
  48. return newTerm;
  49.  
  50. } //nterms end
  51.  
  52. //print terms
  53. public static void printTerms ( int[] p, int lenght ) {
  54.  
  55. //print on console
  56. System.out.print(getNTerms ( p, lenght ));
  57. } //print end
  58.  
  59. } // file end

Any tips would be appreciated
fyi, "length" is the correct spelling. A trace of p[1] and p[2] is as follows:

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".
Reputation Points: 10
Solved Threads: 0
Newbie Poster
twilightwolf90 is offline Offline
4 posts
since May 2008
Oct 28th, 2009
0
Re: Fibonacci sequence with arrays
Hi can someone explain how to get rid of the extra number that is being outputted at the end of the sequence? I didn't understand what the above comment said..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dotaa is offline Offline
2 posts
since Oct 2009
Oct 28th, 2009
0
Re: Fibonacci sequence with arrays
Click to Expand / Collapse  Quote originally posted by dotaa ...
Hi can someone explain how to get rid of the extra number that is being outputted at the end of the sequence? I didn't understand what the above comment said..
Ahh i got it lol
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dotaa is offline Offline
2 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Desperately Needing Help in Hangman Game
Next Thread in Java Forum Timeline: compareTo and Priority Queue problems.....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC