Recursion to normal way
-
I've read a lot of articles believe me, I wouldn't ask for help re-writing the code if I didn't check every possible solution or article, that's why I'm asking for someone to re-write it to see how can I: 1- re-write recursion code to non-recursion code. 2- non-recursion solution for the knapsack problem.
iNoor72 wrote:
I'm asking for someone to re-write it
You keep saying this but you refuse to understand that nobody is going to do your work for you. Do you have any idea how times a day people show up here and ask to have someone do their homework for them? IT'S NOT GOING TO HAPPEN. You cannot just "rewrite this code as a for loop". That doesn't work at all. Forget the code you have and start over using the resources you've been given.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
I've read that any recursion program can be written in non-recursion manner unless there's dynamic allocation in that recursion method, and I want to apply that on this particular code because I tried doing it by myself but again, not having a good programming background made me stop and couldn't do it myself.
-
iNoor72 wrote:
not having a good programming background made me stop
So now would be a good time to learn programming properly, rather than trying to rewrite something that you do not understand.
Trying to evade the problem that you don't understand recursion by rewriting it to non-recursion is never going to work. In my university days, a fellow student realized that he did not fully master recursion - termination in particular. So he defined a small programming problem for himself. After solving the task, he never had any problems with how to terminate a recursion. I think he made an excellent "programming etude", and have spread it out to a lot of people. It goes like this: When you enter the recursive function, you write a line with the number of spaces given by the recursion depth, and then an asterisk. When you leave the recursive function, you do the same. The top level call gives parameters for (a) the maximum recursion depth, which you dive right into, (b) an intermediate recursion depth that you return to, before again recursing to the maximum depth, and (c) the number of times to recurse to the maximum depth and back to the intermediate level, before finally returning to the top level call (i.e. the number of "peaks"). For a call with arguments (5, 3, 3) the ouput should look something like
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*The problem statement seems very simple (and it is, for a seasoned recursionist). For an inexperienced programmer, you can usually hear a lot of cursing and re-cursing during the testing :-)
-
Trying to evade the problem that you don't understand recursion by rewriting it to non-recursion is never going to work. In my university days, a fellow student realized that he did not fully master recursion - termination in particular. So he defined a small programming problem for himself. After solving the task, he never had any problems with how to terminate a recursion. I think he made an excellent "programming etude", and have spread it out to a lot of people. It goes like this: When you enter the recursive function, you write a line with the number of spaces given by the recursion depth, and then an asterisk. When you leave the recursive function, you do the same. The top level call gives parameters for (a) the maximum recursion depth, which you dive right into, (b) an intermediate recursion depth that you return to, before again recursing to the maximum depth, and (c) the number of times to recurse to the maximum depth and back to the intermediate level, before finally returning to the top level call (i.e. the number of "peaks"). For a call with arguments (5, 3, 3) the ouput should look something like
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*The problem statement seems very simple (and it is, for a seasoned recursionist). For an inexperienced programmer, you can usually hear a lot of cursing and re-cursing during the testing :-)
-
Pass it up the recursion stack! ;P
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
No, it was meant as a side-by-side answer to yours, supporting what you wrote. So it was primarily meant for the originial poster. But it is a good exercise in recursion. Lots of (even experienced) programmers are touching recursion only occasionally, and then often in a quite simplistic way. So I propose it to any programmer. If you think it so trivial that you can do it ten minutes, then spend those ten minutes proving to yourself that you master recursion. I have met several experienced programmers who thought it would be simple and straightforward, but experienced it to be "somewhat more tricky" than they at first thought it would be.
-
I guess it's basically re-writing the code from this current way (the recursion method) to a for-loop way, I've nothing more to say tbh :laugh: If you can help me re-writing this code because I'm unfortunately not that good with programming at the moment, I'm still learning some basics.
iNoor72 wrote:
I'm still learning some basics.
And you'll be stuck there by continuing with this paradigm. Do the work yourself. Only ask for help after you have exhausted all other resources!
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
No, it was meant as a side-by-side answer to yours, supporting what you wrote. So it was primarily meant for the originial poster. But it is a good exercise in recursion. Lots of (even experienced) programmers are touching recursion only occasionally, and then often in a quite simplistic way. So I propose it to any programmer. If you think it so trivial that you can do it ten minutes, then spend those ten minutes proving to yourself that you master recursion. I have met several experienced programmers who thought it would be simple and straightforward, but experienced it to be "somewhat more tricky" than they at first thought it would be.
-
Very interesting, but the person that the message was meant for is unlikely ever to see it.
If only five other people read it, say "Hey, that I can do in ten minutes", go ahead to show it and they are still fiddeling around with termination conditions after half an hour, I think it has been worth it. You are probably right about the original poster, and nothing seems to indicate that he will be ready to take on the task for some time.
-
If only five other people read it, say "Hey, that I can do in ten minutes", go ahead to show it and they are still fiddeling around with termination conditions after half an hour, I think it has been worth it. You are probably right about the original poster, and nothing seems to indicate that he will be ready to take on the task for some time.
Member 7989122 wrote:
say "Hey, that I can do in ten minutes"
Making such statements is guaranteed to cause failure. I have done plenty of basic recursion functions but never anything very complicated. So I understand the concept, but the practical application always requires considerable thought.
-
Trying to evade the problem that you don't understand recursion by rewriting it to non-recursion is never going to work. In my university days, a fellow student realized that he did not fully master recursion - termination in particular. So he defined a small programming problem for himself. After solving the task, he never had any problems with how to terminate a recursion. I think he made an excellent "programming etude", and have spread it out to a lot of people. It goes like this: When you enter the recursive function, you write a line with the number of spaces given by the recursion depth, and then an asterisk. When you leave the recursive function, you do the same. The top level call gives parameters for (a) the maximum recursion depth, which you dive right into, (b) an intermediate recursion depth that you return to, before again recursing to the maximum depth, and (c) the number of times to recurse to the maximum depth and back to the intermediate level, before finally returning to the top level call (i.e. the number of "peaks"). For a call with arguments (5, 3, 3) the ouput should look something like
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*The problem statement seems very simple (and it is, for a seasoned recursionist). For an inexperienced programmer, you can usually hear a lot of cursing and re-cursing during the testing :-)
Member 7989122 wrote:
When you enter the recursive function, you write a line with the number of spaces given by the recursion depth, and then an asterisk. When you leave the recursive function, you do the same.
If the first value is 5 then the first line should be 5 spaces and then the asterisk, then 4 spaces asterisk ... (ignoring the second two parameters).
\* \*
*
*
*
*
*
*
*
*
* -
Trying to evade the problem that you don't understand recursion by rewriting it to non-recursion is never going to work. In my university days, a fellow student realized that he did not fully master recursion - termination in particular. So he defined a small programming problem for himself. After solving the task, he never had any problems with how to terminate a recursion. I think he made an excellent "programming etude", and have spread it out to a lot of people. It goes like this: When you enter the recursive function, you write a line with the number of spaces given by the recursion depth, and then an asterisk. When you leave the recursive function, you do the same. The top level call gives parameters for (a) the maximum recursion depth, which you dive right into, (b) an intermediate recursion depth that you return to, before again recursing to the maximum depth, and (c) the number of times to recurse to the maximum depth and back to the intermediate level, before finally returning to the top level call (i.e. the number of "peaks"). For a call with arguments (5, 3, 3) the ouput should look something like
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*The problem statement seems very simple (and it is, for a seasoned recursionist). For an inexperienced programmer, you can usually hear a lot of cursing and re-cursing during the testing :-)
-
iNoor72 wrote:
but I want someone to re-write the code in a normal form (For loops, while loops, etc...) for me, here's the code:
It doesn't work that way here. You would be better off asking specific questions, with as much detail as possible, and posting the appropriate code.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
1. pre>Determine the base case of the Recursion. Base case, when reached, causes Recursion to end. ... 2. Implement a loop that will iterate until the base case is reached. 3. Make a progress towards the base case. Send the new arguments to the top of the loop instead to the recursive method. Example:- UGC NET Mock Test & Books
-
I made my solution 35+ years ago, and the source might still exist on one of the approx 100 eight-inch floppy disks, with a proprietary formatting and a proprietary file system, that I'm still keeping in a box in my basement. The manufacturer went bankrupt in 1992. I think that we were programming in Pascal at that time, so I would have to obtain a Pascal compiler to verify the code. The cost&effort of recovering the source code (if it is available on one of the floppies, and the floppy is still readable) would be much larger than the cost of developing it anew. I could do it; you could do it yourself. The solution is really tiny, but the devil is in the details. Once the details are right, you'll say: Well, of course that's how it should be. I might give it a try (maybe 35+ years of coding experience will make it appear simpler), but I haven't got the time right now. Go ahead yourself! :-)
-
I made my solution 35+ years ago, and the source might still exist on one of the approx 100 eight-inch floppy disks, with a proprietary formatting and a proprietary file system, that I'm still keeping in a box in my basement. The manufacturer went bankrupt in 1992. I think that we were programming in Pascal at that time, so I would have to obtain a Pascal compiler to verify the code. The cost&effort of recovering the source code (if it is available on one of the floppies, and the floppy is still readable) would be much larger than the cost of developing it anew. I could do it; you could do it yourself. The solution is really tiny, but the devil is in the details. Once the details are right, you'll say: Well, of course that's how it should be. I might give it a try (maybe 35+ years of coding experience will make it appear simpler), but I haven't got the time right now. Go ahead yourself! :-)