Fibonacci sequence with arrays

Reply

Join Date: Apr 2008
Posts: 10
Reputation: Karkalash is an unknown quantity at this point 
Solved Threads: 0
Karkalash Karkalash is offline Offline
Newbie Poster

Fibonacci sequence with arrays

 
0
  #1
May 13th, 2008
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

  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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 4
Reputation: twilightwolf90 is an unknown quantity at this point 
Solved Threads: 0
twilightwolf90 twilightwolf90 is offline Offline
Newbie Poster

Re: Fibonacci sequence with arrays

 
0
  #2
May 16th, 2008
Originally Posted by Karkalash View 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

  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".
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: dotaa is an unknown quantity at this point 
Solved Threads: 0
dotaa dotaa is offline Offline
Newbie Poster
 
0
  #3
29 Days Ago
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..
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: dotaa is an unknown quantity at this point 
Solved Threads: 0
dotaa dotaa is offline Offline
Newbie Poster
 
0
  #4
29 Days Ago
Originally Posted by dotaa View Post
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
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC