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?
Printable View
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.
Look at my example, note:
1) You call the recursive function ONE time, only. After that ONE call in main(), the recursive function calls itself the correct number of times, because you set up the base case. That tells it when it should end.
Remove your for loop in main() which calls the recursive function.
2) The printing is done INSIDE the recursive function, not in main().
3) You need an if statement in the recursive function, to give it the logic when it should return. You don't have that if statement in yours yet.
That's not how functions work. You give them something (or some things), and they give you something back. For instance, if I do
I give the function the value 10, as input, and it gives me back the result, 2.302585 or so. So when you call recursion(6), you are giving the function 6, and you had better be expecting the number 728 back from it.Code:log(10)
Not good at all. I'm not getting the impression that you have actually tried any of the code you have posted, but instead just threw it up there willy-nilly hoping somebody would take pity and write out the solution for you. First off, your examples wont even compile, which tells me you basically put in zero effort. Second, you clearly didn't follow Adak's or my examples, both of which gave you examples of a recursive function. At the very least, I would have expected you to have a base case in there, since it's been mentioned so much and is in both examples, with a comment to draw your attention to it.
We all really want to help you, which is why we spend our spare time on this board. Most of us, however, have better stuff to do than write programs for someone else (okay, many of us do that for a living, but it's for a living). We need to see some serious effort from you if you want serious help from us. Read an article on recursion. Try out the examples Adak and I provided, then try tweaking them and see what happens to the values you get. Try modifying them to fit your need for your program.
If you still have trouble after that, come back with a serious effort, and some specific questions, and we will help you.
The first time you enter recursion, it does an orphaned math function then calls itself... and then calls itself again... and again.... there is nothing there to stop it from calling itself forever. (Which is why I avoid recursive algorythms like they have a disease... it's too easy to get a runaway.)
I found this thread relevant to your question about recursion...
Oh you jokers, Brewbuck and Quzah!
Thanks for the laughs. :p :p :D :D
A bit like if you go to Google and do a search for Recursion.