943,925 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7935
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 24th, 2007
0

fibonacci

Expand Post »
Hey guys,

I'm supposed to write a program that will calculate the Nth fibonacci number, without using recursion.

I have the recursion problem written already, but am having a hard time doing the iterative one.

here's my recursion function:

C++ Syntax (Toggle Plain Text)
  1. int fib ( int n )
  2. {
  3. if ( n <= 1 )
  4. return 1 ;
  5. else
  6. return ( fib ( n - 1 ) + ( fib ( n - 2 ) ) ) ;
  7. }

any help with getting me started on the iterative one?
Similar Threads
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Oct 24th, 2007
0

Re: fibonacci

Nevermind, I got it working. Didn't think to use an array; well I thought about it, but didn't think it would make the problem easier. Here's my fibonacci sequence, written iteratively.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std ;
  3.  
  4. int main ( )
  5. {
  6. int fibArray[25] ; //max fib number
  7. fibArray[0] = 1 ;
  8. fibArray[1] = 1 ;
  9.  
  10. for ( int i = 2 ; i < 25 ; i ++ )
  11. fibArray[ i ] = fibArray[ i - 1 ] + fibArray[ i - 2 ] ;
  12.  
  13. cout << fibArray[4] << "\t" << fibArray[7] << "\t" << fibArray[24] << endl ;
  14.  
  15. return 0 ;
  16. }
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Oct 24th, 2007
0

Re: fibonacci

Your array based solution is limiting in that you have set the array size to the number of values to display.

What about a purely iterative solution, that does not need the excess memory, and is not logically limited in the number of values to display?
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std ;
  3.  
  4. int main ( )
  5. {
  6. unsigned int fib ; //current fibonnaci value
  7. unsigned int n1 = 1 ;
  8. unsigned int n2 = 1 ;
  9.  
  10. int max_fib;
  11.  
  12. cout << "Enter number of term for Fibonacci series: " ;
  13. cin >> max_fib;
  14.  
  15. cout << n1 << '\t' << n2 << '\t';
  16.  
  17. for ( int i = 2 ; i < max_fib; i ++ )
  18. {
  19. fib = n1 + n2;
  20. n1 = n2;
  21. n2 = fib;
  22. cout << fib << "\t" ;
  23.  
  24. if( i % 5 == 0 ) //five numbers per line
  25. cout << endl;
  26. }
  27.  
  28. return 0 ;
  29. }
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 24th, 2007
0

Re: fibonacci

>What about a purely iterative solution, that does not need the excess
>memory, and is not logically limited in the number of values to display?
That's fine for just printing the sequence. The array is more open to extension though. Let's say you want to write fib(x) where x is the number and the function returns F^N. With the array you can do this:
C++ Syntax (Toggle Plain Text)
  1. int fib ( int x )
  2. {
  3. static const int max = 25;
  4. static int n = 2;
  5. static int fibArray[max] = {1,1};
  6.  
  7. while ( n < x ) {
  8. fibArray[n] = fibArray[n - 1] + fibArray[n - 2];
  9. ++n;
  10. }
  11.  
  12. return fibArray[x];
  13. }
At the (minor, in this case) cost of extra storage, you avoid recalculating parts of the sequence that you've already calculated. That's the principle behind the dynamic recursive algorithm.
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Oct 24th, 2007
0

Re: fibonacci

seeing as this is being discussed still, I would like to pose another question: How do I use an array using recursion? This will be more efficient, no? Here is my current code:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std ;
  3.  
  4. int fib ( int ) ;
  5.  
  6. int main ( )
  7. {
  8. int i = fib ( 5 ) ;
  9. cout << i << endl ;
  10.  
  11. return 0 ;
  12. }
  13.  
  14. int fib ( int n )
  15. {
  16. int fibArray [ const n ] ;
  17.  
  18. if ( <= 1 )
  19. return 1 ;
  20. else
  21. {
  22. fibArray[ 0 ] = 1 ;
  23. fibArray[ 1 ] = 1 ;
  24.  
  25. for ( int i = 2 ; i < 25 ; i++ )
  26. fibArray[ i ] = ( fib ( n - 1 ) + ( fib ( n - 2 ) ) ) ;
  27. }
  28.  
  29. return fibArray[ n ] ;
  30. }
Last edited by Duki; Oct 24th, 2007 at 9:31 pm.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Oct 24th, 2007
0

Re: fibonacci

Click to Expand / Collapse  Quote originally posted by Duki ...
seeing as this is being discussed still, I would like to pose another question: How do I use an array using recursion? This will be more efficient, no? Here is my current code:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std ;
  3.  
  4. int fib ( int ) ;
  5.  
  6. int main ( )
  7. {
  8. int i = fib ( 5 ) ;
  9. cout << i << endl ;
  10.  
  11. return 0 ;
  12. }
  13.  
  14. int fib ( int n )
  15. {
  16. int fibArray [ const n ] ;
  17.  
  18. if ( <= 1 )
  19. return 1 ;
  20. else
  21. {
  22. fibArray[ 0 ] = 1 ;
  23. fibArray[ 1 ] = 1 ;
  24.  
  25. for ( int i = 2 ; i < 25 ; i++ )
  26. fibArray[ i ] = ( fib ( n - 1 ) + ( fib ( n - 2 ) ) ) ;
  27. }
  28.  
  29. return fibArray[ n ] ;
  30. }
why can i not do int fibArray [ n ] ? It says "expected constant"
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Oct 25th, 2007
0

Re: fibonacci

That's a compiler dependent problem now.

Previously, any declaration of a local array had to be done with a constant expression as the size. The latest C standard (C99) has changed that restriction, but not all compilers have adopted that. And remember, the C++ standard includes the C standard, more or less.

So, legally speaking:
C++ Syntax (Toggle Plain Text)
  1. int size = 5;
  2. int arr[size];
is now a legitimate construct. Current version of gcc supports this. Visual C++ does not.

Now I have to go change my lesson plans.....
Val
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Oct 25th, 2007
0

Re: fibonacci

Ok, I got it to work using a pointer. Here's my code.

  
c++ Syntax (Toggle Plain Text)
  1. #include <ctime>
  2. #include <iostream>
  3. using namespace std ;
  4.  
  5. int fib ( int ) ;
  6.  
  7. int main ( )
  8. {
  9. clock_t start ;
  10. clock_t end ;
  11.  
  12. cout << "Beginning timer..." << endl ;
  13. start = clock() ;
  14.  
  15. cout << endl ;
  16. for ( int k = 0 ; k < 12 ; k++ )
  17. {
  18. int i = fib ( k ) ;
  19. cout << i << "\t" ;
  20. }
  21.  
  22. cout << endl ;
  23.  
  24. end = clock() ;
  25. cout << "\nThe calculation took " << ( double ( end ) - start ) / CLOCKS_PER_SEC << " seconds.\n" ;
  26.  
  27. cout << "\n\n-- Done --\n\n" ;
  28.  
  29. return 0 ;
  30. }
  31.  
  32. int fib ( int n )
  33. {
  34. int * fibArray = new int[ n ] ;
  35.  
  36. if ( n <= 1 )
  37. return 1 ;
  38. else
  39. {
  40. fibArray[ 0 ] = 1 ;
  41. fibArray[ 1 ] = 1 ;
  42.  
  43. for ( int i = 2 ; i <= n ; i++ )
  44. fibArray [ i ] = ( fib ( n - 1 ) + ( fib ( n - 2 ) ) ) ;
  45.  
  46. }
  47.  
  48. return fibArray[ n ] ;
  49. }
I can't get it to work with numbers >=12; it gives a system error that it failed unexpectedly. I'm guessing it's a stack issue, but is there a way around it? Is my program the best it can be (using recursion and arrays)?
Last edited by Duki; Oct 25th, 2007 at 1:44 am.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Oct 25th, 2007
0

Re: fibonacci

>And remember, the C++ standard includes the C standard, more or less.
Standard C++ currently maintains compatibility with C89, not C99. So you can't use VLAs in C++

>Is my program the best it can be (using recursion and arrays)?
No. The entire point of using an array with recursion is to avoid recalculating everything all the time. This is especially true with the Fibonacci sequence, where a lot of the numbers have to be recalculated. You use an array, but you don't take advantage of it, so your function is just a slower, more wasteful version of the original recursive algorithm.

What you need is for the array to persist between function calls and be able to tell if a number has been calculated or not. If it has, you don't need to go through the recursive chain. This is where you'll see performance improvements:
C++ Syntax (Toggle Plain Text)
  1. int fib ( int n )
  2. {
  3. static int fibArray[25] = {0};
  4.  
  5. if ( n <= 1 )
  6. return 1;
  7. else {
  8. fibArray[0] = 1;
  9. fibArray[1] = 1;
  10.  
  11. for ( int i = 2 ; i <= n ; i++ ) {
  12. if ( fibArray[i] == 0 )
  13. fibArray[i] = ( fib ( n - 1 ) + ( fib ( n - 2 ) ) );
  14. }
  15. }
  16.  
  17. return fibArray[n];
  18. }
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Oct 25th, 2007
0

Re: fibonacci

> I can't get it to work with numbers >=12; it gives a system error that it failed unexpectedly.
1. You don't free the memory you allocate. Eventually, you'll run out of memory.

2. You run off the end of the memory allocated.
for ( int i = 2 ; i <= n ; i++ )
If you want <= n, then you need to allocate n+1 items.
It was always wrong, you just got caught with your hand in the cookie jar at n >= 12.

3. Unlike Ptolemy's answer, your array serves no purpose.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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 C++ Forum Timeline: Please Help Dynamic Memory Allocation.
Next Thread in C++ Forum Timeline: homework help





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


Follow us on Twitter


© 2011 DaniWeb® LLC