Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Recursion Help

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Recursion Help

    I'm having problems finishing my java homework - please help
    I need to write a method that draws an upside down pattern based on a given string (what the pattern is made of), and an int n (how many lines it has). So far I've managed to get it to work, but only the "right side up." What am I doing wrong?
    This is the code I have now:
    public static void printPattern(String str, int n) {
            if (n < 1) {
                return;
            }
    	System.out.println(str);
            printPattern(str + str, n - 1);
        }
    Example: for printPattern("*", 4) I currently get:

    *
    **
    ****
    ********

    and I'm supposed to get:

    ********
    ****
    **
    *

    Please help. Thanks in advance!
    Last edited by helloworld922; April 1st, 2010 at 11:02 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Recursion Help

    Reversing recursive functions...think about order (aka order of calls)

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursion Help

    I'm not getting it. How would i reverse the order of calls for this piece of code?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Recursion Help

    put what you get into a stack and just pop stuff off the stack.

Similar Threads

  1. Program without loops and recursion
    By jayaram in forum Algorithms & Recursion
    Replies: 7
    Last Post: April 1st, 2010, 09:02 AM
  2. Converting a recursion to iteration
    By javaplus in forum Algorithms & Recursion
    Replies: 7
    Last Post: March 3rd, 2010, 05:38 PM
  3. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM
  4. Simple recursion logic
    By chronoz13 in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 24th, 2009, 10:53 PM
  5. Problems with recursion
    By KingLane in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 20th, 2009, 11:02 PM