943,952 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7936
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 25th, 2007
0

Re: fibonacci

Oh, ok. so changing the array to static solved everything. It's still really slow, compared to this one:

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Oct 25th, 2007
1

Re: fibonacci

>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.
C++ Syntax (Toggle Plain Text)
  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. }
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

Oh ok great. thanks for the help! It's working great!
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006

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