recursion function

how to write a program that do this ? i need help please


{2*f(n-3) +f(n-2)+f(n-1) x>1}
{1 x=<1}

A. A: What is the result of this function for 6? Draw the tree recursive process generated in computing (g (6)).
B. B: write a recursion function for this function.
Last edited on
Hello Baraanashed,

I assume that in your description:
f(n) is equal to your funny brackets;
'x' actually means 'n';
g(6) actually means f(6);

You should write a function
1
2
3
4
int f( int n )
{
//
}


Inside your function you have an if-else structure so that if n is <= 1 you return 1 and if not you return 2*f(n-3) +f(n-2)+f(n-1). (These are the recursive function calls - just code them like that and let the computer take care of them.)

Apart from initially calling this function from main() that's about it.
Last edited on
Well, you just have to adjust your return type according to the data that comes through. Try to solve it by hand first and see how would you approach it then translate that into code.

Here's an example of a branched pure recursive function to help you out. As you see, it returns a different thing according to its arguments.

1
2
3
4
5
6
int f_ack(int n, int m)
{
   if(m==0) return n+1;
   else if(m>0 and n==0) return f_ack(m-1,1);
   else if(m>0 and n>0) return f_ack(m-1, f_ack(m,n-1));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int f( int n )
{
   if ( n <= 1 ) return 1;
   else          return 2 * f(n-3) + f(n-2) + f(n-1);
}

int main()
{
   int n;
   cout << "Input n: ";
   cin >> n;
   cout << "Result is " << f(n);
}
Topic archived. No new replies allowed.