This is a discussion on Fibonacci within the C Programming forums, part of the General Programming Boards category; Originally Posted by liyanhong My English is poor, so I post code directly Code: #include <stdio.h> int main(void) { int ...
Code:#include <stdio.h> #define N 8 int main(void) { int F[46] = {0, 1}; int i; for (i = 2; i <= N; i++) F[i] = F[i-1] + F[i-2]; printf("%d\n", F[N]); return 0; } //I'm sorry, I mean 'F[8]' , heihei
Posting solutions directly is a bad way of teaching anyone anything.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Apart from not knowing whether saria777 has covered arrays yet, saria777 looks to have worked out about as much as liyanhong posted anyway, but without unnecessarily using a fixed-sized array, which incidentally is too small (as is the data type).
As for the bugs:
Take a closer look at laserlight's first post. You didn't follow the advice fully.
Check the places your brackets are. Indent the code properly and make sure everything is either inside or outside of where it should be.
Don't forget to actually return something.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Right what you said!!
I think the best solution is to implement the Fibonacci algorithm iteratively, as you have already said. Infact it takes much less memory and time (on an input n = 58, F(58) with the recursion takes ≈ 4 hours, while iteratively only ≈ 0.7 millionths of a second)!
Here my pseudocode algorithm for Fibonacci, using cycles instead of recursion for calculating the n-th Fibonacci number :
Algorithm Fibonacci4 (int n) --> int
1. a <-- 1, b <-- 1
2. for i = 3 to n do
3. c <-- a + b
4. a <-- b
5. b <-- c
6. return b.
I hope it could be of a little help! Bye!!
Last edited by Zeldic; 06-14-2010 at 06:14 AM. Reason: An error
saria777, concerning the range of numbers that you are supposed to accommodate: check with your teacher. If you are allowed to compile with respect to the 1999 edition of the C standard, then you have access to unsigned long long, which would allow you to handle F(93). But F(100) really is beyond reach unless you use a non-standard bignum library, or code your own (which would be an entirely different project, and in some ways more difficult than what you are supposed to solve here). Otherwise, your teacher may tell you that this was an oversight, and perhaps you only need to deal with at most F(47).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
alrighty. I've got it to compile /finally/. And yes, when it gets so large it begins showing negative numbers.
Also, it's showing them as F5 being 5, F6 being 8, F7 being 13, F8 being 21, when it should be F5 being 8, F6 being 13, F7 being 21, etc. What would I need to make my int previous and result and i= to make it display the way I want?Code:#include <stdio.h> int Fibonacci (int n); int main () { long result; long number; printf("Enter an integer: "); scanf("%ld", &number); result=Fibonacci(number); printf("Fibonacci(%ld)=%ld/n",number,result); return 0; } int Fibonacci (int n) { int previous= 0; int result =1; int i; for (i=2; i <=n; ++i) { int const sum=result + previous; previous= result; result = sum; } return result; }
Your solution agrees with this Fibonacci table here:
PlanetMath: list of Fibonacci numbers
Your teacher has the sequence advanced by one... so if your teacher wanted 21 as an answer for input 7, just add 1 to user input and proceed.
Or callCode:Fibonacci(number + 1);
Last edited by nonoob; 06-14-2010 at 03:29 PM.
Oh, I didn't notice that he had to find large fibonacci numbers, i'm sorry, in this case the only solution is to use strings and int arrays to store big numbers, I made an algorithm to find fibonacci numbers up to 5000 once, my solution was to precalculate into a matrix all fibonacci numbers untill n = 5000 by doing string addition