Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 14th, 2006, 6:22 PM   #1
angry_asian
Hobbyist Programmer
 
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3 angry_asian is on a distinguished road
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;
}
but i get 554623543 for fib(5) instead of 5
..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;
}
angry_asian is offline   Reply With Quote
Old Aug 14th, 2006, 6:43 PM   #2
angry_asian
Hobbyist Programmer
 
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3 angry_asian is on a distinguished road
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;
}
angry_asian is offline   Reply With Quote
Old Aug 14th, 2006, 6:46 PM   #3
Random Spirit
Unverified User
 
Join Date: Aug 2006
Posts: 88
Rep Power: 0 Random Spirit is on a distinguished road
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.
Random Spirit is offline   Reply With Quote
Old Aug 14th, 2006, 6:50 PM   #4
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by angry_asian
... i have no idea whats wrong...
this is what is wrong:
Quote:
Originally Posted by angry_asian
i understand it perfectly and know exactly what to do
EDIT: too slow again... sorry RS

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.
stevengs is offline   Reply With Quote
Old Aug 14th, 2006, 6:53 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 14th, 2006, 6:54 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 14th, 2006, 6:56 PM   #7
Random Spirit
Unverified User
 
Join Date: Aug 2006
Posts: 88
Rep Power: 0 Random Spirit is on a distinguished road
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.
Random Spirit is offline   Reply With Quote
Old Aug 14th, 2006, 7:44 PM   #8
angry_asian
Hobbyist Programmer
 
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3 angry_asian is on a distinguished road
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.
angry_asian is offline   Reply With Quote
Old Aug 14th, 2006, 8:06 PM   #9
angry_asian
Hobbyist Programmer
 
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3 angry_asian is on a distinguished road
Quote:
Originally Posted by DaWei
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.
haha i get it..yeah i figured out that was the problem...3 wasnt accounted for cause i had greater and less no equal...it was not in my approach, just a simle n00b mistake.
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;
}
angry_asian is offline   Reply With Quote
Old Aug 14th, 2006, 8:15 PM   #10
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
Quote:
Originally Posted by angry_asian
Quote:
Originally Posted by DaWei
Have you checked the forum's rules regarding signatures?(...)
haha i get it..
Quote:
Originally Posted by Signature rules
Maximum length - The maximum length of a forum signature is 4 to 5 lines.
I don't think you get it just yet... :p
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC