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.
  • I Offline
    I Offline
    iNoor72
    wrote on last edited by
    #1

    Hello guys ! I just need a little help with this code, it's a recursion method to solve the 0-1 knapsack problem, but I want someone to re-write the code in a normal form (For loops, while loops, etc...) for me, here's the code:

    // 0-1 knapsack problem using Divide & Conquer
    #include #include using namespace std;

    // Values (stored in array v)
    // Weights (stored in array w)
    // Number of distinct items (n)
    // Knapsack capacity W
    int knapSack(int v[], int w[], int n, int W)
    {
    // Stopping conditions
    // Condition 1: Negative capacity
    if (W < 0)
    return INT_MIN;

    // Condition 2: No items left or capacity becomes 0
    if (n < 0 || W == 0)
    	return 0;
    

    // Cases of solving
    // Case 1: Take an item, use recursion for capacity (W - item's weight) and (No. of items - 1)
    int include = v[n] + knapSack(v, w, n - 1, W - w[n]);

    // Case 2. Take out the current item n from the knapsack. Use recursion for the remaining items (n - 1)
    int exclude = knapSack(v, w, n - 1, W);
    
    // return maximum value we get by including or excluding current item
    return max (include, exclude);
    

    }

    int main()
    {
    // Input: set of items each with a weight and a value
    // Change it to any numbers you wish
    int n;
    cout<<"Enter number of items:";
    cin>n;
    for (int i=0; i>v[i];
    cout<<"Enter the weight of item number "<< i <<"\n";
    cin>>w[i];
    }

    // Knapsack capacity, put it as you wish
    int W;
    cout<<"Enter the capacity of knapsack"<<"\\n";
    cin>>W;
    
    
    cout << "Knapsack value is " << knapSack(v, w, n - 1, W);
    
    return 0;
    

    }

    And I really appreciate all of your help :D

    J L K 3 Replies Last reply
    0
    • I iNoor72

      Hello guys ! I just need a little help with this code, it's a recursion method to solve the 0-1 knapsack problem, but I want someone to re-write the code in a normal form (For loops, while loops, etc...) for me, here's the code:

      // 0-1 knapsack problem using Divide & Conquer
      #include #include using namespace std;

      // Values (stored in array v)
      // Weights (stored in array w)
      // Number of distinct items (n)
      // Knapsack capacity W
      int knapSack(int v[], int w[], int n, int W)
      {
      // Stopping conditions
      // Condition 1: Negative capacity
      if (W < 0)
      return INT_MIN;

      // Condition 2: No items left or capacity becomes 0
      if (n < 0 || W == 0)
      	return 0;
      

      // Cases of solving
      // Case 1: Take an item, use recursion for capacity (W - item's weight) and (No. of items - 1)
      int include = v[n] + knapSack(v, w, n - 1, W - w[n]);

      // Case 2. Take out the current item n from the knapsack. Use recursion for the remaining items (n - 1)
      int exclude = knapSack(v, w, n - 1, W);
      
      // return maximum value we get by including or excluding current item
      return max (include, exclude);
      

      }

      int main()
      {
      // Input: set of items each with a weight and a value
      // Change it to any numbers you wish
      int n;
      cout<<"Enter number of items:";
      cin>n;
      for (int i=0; i>v[i];
      cout<<"Enter the weight of item number "<< i <<"\n";
      cin>>w[i];
      }

      // Knapsack capacity, put it as you wish
      int W;
      cout<<"Enter the capacity of knapsack"<<"\\n";
      cin>>W;
      
      
      cout << "Knapsack value is " << knapSack(v, w, n - 1, W);
      
      return 0;
      

      }

      And I really appreciate all of your help :D

      J Online
      J Online
      jeron1
      wrote on last edited by
      #2

      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

      I A 2 Replies Last reply
      0
      • I iNoor72

        Hello guys ! I just need a little help with this code, it's a recursion method to solve the 0-1 knapsack problem, but I want someone to re-write the code in a normal form (For loops, while loops, etc...) for me, here's the code:

        // 0-1 knapsack problem using Divide & Conquer
        #include #include using namespace std;

        // Values (stored in array v)
        // Weights (stored in array w)
        // Number of distinct items (n)
        // Knapsack capacity W
        int knapSack(int v[], int w[], int n, int W)
        {
        // Stopping conditions
        // Condition 1: Negative capacity
        if (W < 0)
        return INT_MIN;

        // Condition 2: No items left or capacity becomes 0
        if (n < 0 || W == 0)
        	return 0;
        

        // Cases of solving
        // Case 1: Take an item, use recursion for capacity (W - item's weight) and (No. of items - 1)
        int include = v[n] + knapSack(v, w, n - 1, W - w[n]);

        // Case 2. Take out the current item n from the knapsack. Use recursion for the remaining items (n - 1)
        int exclude = knapSack(v, w, n - 1, W);
        
        // return maximum value we get by including or excluding current item
        return max (include, exclude);
        

        }

        int main()
        {
        // Input: set of items each with a weight and a value
        // Change it to any numbers you wish
        int n;
        cout<<"Enter number of items:";
        cin>n;
        for (int i=0; i>v[i];
        cout<<"Enter the weight of item number "<< i <<"\n";
        cin>>w[i];
        }

        // Knapsack capacity, put it as you wish
        int W;
        cout<<"Enter the capacity of knapsack"<<"\\n";
        cin>>W;
        
        
        cout << "Knapsack value is " << knapSack(v, w, n - 1, W);
        
        return 0;
        

        }

        And I really appreciate all of your help :D

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

        iNoor72 wrote:

        but I want someone to re-write the code in a normal form (For loops, while loops, etc...)

        Good plan. There are some different ways to do that: - The "naive" way: iterate over all sets. That's easy, because sets can represented as integers, where bit `i` being set means "item `i` is in the set" and bit `i` not being set meaning "item `i` is not in the set". Iterating over ranges of integers, well, that's just a normal loop. Slow but simple. Simple in terms of code at least. There is a certain leap of logic to it (set=integer) that is not obvious for beginners. - The "mechanical" way: traverse the same recursion-tree, but with iteration and an *explicit stack*. That's a straightforward refactoring, just tricky (for a human) to get right. Hypothetically this refactoring could be done by a computer, so I call it mechanical - it doesn't require any insight in the Knapsack problem. - The "clever" way: fill an array iteratively, so that every result for a unique combination of `(n, W)` is only computed once, starting with the *bottom* of the recursion-tree and working up. The original algorithm will often reach the same state through different paths and then *recalculate the same thing*, that wasted work is avoided this way. This is also known as "Dynamic Programming", which is a far too fancy term for what it is.. Any way you go with, you should be able to make some sort of progress. If you can't finish it then you would have a real question to ask - or to look up. Knapsack has been done to death, there are already too many introductory articles about it, there have to be some that will help you.

        I 1 Reply Last reply
        0
        • I iNoor72

          Hello guys ! I just need a little help with this code, it's a recursion method to solve the 0-1 knapsack problem, but I want someone to re-write the code in a normal form (For loops, while loops, etc...) for me, here's the code:

          // 0-1 knapsack problem using Divide & Conquer
          #include #include using namespace std;

          // Values (stored in array v)
          // Weights (stored in array w)
          // Number of distinct items (n)
          // Knapsack capacity W
          int knapSack(int v[], int w[], int n, int W)
          {
          // Stopping conditions
          // Condition 1: Negative capacity
          if (W < 0)
          return INT_MIN;

          // Condition 2: No items left or capacity becomes 0
          if (n < 0 || W == 0)
          	return 0;
          

          // Cases of solving
          // Case 1: Take an item, use recursion for capacity (W - item's weight) and (No. of items - 1)
          int include = v[n] + knapSack(v, w, n - 1, W - w[n]);

          // Case 2. Take out the current item n from the knapsack. Use recursion for the remaining items (n - 1)
          int exclude = knapSack(v, w, n - 1, W);
          
          // return maximum value we get by including or excluding current item
          return max (include, exclude);
          

          }

          int main()
          {
          // Input: set of items each with a weight and a value
          // Change it to any numbers you wish
          int n;
          cout<<"Enter number of items:";
          cin>n;
          for (int i=0; i>v[i];
          cout<<"Enter the weight of item number "<< i <<"\n";
          cin>>w[i];
          }

          // Knapsack capacity, put it as you wish
          int W;
          cout<<"Enter the capacity of knapsack"<<"\\n";
          cin>>W;
          
          
          cout << "Knapsack value is " << knapSack(v, w, n - 1, W);
          
          return 0;
          

          }

          And I really appreciate all of your help :D

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

          Are you saying that you want the recursive solution rewritten into a non-recursive one? Then you have two main alternatives: Either find a completely different algorithm for solving the same task. Or do it by recursion, by maintaining your own recursion stack, e.g. in an array. The only reason for doing it that way would be because your programming language doesn't allow recursion - but I haven't seen that limitation since the days of Fortran :-) I'll admit you a secondary reason for being a little careful with recursion: If your estimate for maximum recursion depth is way off, you may experience a stack overflow. In principle, you have the same situation with your array-as-recursion stack, but as you explicitly move stack pointers (i.e. calculate array indexes), it will be far more visible to you, so that you can avoid a fatal crash. You may set up an exception handler for a "real" stack overflow as well, but usually you have a hard time getting back onto your feet with no loss of data or code flow.

          I 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

            I Offline
            I Offline
            iNoor72
            wrote on last edited by
            #5

            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.

            D 1 Reply Last reply
            0
            • L Lost User

              iNoor72 wrote:

              but I want someone to re-write the code in a normal form (For loops, while loops, etc...)

              Good plan. There are some different ways to do that: - The "naive" way: iterate over all sets. That's easy, because sets can represented as integers, where bit `i` being set means "item `i` is in the set" and bit `i` not being set meaning "item `i` is not in the set". Iterating over ranges of integers, well, that's just a normal loop. Slow but simple. Simple in terms of code at least. There is a certain leap of logic to it (set=integer) that is not obvious for beginners. - The "mechanical" way: traverse the same recursion-tree, but with iteration and an *explicit stack*. That's a straightforward refactoring, just tricky (for a human) to get right. Hypothetically this refactoring could be done by a computer, so I call it mechanical - it doesn't require any insight in the Knapsack problem. - The "clever" way: fill an array iteratively, so that every result for a unique combination of `(n, W)` is only computed once, starting with the *bottom* of the recursion-tree and working up. The original algorithm will often reach the same state through different paths and then *recalculate the same thing*, that wasted work is avoided this way. This is also known as "Dynamic Programming", which is a far too fancy term for what it is.. Any way you go with, you should be able to make some sort of progress. If you can't finish it then you would have a real question to ask - or to look up. Knapsack has been done to death, there are already too many introductory articles about it, there have to be some that will help you.

              I Offline
              I Offline
              iNoor72
              wrote on last edited by
              #6

              Could you implement any of these ways and show me how it's done? Because I understood some of the things you've said but the rest I couldn't tbh :laugh:

              L 1 Reply Last reply
              0
              • K kalberts

                Are you saying that you want the recursive solution rewritten into a non-recursive one? Then you have two main alternatives: Either find a completely different algorithm for solving the same task. Or do it by recursion, by maintaining your own recursion stack, e.g. in an array. The only reason for doing it that way would be because your programming language doesn't allow recursion - but I haven't seen that limitation since the days of Fortran :-) I'll admit you a secondary reason for being a little careful with recursion: If your estimate for maximum recursion depth is way off, you may experience a stack overflow. In principle, you have the same situation with your array-as-recursion stack, but as you explicitly move stack pointers (i.e. calculate array indexes), it will be far more visible to you, so that you can avoid a fatal crash. You may set up an exception handler for a "real" stack overflow as well, but usually you have a hard time getting back onto your feet with no loss of data or code flow.

                I Offline
                I Offline
                iNoor72
                wrote on last edited by
                #7

                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.

                L 1 Reply Last reply
                0
                • I iNoor72

                  Could you implement any of these ways and show me how it's done? Because I understood some of the things you've said but the rest I couldn't tbh :laugh:

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

                  As I wrote already, there are many articles about it. There are articles on *this very website*, for example [Knapsack Bitwise](https://www.codeproject.com/Articles/1140581/Knapsack-Bitwise). geeksforgeeks.org has articles about it. The wikipedia article is so complete that you can implement it straight from there. Medium.com is spammed full of Knapsack articles, just about any programmer transitioning from beginner to intermediate feels qualified to write them. You can read any of them, or any other articles about the subject, you don't need me - not yet. When you've tried it, and you still need help, then let's talk about it.

                  I 1 Reply Last reply
                  0
                  • L Lost User

                    As I wrote already, there are many articles about it. There are articles on *this very website*, for example [Knapsack Bitwise](https://www.codeproject.com/Articles/1140581/Knapsack-Bitwise). geeksforgeeks.org has articles about it. The wikipedia article is so complete that you can implement it straight from there. Medium.com is spammed full of Knapsack articles, just about any programmer transitioning from beginner to intermediate feels qualified to write them. You can read any of them, or any other articles about the subject, you don't need me - not yet. When you've tried it, and you still need help, then let's talk about it.

                    I Offline
                    I Offline
                    iNoor72
                    wrote on last edited by
                    #9

                    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.

                    D 1 Reply Last reply
                    0
                    • I iNoor72

                      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.

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • I iNoor72

                        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.

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

                        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.

                        K 1 Reply Last reply
                        0
                        • L Lost User

                          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.

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

                          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 3 Replies Last reply
                          0
                          • 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
                            #13

                            I don't think that message was meant for me.

                            P K 2 Replies Last reply
                            0
                            • L Lost User

                              I don't think that message was meant for me.

                              P Offline
                              P Offline
                              Peter_in_2780
                              wrote on last edited by
                              #14

                              Pass it up the recursion stack! ;P

                              Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

                              1 Reply Last reply
                              0
                              • L Lost User

                                I don't think that message was meant for me.

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

                                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.

                                L 1 Reply Last reply
                                0
                                • I iNoor72

                                  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.

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #16

                                  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

                                  1 Reply Last reply
                                  0
                                  • K kalberts

                                    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.

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

                                    Very interesting, but the person that the message was meant for is unlikely ever to see it.

                                    K 1 Reply Last reply
                                    0
                                    • L Lost User

                                      Very interesting, but the person that the message was meant for is unlikely ever to see it.

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

                                      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.

                                      L 1 Reply Last reply
                                      0
                                      • K kalberts

                                        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.

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

                                        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.

                                        1 Reply Last reply
                                        0
                                        • 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
                                          #20

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

                                           \*
                                          \*
                                          

                                          *
                                          *
                                          *
                                          *
                                          *
                                          *
                                          *
                                          *
                                          *

                                          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