Hello im a student and this is homework so i just need help, thank you. My problem is with this fibonacci sequence, we need to write a recursive function that returns void. I think i pretty much understand recursion. Please help.

I initialized result=1 in the main function. For i, it is the value that the for loop is looping.

if(i<=2)
	{
		result+=1;
	}

	if(i>2)
	{
		(fibRecursive(i-1,result),fibRecursive(i-2,result));
	}

Do you mean something like this:

void fibonacci(int i, int & result)
{
    if(i < 2) result = 1;
    else
    {
        int prev, prev_prev;
        fibonacci(i - 1, prev); fibonacci(i - 2, prev_prev);
        result = prev + prev_prev;
    }

}

int main()
{
    int n;
    fibonacci(6, n);
    cout << n << endl;

    return 0;
}

?

commented: Nice job, you just did his homework for him. -4

No actually the code i had on there was right, the problem was with my for loop which i figured out several hours later:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.