![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3
![]() |
fibonacci sequence
i understand it perfectly and know exactly what to do, i have no idea whats wrong:
#include <cstdlib>
#include <iostream>
using namespace std;
int fib(int x);
int main()
{
int x;
Prompt:
cout << "Enter any integer above 0 to find its fibonacci value:";
cin >> x;
cout << "\n";
cout << fib(x);
cout << "\n";
system("PAUSE");
return 0;
}
int fib(int x)
{
if (x < 3)
return 1;
if (x > 3)
return (fib(x-1) + fib(x-2));
if (x <= 0)
cout << 0;
}..yeah....
__________________
my site: www.sreenathpillai.tk #include<iostream>
using namespace std;
int a=1;
int main()
{while(a<=500, a++)
{cout >>"I will not throw paper airplanes in class";}
return 0;
} |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3
![]() |
ok i got it but dont understand why...i changed it to this:
#include <cstdlib>
#include <iostream>
using namespace std;
int fib(int x);
int main()
{
int x;
Prompt:
cout << "Enter any integer above 0 to find its fibonacci value:";
cin >> x;
cout << "\n";
cout << fib(x);
cout << "\n";
system("PAUSE");
return 0;
}
int fib(int x)
{
if (x < 3)
return 1;
else
return (fib(x-1) + fib(x-2));
}
__________________
my site: www.sreenathpillai.tk #include<iostream>
using namespace std;
int a=1;
int main()
{while(a<=500, a++)
{cout >>"I will not throw paper airplanes in class";}
return 0;
} |
|
|
|
|
|
#3 |
|
Unverified User
Join Date: Aug 2006
Posts: 88
Rep Power: 0
![]() |
If x = 0 then the function should return 0
If x = 1 then the function should return 1 If x > 1 then the function should return fib(n - 1) + fib(n - 2) int fib(int x)
{
if (x <= 1)
return x;
else
return(fib(n -1) + fib(n -2));
}Last edited by Random Spirit; Aug 14th, 2006 at 6:57 PM. |
|
|
|
|
|
#4 | ||
|
Professional Programmer
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4
![]() |
Quote:
Quote:
Since RandomSpirit solved it for you, now you can give the iterative approach a try.
__________________
-Steven "Is this a piece of your brain?" - Basil Fawlty Last edited by stevengs; Aug 14th, 2006 at 7:05 PM. |
||
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Have you checked the forum's rules regarding signatures? I don't actually think it will tell you that 3 is neither less than 3 nor greater than 3, but it might.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Too slow here, too, but then I'm ancient and dilapidated.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Unverified User
Join Date: Aug 2006
Posts: 88
Rep Power: 0
![]() |
Thats ok stevengs. Im surprised i remembered how the fibonacci series worked off the top of my head. I was expecting someone like DaWei to come an correct me.
![]() |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3
![]() |
i said i got it working...i took a different approach however as u can see above. random spirits works too but technically its fib(1), 2, 3 not 0, 1 2 3...
__________________
my site: www.sreenathpillai.tk #include<iostream>
using namespace std;
int a=1;
int main()
{while(a<=500, a++)
{cout >>"I will not throw paper airplanes in class";}
return 0;
}Last edited by angry_asian; Aug 14th, 2006 at 8:04 PM. |
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3
![]() |
Quote:
i thought it was funny how everyone answered me after i posted that it worked ![]() still, THANKS ALL
__________________
my site: www.sreenathpillai.tk #include<iostream>
using namespace std;
int a=1;
int main()
{while(a<=500, a++)
{cout >>"I will not throw paper airplanes in class";}
return 0;
} |
|
|
|
|
|
|
#10 | |||
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3
![]() |
Quote:
Quote:
|
|||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| generating character sequence | Gumby | Java | 12 | Jun 14th, 2006 8:46 AM |
| First Python Programme: Fibonacci Finder | UnKnown X | Python | 2 | Dec 15th, 2005 7:19 PM |
| Fibonacci | lingon | Python | 8 | Apr 29th, 2005 7:22 PM |
| Help - Execution Sequence | anandt4u | C++ | 23 | Apr 8th, 2005 4:46 PM |
| Sequence Problem | Planet_EN | C++ | 9 | Mar 9th, 2005 4:24 PM |