Thread: fibonacci
View Single Post
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: fibonacci

 
0
  #5
Oct 24th, 2007
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:

  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.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote