I don't understand how to create a function that returns the n-th Fibonacci number.
thanks in advance
C++ Help (Recursive Fibonacci Function)?
Here is your answer, the first function is what you asked for, the main() function shows you how to call (use) it:
int Fibonacci(int nNumber)
{
if (nNumber == 0)
return 0;
if (nNumber == 1)
return 1;
return Fibonacci(nNumber-1) + Fibonacci(nNumber-2);
}
// And a main program to display the first 13 Fibonacci numbers
int main(void)
{
using namespace std;
for (int iii=0; iii %26lt; 13; iii++)
cout %26lt;%26lt; Fibonacci(iii) %26lt;%26lt; " ";
return 0;
}
Reply:This page shows the fibonacci formula:
http://en.wikipedia.org/wiki/Fibonacci_n...
Once you understand the formula, put it into psuedocode:
Take in a number.
If the number is 0 or 1, return that number.
Else
Return the result of f(n-1) + f(n-2)
Then put it into code:
long fib(unsigned long n) {
if (n %26lt;= 1) {
return n;
} else {
return fib(n-1)+fib(n-2);
}
}
Reply:i e-mailed you all the instructions
jk
blazing star
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment