Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 109,548 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,227 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Fibonacci

 
Reply to this topicStart new topic

Fibonacci

mapes479
post 5 Aug, 2008 - 06:54 PM
Post #1


New D.I.C Head

*
Joined: 17 Feb, 2008
Posts: 3

Can someone please read the following assignment and look at the code I've completed and tell me if it is correct before I submit it to my professor? Immediate help would be great if possible.

Here's the assignment -
The Fibonacci sequence consists of the numbers 1,1,2,3,5,8,13 ...... in which each number (except the first 2) is the sum of the two preceding numbers. This means that:

Assume that Fib(0)=1

Assume that Fib(1)=1

Then Fib(2)=Fib(0) + Fib(1)=1+1=2

Also Fib(3)= Fib(1)+Fib(2)=1+2=3

Also Fib(4)=Fib(2)+Fib(3)=2+3=5

Also Fib(5)=Fib(3)+Fib(4)=3+5=8

Write a header file which that you will call Fibonacci.h. The file contains a recursive method Fib with one parameter of type long and returns the Fibonacci value. Remember to use long as a data type for the returned fibonacci result by the method Fib because the number could be very large.

Write a C++ file that reads in a long number and returns the corresponding Fibonacci number. Remember to limit the number you read in to a value between 0 and 40.

And here's my coding for both files

(header file)

cpp

#include <iostream>

using namespace std;

int fib(int num)


{
if (num <= 1) {
return 1;
} else {
return fib(num-1)+fib(num-2);
}
}


(cpp file)

cpp

#include <iostream>
#include <cmath>
#include <iomanip>
#include "Fibonacci.h"

using namespace std;

//int Fibonacci(int, int, int);

int main()
{
cout << " enter a number" << endl;
int num;
cin >> num;
cout << fib(num) << endl;

cin.get();

return 0;
}


** Edit ** code.gif
User is offlineProfile CardPM

Go to the top of the page


KYA
post 5 Aug, 2008 - 07:00 PM
Post #2


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 2,932



Thanked 22 times

Dream Kudos: 1150
My Contributions


My initial question is, have you ran this code? If so, then you can determine whether or not it "works".

Your method/function does not take 3 parameters based on your code. It only takes one. You also include iostream twice which should generate a warning or error. I also think you are including way too much other stuff; its bloating up your program. If you don't need the library or file, don't include it.

You also don't have a check to verify use input is between 0 and 40. You also declared an int function when the directions explicitly state the value returned is to be of the long variable type. In any program/assignment:

1. Read the directions
2. Plan out code
3. Actually code
4. Test, debug and rework code
5. Repeat step 2 and 3 as needed
User is offlineProfile CardPM

Go to the top of the page

polymath
post 7 Aug, 2008 - 10:38 AM
Post #3


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 390



Thanked 4 times

Dream Kudos: 500
My Contributions


There are so many Fibonacci code snippets, just go into the "search" bar at the top of the page. Also, someone else came with the same exact assignment a couple of weeks ago, i forget who, maybe that thread could give you some ideas. *DISCLAIMER* I am not encouraging cheating, simply saying that you could see what kind of thing is needed.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 9/7/08 10:22PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month