944,083 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 44665
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 2nd, 2006
0

Fibonacci Series

Expand Post »
hi
I need help with this problem. I have tried writting the program, i just can not get it to run properly.

Please Write
The Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
(a) Write a nonrecursive function fibonacci( n ) that calculates the nth Fibonacci number.

and than you
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kookai is offline Offline
6 posts
since Jul 2006
Aug 2nd, 2006
0

Re: Fibonacci Series

A search for Fibonacci here or with Google ought to give you enough to start with, if not something to finish with.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 3rd, 2006
0

Re: Fibonacci Series

This is one way of going about it without much thinking

fn = [ Phi^ n - (-Phi)^-n ] / (2Phi-1)
for smaller values of n you need approximation.

Phi = ( 5^½ + 1 ) / 2 = 1.6180339 (the golden ratio)
^ = power

But this method is seldom used. This is more mathematical. Use Google as Dave suggested. You can get lots of material.
Reputation Points: 16
Solved Threads: 3
Junior Poster in Training
dilip.mathews is offline Offline
89 posts
since Jun 2006
Aug 3rd, 2006
0

Re: Fibonacci Series

Simply put it this way, when ur applications demands a particular Fibonacci number (for eg. the 10th fibonacci number) then GOLDEN RATIO is the way to do it and if u require the whole series of numbers then the incremental method is the way.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Aug 5th, 2006
0

Re: Fibonacci Series

G'day

where error?

1 error(s), 0 warning(s)



C++ Syntax (Toggle Plain Text)
  1. # include <iostream.h>
  2. int fibonacci(int n)
  3. {<blockquote> int x1 = 0, fib;
  4. int x2 = 1;
  5. if(n >= 1)
  6. {<blockquote> for(int i=2;i<= n; i++)
  7. {
  8. </blockquote></blockquote><blockquote><blockquote><blockquote>fib = x1+ x2;
  9. x1 = x2;
  10. x2 = fib;
  11. </blockquote></blockquote></blockquote><blockquote><blockquote> }
  12.  
  13. return fib;
  14. </blockquote></blockquote><blockquote> }
  15. </blockquote>
Last edited by WolfPack; Aug 5th, 2006 at 7:58 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kookai is offline Offline
6 posts
since Jul 2006
Aug 5th, 2006
1

Re: Fibonacci Series

Try to append this to your code
C++ Syntax (Toggle Plain Text)
  1. }
  2.  
  3. int main()
  4. {
  5. cout<<fibonacci(/*some number*/);
  6. }
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Aug 5th, 2006
1

Re: Fibonacci Series

> where error?
Please use the code tags when posting code - can't you read this
http://www.daniweb.com/techtalkforum...cement8-3.html
Or see the background image in the compose message window?

Also, count the { and }
I count 3 { and only 2 }
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 5th, 2006
0

Re: Fibonacci Series

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int fibonacci(int n)
  5. {
  6. int x1 = 0, fib;
  7. int x2 = 1;
  8. if(n >= 1)
  9. {
  10. for(int i=2;i<= n; i++)
  11. {
  12. fib = x1+ x2;
  13. x1 = x2;
  14. x2 = fib;
  15. }}
  16.  
  17. return fib;
  18. }
  19. int main(){
  20. for(int i=2;i<=10;i++) {
  21. cout<<fibonacci(i)<<endl;
  22. }
  23. return 0;
  24. }

this is ok now I'm finish it
but I need this


(1) Determine the largest int Fibonacci number that can be printed on your system.
(2) Modify the program of part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified program to repeat part (b).

and
Last edited by Dave Sinkula; Aug 5th, 2006 at 3:51 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kookai is offline Offline
6 posts
since Jul 2006
Aug 5th, 2006
1

Re: Fibonacci Series

Quote originally posted by kookai ...
this is ok now I'm finish it
but I need this


(1) Determine the largest int Fibonacci number that can be printed on your system.
(2) Modify the program of part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified program to repeat part (b).
(1) Run the program. Use a value bigger than 10 in the main loop.

(2) Surely you can make an attempt at these changes.

Please enclose posted code in code tags -- just like it says in the little box you reply in.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 12th, 2009
0

Re: Fibonacci Series

Here is the shortest code for finding the fibo series

#include <iostream>
using namespace std;

int main(){
long a;
cout << "Enetr Limit you want\n\n";
cin >> a;
cout << "Fibonachi numbers < " << a << endl;
long f = 0, f2, f1 = 1;
while(true) {
f2 = f + f1;
if(f2 > a) {
break;
}
cout << " " << f2 << endl;
f = f1;
f1 = f2;
}
system("pause");
return 0;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
asad jaan is offline Offline
2 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: Corrupted .cpp file
Next Thread in C++ Forum Timeline: C++ newbie: understanding example on benefit of using const in function declarations





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC