fibonacci

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

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
  #11
Oct 25th, 2007
Oh, ok. so changing the array to static solved everything. It's still really slow, compared to this one:

  1. #include <ctime>
  2. #include <iostream>
  3. using namespace std ;
  4.  
  5. int main ( )
  6. {
  7. int n ;
  8.  
  9. cout << "Enter number of elements: " << flush ;
  10. cin >> n ;
  11.  
  12. int * fibArray = new int[ n ] ;
  13.  
  14. clock_t start ;
  15. clock_t end ;
  16.  
  17. cout << "Beginning timer..." << endl ;
  18. start = clock() ;
  19.  
  20. fibArray[ 0 ] = 1 ;
  21. fibArray[ 1 ] = 1 ;
  22.  
  23. for ( int i = 2 ; i < n ; i++ )
  24. fibArray[ i ] = fibArray[ i - 1 ] + fibArray[ i - 2 ] ;
  25.  
  26. for ( int j = 0 ; j < n ; j++ )
  27. cout << fibArray[j] << "\t" ;
  28.  
  29. cout << endl ;
  30.  
  31. end = clock() ;
  32. //cout.precision(1000) ;
  33. cout << "\nThe calculation took " << showpoint <<( double ( end ) - start ) / CLOCKS_PER_SEC << " seconds.\n" ;
  34.  
  35. cout << "\n\n-- Done --\n\n" ;
  36.  
  37. cout << endl ;
  38.  
  39. return 0 ;
  40. }
But that was expected; we get extra credit for programming both and timing the two.
I'm still frustrated that I can't do static int fibArray [ n ] instead of 25, or some other constant value.
Last edited by Duki; Oct 25th, 2007 at 2:24 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 Quick reply to this message  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: fibonacci

 
1
  #12
Oct 25th, 2007
>so changing the array to static solved everything.
No, changing the array to static and actually using the values you've cached solved everything. If you don't to the latter, your code won't be any faster than the usual recursive solution.

>It's still really slow, compared to this one
Your function might be slow, but mine is a hair faster in my tests than the code you're referring to. I've included a little test below.

>I'm still frustrated that I can't do static int fibArray [ n ]
>instead of 25, or some other constant value.
A better alternative is to pass a buffer to the function rather than use a static or global array. I've made that change in the test below.
  1. #include <ctime>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int fib ( int fibArray[], int n )
  7. {
  8. if ( n <= 1 )
  9. return 1;
  10. else {
  11. fibArray[0] = 1;
  12. fibArray[1] = 1;
  13.  
  14. for ( int i = 2 ; i <= n ; i++ ) {
  15. if ( fibArray[i] == 0 )
  16. fibArray[i] = ( fib ( fibArray, n - 1 )
  17. + ( fib ( fibArray, n - 2 ) ) );
  18. }
  19. }
  20.  
  21. return fibArray[n];
  22. }
  23.  
  24. int main()
  25. {
  26. int n;
  27.  
  28. cout << "Enter number of elements: ";
  29. cin >> n;
  30.  
  31. int * fibArray = new int[n];
  32.  
  33. clock_t start;
  34.  
  35. cout << "Beginning timer..." << endl;
  36. start = clock();
  37.  
  38. fibArray[ 0 ] = 1;
  39. fibArray[ 1 ] = 1;
  40.  
  41. for ( int i = 2 ; i < n ; i++ )
  42. fibArray[ i ] = fibArray[ i - 1 ] + fibArray[ i - 2 ];
  43.  
  44. for ( int j = 0 ; j < n ; j++ )
  45. cout << fibArray[j] << "\t";
  46. cout << '\n';
  47.  
  48. cout << "\nThe calculation took " << showpoint
  49. <<( double ( clock() ) - start ) / CLOCKS_PER_SEC << " seconds.\n";
  50. cout << "\n\n-- Done --\n\n";
  51.  
  52. // Reset the array so that previous results don't make fib faster
  53. for ( int i = 0; i < n; i++ )
  54. fibArray[i] = 0;
  55.  
  56. cout << "Beginning timer..." << endl;
  57. start = clock();
  58.  
  59. for ( int j = 0 ; j < n ; j++ )
  60. cout << fib ( fibArray, j ) << "\t";
  61. cout << '\n';
  62.  
  63. cout << "\nThe calculation took " << showpoint
  64. <<( double ( clock() ) - start ) / CLOCKS_PER_SEC << " seconds.\n";
  65. cout << "\n\n-- Done --\n\n";
  66.  
  67. return 0;
  68. }
Reply With Quote Quick reply to this message  
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
  #13
Oct 25th, 2007
Oh ok great. thanks for the help! It's working great!
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 Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC