Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Recursion to normal way

Recursion to normal way

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structures
24 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K kalberts

    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 :-)

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #21

    I'd like to see the code.

    K 1 Reply Last reply
    0
    • J jeron1

      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

      A Offline
      A Offline
      Anujjndal01
      wrote on last edited by
      #22

      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

      1 Reply Last reply
      0
      • L Lost User

        I'd like to see the code.

        K Offline
        K Offline
        kalberts
        wrote on last edited by
        #23

        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! :-)

        L 1 Reply Last reply
        0
        • K kalberts

          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! :-)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #24

          Member 7989122 wrote:

          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.

          Same here, although coming up for 55 years. One day soon ... ;)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups