Thread: fibonacci
View Single Post
Join Date: Aug 2007
Posts: 1,670
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 192
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: fibonacci

 
0
  #3
Oct 24th, 2007
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?
  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. }
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote