Fibonacci Sequence

I recently got a hw assignment to create a program that calculates Fibonacci Sequence using a function. I've had problems with passing paremeters in the past so it's been difficult to figure out.Any help would be appreciated, Thanks!
Show us what you've tried.
Here's a tutorial on functions: http://cplusplus.com/doc/tutorial/functions/
#include<iostream>
using namespace std;

//Function prototype
int fibonacci(int num,int next,int first,int second)

int main()
{

//local variables
int num; //holds number to calc factorial provided by the user

int first = 0;
int second = 1;
int next;

//prompt the user to enter a number of Fibonacci numbers to calculate and print
cout << "Please enter the number of terms to be printed in a fibonacci sequence" << endl;
cin > num;
//keep the user in the loop until positive integer is entered
if (!num % 2 == 0)
{

cout<<"Error: Number not even"
}
else
{
cin >> num;
}


//Call the function to calculate and output requested number of Fibonacci sequence


fibonacci(num);


return 0;

}

int fibonacci(num,next,first,second)

/*
Pre:
Post:
Purpose: calculates and prints first n fibonacci numbers
*/

{
for (int i = 0; i < num; i++)
{
cout << first << endl;
next = first + second;
first = second;
second = next;
}

}
Last edited on
Thats what I have so far i dont know if its right
When posting code, use code tags: http://www.cplusplus.com/forum/articles/16853/

i dont know if its right

You could try to compile it, which would tell you it isn't quite right. The algorithm is close, but the function interface is wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Declaration
int fibonacci(int num);

// Definition
int fibonacci(int num)
{
    int first = 0, second = 1;
    for (int i = 0; i < num; i++)
    {
        cout << first << endl;
        int next = first + second;
        first = second;
        second = next;
    }
}

There isn't any point in ensuring that num is even in main, though, so I don't know what that's about.
You're right that the parameters are being passed wrong.

The purpose of your fibonacci function is to print the first n fibonacci numbers, so the only input to the function needs to be n, which is an int.

Also, factorials have nothing to do with fibonacci numbers, so the notes in your comment are a bit misleading.

Another misleading comment is the following:
1
2
    //keep the user in the loop until positive integer is entered
    if (!num % 2 == 0)
That's not how you check for a positive number, or division by 2.
Why does it need to be an even number?
Just have the check be if (num < 0) { /* error */ }

Your function prototype can just look like this:
1
2
//Function prototype
int fibonacci(int num);


The actual function can just look like:

1
2
3
4
5
6
7
int fibonacci(int num)
{
    int first = 0;
    int second = 1;
    int next;
    // logic here
}


Have first, second, next just be local variables.
Last edited on
Are you looking for help in calculating a Fibonacci number by function recursion or a function that does iteration? The code is different.
I've corrected it to look like this but when I type in an odd number it displays my error message, then continues to output the sequence anyway. How can I correct this?

(@Gando,I've tried doing what you said above but when I press enter nothing happens)




#include<iostream>
using namespace std;

//Function prototype
int fibonacci(int num);

int main()
{

//local variables
int num; //holds number to calc factorial provided by the user

int first = 0;
int second = 1;
int next;

//prompt the user to enter a number of Fibonacci numbers to calculate and print
cout << "Please enter the number of terms to be printed in a fibonacci sequence" << endl;
cin >> num;
//keep the user in the loop until positive integer is entered
if (!num % 2 == 0)
{

cout<<"Error: Number not even"<<endl;
}
else
{
cin >> num;
}


//Call the function to calculate and output requested number of Fibonacci sequence
fibonacci(num);


return 0;

}

int fibonacci(int num)
/*
Pre:
Post:
Purpose: calculates and prints first n fibonacci numbers
*/

{
int first = 0, second = 1;
for (int i = 0; i < num; i++)
{
cout << first << endl;
int next = first + second;
first = second;
second = next;
}

}
Never mind I figured it out. Thanks for all the help!
Topic archived. No new replies allowed.