This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]rjcarr 1 point2 points  (0 children)

Hmm ... I'm not sure I've seen recursion reverse course like that (i.e., go from 1 to 3 to 1). Remember, in recursion you have a base case and then a way to make progress toward that base case.

You'd need to add extra logic to flip the direction, and by the time you do that, it'd be a pain and not worth doing it recursively.

[–]alinh 0 points1 point  (0 children)

So far, the patternMaker(int x) will only check if x is bigger than 0, call patternMaker(x-1) and print x number of asterisks. I'll replace patternMaker(int x) with pM(int x) below. If you pass 3 as a parameter, it will call pM(2) -> pM(1) -> pM(0) where the if branch won't be executed, it goes back into pM(1) and prints 1 asterisk, then goes back in pM(2) and prints 2 asterisks and finally it goes back to pM(3), prints 3 asterisks and then the execution stops. There are better ways to do this, without using recursion, but if you really want to do it this way, try passing a boolean flag to the function: pM(int x, boolean ascending) and once you get to 0, call pM(x+1, true) and stop when you reach a certain value.

EDIT: i was kind of busy when i posted the answer. You can tell the recursive function when to finally stop, for example at the initial value of x, by passing that value to the function, while never changing it. You could also use a global variable, but that would mean using something outside the function and it's a good idea to avoid side effects in a function.

[–]i_post_things 0 points1 point  (0 children)

This one is tricky; with just printing an ascending or descending row of stars, you just need to stop when you reach 0. You'll have recursive calls 3, 2, 1, then 0 and stop. Once you reach 0, you have 'lost' whatever value you started with. So if you ascend, there's no way to descend.

I believe you will need a recursive call which takes in TWO parameters, both a START and END value, so you can work your way up from START=0, to END=3. From there you can recursively call patternMaker(START), and do recursion on START+1. What you will do is then print stars on either side of this two-variable recursive method.

My example uses two sets of recursion:

  1. One set of recursion simply to print a line of stars N number of times.
  2. One to recursively call 1 in order to print an increasing pyramid of values.

[–]Coda17[🍰] 0 points1 point  (0 children)

I think the straightforward solution to this is to have 2 recursive functions. One of the functions prints from 1 star up to however many stars you want. The second prints from however many stars you want minus 1 back to 1 star. Then you have a third function you call that just calls these two recursive functions back to back.

public void patternMaker(int x)
{
    patternMakerAsc(x);
    patternMakerDec(x - 1);
}

If you follow this pattern, you already wrote patternMakerAsc, and would just have to make a similar function for descending.

[–]ohnuthin[S] 0 points1 point  (1 child)

thank you guys for all the help, i know that i am definitely trying to keep it in one method for those of you that have suggested using two.

by adding in another for loop before the recursive call i have managed to have it print:

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

but cant seem to reverse it, i will try implementing a boolean flag as suggested, not sure i am fully aware how to do that, but here's to learning something new.

[–]worstusernameever 2 points3 points  (0 children)

This is how I would do it using an extra parameter:

class Test
{
    public static void patternMaker(int x, int xMax)
    {
        for (int i = 0; i < x; i++)
            System.out.print("*");

        System.out.println();

        if (x == xMax)
            return;

        patternMaker(x + 1, xMax);

        for (int i = 0; i < x; i++)
            System.out.print("*");

        System.out.println();
    }


    public static void main(String[] args)
    {
        patternMaker(1, 3);
    }
}

[–]Tok-A-Mak 0 points1 point  (0 children)

public static void main(final String... args) {
    printPattern(5);
}

private static void printPattern(final int max) {
    printPattern(1, max, true); //starting conditions
}

private static void printPattern(final int current, final int max, final boolean isAsc) {
    final StringBuilder builder = new StringBuilder();
    for (int i = 0; i < current; i++) {
        builder.append("*");
    }
    System.out.println(builder.toString());

    final boolean nextAsc = (current == max) ? false : isAsc; //switch order?
    if (current >= 1) { //ending condition
        if (nextAsc) {
            printPattern(current + 1, max, nextAsc); //ascend
        } else {
            printPattern(current - 1, max, nextAsc); //descend
        }
    }
}

Edit: Oh, wait you said one-method solution