0,2,8,26,80,242,728,2186,6560,19682....how can i write these series with recursion?
if we start k=0 and then return the function 3k+2 we can get that but I don't know how to turn this into code,can you help me about that?
0,2,8,26,80,242,728,2186,6560,19682....how can i write these series with recursion?
if we start k=0 and then return the function 3k+2 we can get that but I don't know how to turn this into code,can you help me about that?
All recursion can be broken up into two pieces:
A - the "base case" when you've reached the "bottom" (or the "beginning")
B - every other case, where you call the function with a smaller value, then manipulate that value as necessary.
Could you be more specific?
First of all, the mods here take a rather dim view of people using us to do their homework.
You really need to make your best effort on this, on your own, and if you run into problems post your code (in code tags please) and we'll see what we can do...
FWIW... you don't actually need recursion to solve this problem...
Mathematically
Edit: Just apply what tabstop said.Code:f(k) = 3 * f(k-1) + 2; k > 0 f(0) = 0;
Okey then I will give a shot that here and please help me to improve it
Now how about some assitment now?Code:main() { int i,k=0; printf("%d,",k); recursion(); printf("%d,",k); } recursion() { for(i=0;i<13;i++) { k=3k+2; return k; }
Recursion is when a function calls itself, meaning you enter function foo and foo also calls foo, unless the base case is true, which is the bottom. At the bottom, now you unwind all the way to the top, resolving all the statements that depended on previous calls of foo.
That's not recursion. Recursion is when a function calls itself. Your function is iterative and only calculates the value of the summation for k equal to 13. You don't calculate for k = 1, 2, ... 12. An example of recrusion would look like:
Put that together with tabstop's and Baying Naung's posts, and you should be able to come up with a solution.Code:void factorial(int x) { if (x == 0) // base case, i.e. stop the recursion return 1; else return x * factorial(x - 1); // recurse by calling foo with some other value }
This is the basic layout of a recursive function:
Code:#include <stdio.h> void recurse(int n); int main() { int n; n=0; recurse(n); printf("\n\n\t\t\t press enter when ready"); (void) getchar(); return 0; } void recurse(int n) { if (n>10) //the base case, return; printf("n = %d\n", n); recurse(n+1); //recursive calls //you don't need to change your variables inside the parameter parenthesis ( ) //having n+= 1; and then recurse(n) is equivalent. }
How about that now?Code:main() { int i,k=0; printf("%d,",k); for(i=0;i<13;i++){ recursion(k); printf("%d,",k); } } recursion(k) { k=3k+2; return recursion(k); }
That's ... pretty horrific. Your first step, which you seem to want to skip but can't, is to write down, in words, what should happen.
In other words, if I give you 6, you're supposed to give me 728 (if I'm reading your top post correctly and not off-by-one). How do you, yourself, get from 6 to 728?
you are not giving anything here...there is no interaction with user.