Help with recursion


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Help with recursion

  1. #1
    Join Date
    Dec 2005
    Posts
    2

    Help with recursion

    Being new to java, I'm having some troubles with recursion.

    I'm trying to do this problem

    Write a recursive method (it should accept the int as an argument) to print the following geometric pattern:


    #
    # #
    # # #
    # # # #
    # # # # #


    My basis start point is :

    public void printAsterrick(int N)
    {
    if (N > 1)
    {
    System.out.println("#");
    printAsterrick(N, N - 1);
    }
    else
    System.out.println("");
    }



    I'm looking on how to keep the astericks to form the required pattern (above)



    Any help or direction would be greatly appericated...thanks..

  2. #2
    Join Date
    Dec 2005
    Posts
    21
    For starters, you'll need to seperate the print lines from when you print "# ". Secondly, this could be very tricky if you're not allowed to use loops at all, and only one recursive method with one variable. If that wasn't the requirement, you would start at n=1, and recursively call for n increasing to n=5, where you have a condition that prevents the method from being run again.

    Although I doubt this is beyond the scope of your study, if the initial condition is to use only one recursive method with one variable, you could be sneaky and use the variable like two integers, as you can use a different range (like negative and positive numbers as ranges), and then make multiple recursive calls in one statement, treating positive and negative numbers different. Otherwise I think you'd have to be very cheap and write out each line and then use a big if/else or switch statement.

    To demonstrate recursion better though, you might want to use two recursive methods to achieve this program, or use two variables in the recursive method.

    I'd recommend learning LISP if you want to think more recursively.

  3. #3
    Join Date
    Oct 2005
    Posts
    107
    It would be quite difficult to do this with only one parameter as smartr said. But here is how I'd do it. Hope this helps you understand recursion a little better...

    Code:
    public void printAsterrick(int N){
      printAsterrickHelp(1, N);
    }
    
    public void printAsterrickHelp(int N, int max)
    {
    if (N > 0 && N <= max)
    {
    for(int i = 0; i < N; ++i)
      System.out.print("# ");
    
    System.out.println();
    printAsterrickHelp(N + 1, max);
    }
    return;
    }
    Last edited by Joe Beam; 12-12-2005 at 01:49 PM.

  4. #4
    Join Date
    Dec 2005
    Posts
    21
    You shouldn't need to use any loops with recursion. The max variable isn't neccesary, you can hard code that into the if statement. The for loop is going back to structured programming. You could replace that with another recursive method, or integrate it into the current method.

    so if you were going to do:
    Code:
    for (int i = 0; i < 5; i++) {
       System.out.print("# ");
    }
    
    you could instead use a method:
    
    void printSpacers(int n) {
       if (n > 0) {
          System.out.print("# ");
          printSpacers(n-1);
       }
    }

  5. #5
    Join Date
    Dec 2005
    Posts
    2
    Hey thanks for you guys help. About just using recursion... to actuall solve the problem I figured that I would need need a loop within the function and then call it recursively..... so the recursion should be the loop delimeter.

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Recursion is the bane to more people than just those "new to java". I've studied programming and computer science for many years (as well as many layers of mathematics) and recursion is tough for me. Allow me to use an iterative approach almost any day!

    "True" recursion does not involve the use of a loop beyond the "if statement" which is where you often test for the base case. The recursive call to the method does the work ... usually "walking out" to the base case and then "walking back" (performing the function of the method using values stored on the run-time stack) doing the actual work in the return to the original proposition. This approach is used for methods beside calculating the sum of values in a collection or factorial - it is used for graph traversals, too.

  7. #7
    Join Date
    Oct 2005
    Posts
    107
    okay this is more recursive in nature...

    Code:
    public String printAsterrick(int N){
    		  return printAsterrickHelp(1, N);
    		}
    
    	public String printAsterrickHelp(int N, int max){
    		if (N > 0 && N <= max)
    		{
    			return pad(N) + "\n" + printAsterrickHelp(N + 1, max);
    		}else{
    			return "";
    		}
    	}
    	public String pad(int N){
    		if(N > 0){
    			return "# " + pad(N-1);
    		}else{
    			return "";
    		}
    	}

  8. #8
    Join Date
    Dec 2005
    Posts
    21
    here is a way with pure recursion, while using one method that takes only one variable...
    Code:
    void masochist(int n) {
       if (n >= 6) System.out.println();
       else if (n > 0) {
          masochist(0-n);
          System.out.println();
          masochist(n+1);
       }
       else if (n < 0) {
          System.out.print("# ");
          masochist(n+1);
       }
    }

Similar Threads

  1. Replies: 9
    Last Post: 09-29-2005, 05:56 AM
  2. Replies: 7
    Last Post: 08-04-2005, 07:28 PM
  3. recursion
    By chad in forum Java
    Replies: 1
    Last Post: 06-22-2002, 05:09 PM
  4. the recursion of the Stored Procedure
    By huyun in forum Database
    Replies: 1
    Last Post: 06-27-2001, 08:47 AM
  5. Trigger Recursion
    By Suresh Tumkur in forum Database
    Replies: 0
    Last Post: 06-20-2001, 08:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center