How To Convert a nested for Loop to Recursive












-3














Can anyone help me converting this for loop into a recursive method:
So far I added these two methods but I still want to change the second loop.
Thank you in advance.



       public void makeDesign1() {
int x;
for (int i = 0; i < 5; i++) // For loop is the one creating the rows
{
for (x = 4; x > i; x--) // Nested loop is the one creating the columns
{
System.out.print("*");
}
System.out.println();
}
System.out.println();

}

public static int makeDesign1Recur(int i) {

if (i == 0) {
return 0;
}
System.out.print("*");
return (makeDesign1Recur(i-1));
}
// How to convert this second loop recursive?
public static void makeDesignRow(int i){
for ( int x = i; x>=0; x--){
makeDesign1Recur(x);
System.out.println("");
}


}









share|improve this question




















  • 1




    Your inner for loop won't even execute, I think, because i has a max value of 4. You might want to add a problem statement to your question. There is nothing wrong with expressing a problem using two for loops, by the way.
    – Tim Biegeleisen
    Nov 12 '18 at 1:14






  • 1




    Please show your attempt and highlight where you are having problems. We won't do your work for you, but will help you if you show what you have done.
    – Erwin Bolwidt
    Nov 12 '18 at 1:15










  • it does work. It prints something like:
    – sannicko
    Nov 12 '18 at 1:16










  • something like this, a few starts on screen: **** *** ** *
    – sannicko
    Nov 12 '18 at 1:16










  • I need to convert the same for loop into recursive method.
    – sannicko
    Nov 12 '18 at 1:17
















-3














Can anyone help me converting this for loop into a recursive method:
So far I added these two methods but I still want to change the second loop.
Thank you in advance.



       public void makeDesign1() {
int x;
for (int i = 0; i < 5; i++) // For loop is the one creating the rows
{
for (x = 4; x > i; x--) // Nested loop is the one creating the columns
{
System.out.print("*");
}
System.out.println();
}
System.out.println();

}

public static int makeDesign1Recur(int i) {

if (i == 0) {
return 0;
}
System.out.print("*");
return (makeDesign1Recur(i-1));
}
// How to convert this second loop recursive?
public static void makeDesignRow(int i){
for ( int x = i; x>=0; x--){
makeDesign1Recur(x);
System.out.println("");
}


}









share|improve this question




















  • 1




    Your inner for loop won't even execute, I think, because i has a max value of 4. You might want to add a problem statement to your question. There is nothing wrong with expressing a problem using two for loops, by the way.
    – Tim Biegeleisen
    Nov 12 '18 at 1:14






  • 1




    Please show your attempt and highlight where you are having problems. We won't do your work for you, but will help you if you show what you have done.
    – Erwin Bolwidt
    Nov 12 '18 at 1:15










  • it does work. It prints something like:
    – sannicko
    Nov 12 '18 at 1:16










  • something like this, a few starts on screen: **** *** ** *
    – sannicko
    Nov 12 '18 at 1:16










  • I need to convert the same for loop into recursive method.
    – sannicko
    Nov 12 '18 at 1:17














-3












-3








-3







Can anyone help me converting this for loop into a recursive method:
So far I added these two methods but I still want to change the second loop.
Thank you in advance.



       public void makeDesign1() {
int x;
for (int i = 0; i < 5; i++) // For loop is the one creating the rows
{
for (x = 4; x > i; x--) // Nested loop is the one creating the columns
{
System.out.print("*");
}
System.out.println();
}
System.out.println();

}

public static int makeDesign1Recur(int i) {

if (i == 0) {
return 0;
}
System.out.print("*");
return (makeDesign1Recur(i-1));
}
// How to convert this second loop recursive?
public static void makeDesignRow(int i){
for ( int x = i; x>=0; x--){
makeDesign1Recur(x);
System.out.println("");
}


}









share|improve this question















Can anyone help me converting this for loop into a recursive method:
So far I added these two methods but I still want to change the second loop.
Thank you in advance.



       public void makeDesign1() {
int x;
for (int i = 0; i < 5; i++) // For loop is the one creating the rows
{
for (x = 4; x > i; x--) // Nested loop is the one creating the columns
{
System.out.print("*");
}
System.out.println();
}
System.out.println();

}

public static int makeDesign1Recur(int i) {

if (i == 0) {
return 0;
}
System.out.print("*");
return (makeDesign1Recur(i-1));
}
// How to convert this second loop recursive?
public static void makeDesignRow(int i){
for ( int x = i; x>=0; x--){
makeDesign1Recur(x);
System.out.println("");
}


}






java loops for-loop recursion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 3:17

























asked Nov 12 '18 at 1:12









sannicko

12




12








  • 1




    Your inner for loop won't even execute, I think, because i has a max value of 4. You might want to add a problem statement to your question. There is nothing wrong with expressing a problem using two for loops, by the way.
    – Tim Biegeleisen
    Nov 12 '18 at 1:14






  • 1




    Please show your attempt and highlight where you are having problems. We won't do your work for you, but will help you if you show what you have done.
    – Erwin Bolwidt
    Nov 12 '18 at 1:15










  • it does work. It prints something like:
    – sannicko
    Nov 12 '18 at 1:16










  • something like this, a few starts on screen: **** *** ** *
    – sannicko
    Nov 12 '18 at 1:16










  • I need to convert the same for loop into recursive method.
    – sannicko
    Nov 12 '18 at 1:17














  • 1




    Your inner for loop won't even execute, I think, because i has a max value of 4. You might want to add a problem statement to your question. There is nothing wrong with expressing a problem using two for loops, by the way.
    – Tim Biegeleisen
    Nov 12 '18 at 1:14






  • 1




    Please show your attempt and highlight where you are having problems. We won't do your work for you, but will help you if you show what you have done.
    – Erwin Bolwidt
    Nov 12 '18 at 1:15










  • it does work. It prints something like:
    – sannicko
    Nov 12 '18 at 1:16










  • something like this, a few starts on screen: **** *** ** *
    – sannicko
    Nov 12 '18 at 1:16










  • I need to convert the same for loop into recursive method.
    – sannicko
    Nov 12 '18 at 1:17








1




1




Your inner for loop won't even execute, I think, because i has a max value of 4. You might want to add a problem statement to your question. There is nothing wrong with expressing a problem using two for loops, by the way.
– Tim Biegeleisen
Nov 12 '18 at 1:14




Your inner for loop won't even execute, I think, because i has a max value of 4. You might want to add a problem statement to your question. There is nothing wrong with expressing a problem using two for loops, by the way.
– Tim Biegeleisen
Nov 12 '18 at 1:14




1




1




Please show your attempt and highlight where you are having problems. We won't do your work for you, but will help you if you show what you have done.
– Erwin Bolwidt
Nov 12 '18 at 1:15




Please show your attempt and highlight where you are having problems. We won't do your work for you, but will help you if you show what you have done.
– Erwin Bolwidt
Nov 12 '18 at 1:15












it does work. It prints something like:
– sannicko
Nov 12 '18 at 1:16




it does work. It prints something like:
– sannicko
Nov 12 '18 at 1:16












something like this, a few starts on screen: **** *** ** *
– sannicko
Nov 12 '18 at 1:16




something like this, a few starts on screen: **** *** ** *
– sannicko
Nov 12 '18 at 1:16












I need to convert the same for loop into recursive method.
– sannicko
Nov 12 '18 at 1:17




I need to convert the same for loop into recursive method.
– sannicko
Nov 12 '18 at 1:17












1 Answer
1






active

oldest

votes


















0














I see the first step as redefining makeDesign1() correctly. We want to pass in a size for our drawing. We also want to change the bounds slightly so a size of 1 draws one star, instead of none like the original:



public static void makeDesign(int n) 
{
for (int i = 0; i < n; i++) // For loop is the one creating the rows
{
for (int x = n; x > i; x--) // Nested loop is the one creating the columns
{
System.out.print("*");
}

System.out.println();
}

System.out.println();
}


The next step is to get both loops to count down to 1 to simplify the recursion when the time comes:



public static void makeDesign(int n) 
{
for (int i = n; i > 0; i--) // For loop is the one creating the rows
{
for (int x = i; x > 0; x--) // Nested loop is the one creating the columns
{
System.out.print("*");
}

System.out.println();
}

System.out.println();
}


Now we can simply convert each loop into it's own recursive function, one calling the other:



public static void makeDesign(int n) 
{
if (n > 0)
{
makeDesignRow(n);
makeDesign(n - 1);
}
else
{
System.out.println();
}
}

public static void makeDesignRow(int x)
{
if (x > 0)
{
System.out.print("*");
makeDesignRow(x - 1);
}
else
{
System.out.println();
}
}


OUTPUT



Passing makeDesign() an argument of 10, we get:



> java Main
**********
*********
********
*******
******
*****
****
***
**
*

>





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254846%2fhow-to-convert-a-nested-for-loop-to-recursive%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I see the first step as redefining makeDesign1() correctly. We want to pass in a size for our drawing. We also want to change the bounds slightly so a size of 1 draws one star, instead of none like the original:



    public static void makeDesign(int n) 
    {
    for (int i = 0; i < n; i++) // For loop is the one creating the rows
    {
    for (int x = n; x > i; x--) // Nested loop is the one creating the columns
    {
    System.out.print("*");
    }

    System.out.println();
    }

    System.out.println();
    }


    The next step is to get both loops to count down to 1 to simplify the recursion when the time comes:



    public static void makeDesign(int n) 
    {
    for (int i = n; i > 0; i--) // For loop is the one creating the rows
    {
    for (int x = i; x > 0; x--) // Nested loop is the one creating the columns
    {
    System.out.print("*");
    }

    System.out.println();
    }

    System.out.println();
    }


    Now we can simply convert each loop into it's own recursive function, one calling the other:



    public static void makeDesign(int n) 
    {
    if (n > 0)
    {
    makeDesignRow(n);
    makeDesign(n - 1);
    }
    else
    {
    System.out.println();
    }
    }

    public static void makeDesignRow(int x)
    {
    if (x > 0)
    {
    System.out.print("*");
    makeDesignRow(x - 1);
    }
    else
    {
    System.out.println();
    }
    }


    OUTPUT



    Passing makeDesign() an argument of 10, we get:



    > java Main
    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *

    >





    share|improve this answer


























      0














      I see the first step as redefining makeDesign1() correctly. We want to pass in a size for our drawing. We also want to change the bounds slightly so a size of 1 draws one star, instead of none like the original:



      public static void makeDesign(int n) 
      {
      for (int i = 0; i < n; i++) // For loop is the one creating the rows
      {
      for (int x = n; x > i; x--) // Nested loop is the one creating the columns
      {
      System.out.print("*");
      }

      System.out.println();
      }

      System.out.println();
      }


      The next step is to get both loops to count down to 1 to simplify the recursion when the time comes:



      public static void makeDesign(int n) 
      {
      for (int i = n; i > 0; i--) // For loop is the one creating the rows
      {
      for (int x = i; x > 0; x--) // Nested loop is the one creating the columns
      {
      System.out.print("*");
      }

      System.out.println();
      }

      System.out.println();
      }


      Now we can simply convert each loop into it's own recursive function, one calling the other:



      public static void makeDesign(int n) 
      {
      if (n > 0)
      {
      makeDesignRow(n);
      makeDesign(n - 1);
      }
      else
      {
      System.out.println();
      }
      }

      public static void makeDesignRow(int x)
      {
      if (x > 0)
      {
      System.out.print("*");
      makeDesignRow(x - 1);
      }
      else
      {
      System.out.println();
      }
      }


      OUTPUT



      Passing makeDesign() an argument of 10, we get:



      > java Main
      **********
      *********
      ********
      *******
      ******
      *****
      ****
      ***
      **
      *

      >





      share|improve this answer
























        0












        0








        0






        I see the first step as redefining makeDesign1() correctly. We want to pass in a size for our drawing. We also want to change the bounds slightly so a size of 1 draws one star, instead of none like the original:



        public static void makeDesign(int n) 
        {
        for (int i = 0; i < n; i++) // For loop is the one creating the rows
        {
        for (int x = n; x > i; x--) // Nested loop is the one creating the columns
        {
        System.out.print("*");
        }

        System.out.println();
        }

        System.out.println();
        }


        The next step is to get both loops to count down to 1 to simplify the recursion when the time comes:



        public static void makeDesign(int n) 
        {
        for (int i = n; i > 0; i--) // For loop is the one creating the rows
        {
        for (int x = i; x > 0; x--) // Nested loop is the one creating the columns
        {
        System.out.print("*");
        }

        System.out.println();
        }

        System.out.println();
        }


        Now we can simply convert each loop into it's own recursive function, one calling the other:



        public static void makeDesign(int n) 
        {
        if (n > 0)
        {
        makeDesignRow(n);
        makeDesign(n - 1);
        }
        else
        {
        System.out.println();
        }
        }

        public static void makeDesignRow(int x)
        {
        if (x > 0)
        {
        System.out.print("*");
        makeDesignRow(x - 1);
        }
        else
        {
        System.out.println();
        }
        }


        OUTPUT



        Passing makeDesign() an argument of 10, we get:



        > java Main
        **********
        *********
        ********
        *******
        ******
        *****
        ****
        ***
        **
        *

        >





        share|improve this answer












        I see the first step as redefining makeDesign1() correctly. We want to pass in a size for our drawing. We also want to change the bounds slightly so a size of 1 draws one star, instead of none like the original:



        public static void makeDesign(int n) 
        {
        for (int i = 0; i < n; i++) // For loop is the one creating the rows
        {
        for (int x = n; x > i; x--) // Nested loop is the one creating the columns
        {
        System.out.print("*");
        }

        System.out.println();
        }

        System.out.println();
        }


        The next step is to get both loops to count down to 1 to simplify the recursion when the time comes:



        public static void makeDesign(int n) 
        {
        for (int i = n; i > 0; i--) // For loop is the one creating the rows
        {
        for (int x = i; x > 0; x--) // Nested loop is the one creating the columns
        {
        System.out.print("*");
        }

        System.out.println();
        }

        System.out.println();
        }


        Now we can simply convert each loop into it's own recursive function, one calling the other:



        public static void makeDesign(int n) 
        {
        if (n > 0)
        {
        makeDesignRow(n);
        makeDesign(n - 1);
        }
        else
        {
        System.out.println();
        }
        }

        public static void makeDesignRow(int x)
        {
        if (x > 0)
        {
        System.out.print("*");
        makeDesignRow(x - 1);
        }
        else
        {
        System.out.println();
        }
        }


        OUTPUT



        Passing makeDesign() an argument of 10, we get:



        > java Main
        **********
        *********
        ********
        *******
        ******
        *****
        ****
        ***
        **
        *

        >






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 6:57









        cdlane

        17.2k21043




        17.2k21043






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254846%2fhow-to-convert-a-nested-for-loop-to-recursive%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation