| | |
fibonacci
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Oh, ok. so changing the array to static solved everything. It's still really slow, compared to this one:
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.
c++ Syntax (Toggle Plain Text)
#include <ctime> #include <iostream> using namespace std ; int main ( ) { int n ; cout << "Enter number of elements: " << flush ; cin >> n ; int * fibArray = new int[ n ] ; clock_t start ; clock_t end ; cout << "Beginning timer..." << endl ; start = clock() ; fibArray[ 0 ] = 1 ; fibArray[ 1 ] = 1 ; for ( int i = 2 ; i < n ; i++ ) fibArray[ i ] = fibArray[ i - 1 ] + fibArray[ i - 2 ] ; for ( int j = 0 ; j < n ; j++ ) cout << fibArray[j] << "\t" ; cout << endl ; end = clock() ; //cout.precision(1000) ; cout << "\nThe calculation took " << showpoint <<( double ( end ) - start ) / CLOCKS_PER_SEC << " seconds.\n" ; cout << "\n\n-- Done --\n\n" ; cout << endl ; return 0 ; }
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
-Edsger Dijkstra
>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.
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)
#include <ctime> #include <iostream> using namespace std; int fib ( int fibArray[], int n ) { if ( n <= 1 ) return 1; else { fibArray[0] = 1; fibArray[1] = 1; for ( int i = 2 ; i <= n ; i++ ) { if ( fibArray[i] == 0 ) fibArray[i] = ( fib ( fibArray, n - 1 ) + ( fib ( fibArray, n - 2 ) ) ); } } return fibArray[n]; } int main() { int n; cout << "Enter number of elements: "; cin >> n; int * fibArray = new int[n]; clock_t start; cout << "Beginning timer..." << endl; start = clock(); fibArray[ 0 ] = 1; fibArray[ 1 ] = 1; for ( int i = 2 ; i < n ; i++ ) fibArray[ i ] = fibArray[ i - 1 ] + fibArray[ i - 2 ]; for ( int j = 0 ; j < n ; j++ ) cout << fibArray[j] << "\t"; cout << '\n'; cout << "\nThe calculation took " << showpoint <<( double ( clock() ) - start ) / CLOCKS_PER_SEC << " seconds.\n"; cout << "\n\n-- Done --\n\n"; // Reset the array so that previous results don't make fib faster for ( int i = 0; i < n; i++ ) fibArray[i] = 0; cout << "Beginning timer..." << endl; start = clock(); for ( int j = 0 ; j < n ; j++ ) cout << fib ( fibArray, j ) << "\t"; cout << '\n'; cout << "\nThe calculation took " << showpoint <<( double ( clock() ) - start ) / CLOCKS_PER_SEC << " seconds.\n"; cout << "\n\n-- Done --\n\n"; return 0; }
![]() |
Similar Threads
- C++ Fibonacci program help (C++)
- I need help writing a program using Fibonacci sequence (C++)
- Help,Fibonacci numbers (VB.NET)
- Fibonacci number (C)
- Really Weird Error... Fibonacci Nums (C++)
Other Threads in the C++ Forum
- Previous Thread: Please Help Dynamic Memory Allocation.
- Next Thread: homework help
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






