1 2 Previous Next 15 Replies Latest reply: Sep 17, 2006 6:59 PM by 807569 RSS

    Recursion

    807569
      Hi,

      I'm a second semester cs student and I am having difficulty finishing my recursion program. The problem is to: write a recursive method that takes as a parameter a non-negative integer and generatesthe following pattern of stars. If the non-negative integer is 4, then the pattern generated is:
      ****
      ***
      **
      *
      *
      **
      ***
      ****
      Seems simple and after awhile I was able to generate a method that generated the above (top half) of the pattern. Here's what I have so far:

      import java.util.*;

      public class Exercise1Chapt14 {

           static Scanner console = new Scanner(System.in);

           static int n = 0;

           public static void main(String[] args) {

                System.out.print("Please enter a non-negative integer: ");
                n = console.nextInt();

                genStarPattern(n);
           }

           public static void genStarPattern(int x) {

                if (x == 1) {
                     System.out.println("*");
                }

                else {
                     for (int i = 1; i <= x; i++)
                          System.out.print("*");
                     System.out.println();
                     genStarPattern(x - 1);
                }
           }
      }

      The problem I am having is generating the remaining (bottom) half. How do I reverse the above so it generates the bottom half? I was thinking along the lines of this formula: x-(x-1) to give me the value 1 less than the input value...but I'm stuck!

      Can someone point me in the right direction? Please?

      aiki
        • 1. Re: Recursion
          807569
          Sorry, first time on this forum...here's a better view of my code:
          import java.util.*;
          
          public class Exercise1Chapt14 {
          
               static Scanner console = new Scanner(System.in);
          
               static int n = 0;
          
               public static void main(String[] args) {
          
                    System.out.print("Please enter a non-negative integer: ");
                    n = console.nextInt();
          
                    genStarPattern(n);
               }
          
               public static void genStarPattern(int x) {
          
                    if (x == 1) {
                         System.out.println("*");
                    }
          
                    else {
                         for (int i = 1; i <= x; i++)
                              System.out.print("*");
                         System.out.println();
                         genStarPattern(x - 1);
                    }
               }
          }
          • 2. Re: Recursion
            807569
            The problem I am having is generating the remaining
            (bottom) half. How do I reverse the above so it
            generates the bottom half?
            You don't have to. It's kind of a tricky question.

            I was thinking along
            the lines of this formula: x-(x-1) to give me the
            value 1 less than the input value...but I'm stuck!
            Nah I think this is the wrong approach. It's actually simpler than you think.

            Can someone point me in the right direction?
            Hint: you can produce output more than once in any method invocation.
            Another hint: you can produce output both before and after the recursive call.

            By the way you don't need that if statement to see if x==1. The following loop should handle it.
            • 3. Re: Recursion
              807569
              The problem I am having is generating the
              remaining
              (bottom) half. How do I reverse the above so it
              generates the bottom half?
              You don't have to. It's kind of a tricky question.
              I mean, you don't have to reverse the previous part to generate the second half. You can generate the second half using the same progression of recursive calls that you have now.
              • 4. Re: Recursion
                807569
                Here, let me give you one more hint. It might be easier to do what you need if you first rewrote your recursive method like this:
                public static void genStarPattern(int x) {
                        String output = "";
                     for (int i = 1; i <= x; i++)
                          output += "*";
                     System.out.println(output);
                     genStarPattern(x - 1);
                }
                • 5. Re: Recursion
                  807569
                  hi ,

                  your Solution is best , but you can think as follows :
                  - why the num is non-negative ? - because if it negative then the solution
                  is simple as : abs( -4 ) , abs ( -3 ) , abs( -2 ) , abs( -1 ) , abs( 0 ) ,
                  abs( 1 ) , abs( 2 ) , abs( 3 ) , abs( 4 ) . and end.

                  and this solution can be solve just in two loop .
                  -why the Recursion ? - because it hard to solve and many problem with it,

                  what about your solution :

                  the solution you provide is very good but if you add another loop as the same one after the recursion call , just as Following :
                  import java.util.*;
                   
                  public class Exercise1Chapt14 {
                   
                       static Scanner console = new Scanner(System.in);
                   
                       static int n = 0;
                   
                       public static void main(String[] args) {
                   
                            System.out.print("Please enter a non-negative integer: ");
                            n = console.nextInt();
                   
                  genStarPattern(n);
                       }
                   
                       public static void genStarPattern(int x) {
                   
                            if (x == 1) {
                                 System.out.println("*");
                            }
                   
                            else {
                                 for (int i = 1; i <= x; i++)
                                      System.out.print("*");
                                 System.out.println();
                                 
                                                                                  genStarPattern(x - 1);
                                                                                
                                             for (int i = 1; i <= x; i++)
                                      System.out.print("*");
                                 System.out.println();
                  
                            }
                       }
                  }
                  see it carfully it may be the idea you want .
                  for more information about this field of topics see :
                  how to deal with Tree Traversal techniquies.

                  Regards.

                  Message was edited by:
                  darim

                  Message was edited by:
                  darim

                  Message was edited by:
                  darim
                  • 6. Re: Recursion
                    807569
                    That's good. Be very sure to deprive the OP the fun and insight that comes from figuring it out himself. The last thing we'd want is to encourage learning.
                    • 7. Re: Recursion
                      807569
                      Thanks for all the hints! Let me sit down with some scratch paper and see if i can visualize/trace what happens to print statements after a recursive method call.
                      • 8. Re: Recursion
                        807569
                        Thanks for input. I need to go through everything and make sure I understand what's happening.
                        • 9. Re: Recursion
                          807569
                          Excellent code! Of course if we wanted to extend the program to add another row of stars then all we need to do is copy and paste another loop.
                          </sarcasm>
                          • 10. Re: Recursion
                            807569
                            why i donot have any : Duke Dollars for my answer , why ????

                            null
                            • 11. Re: Recursion
                              807569
                              why i donot have any : Duke Dollars for my answer
                              , why ????


                              null
                              Because Duke Dollars are worthless.
                              • 12. Re: Recursion
                                807569
                                Darim, if you want your ego stroked by being awarded a few dukes, then I have a few spare you can have.

                                BTW, is that the only reason you provide help, sorry answers? To get your damn dukes.
                                • 13. Re: Recursion
                                  807569
                                  BTW what mean by this.
                                  • 14. Re: Recursion
                                    807569
                                    "BTW" == "by the way"
                                    1 2 Previous Next