Public Group

# Recursion

This topic is 2660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, I'm fooling around with Recursion in JAVA. Anyways, I was just wondering if there is anyway to make this:
*
* *
* * *
* * * *
* * * * *
In java using recursion.
I can get it to print out:
*
**
***
****
*****
However I cant seem to figure out the spacing. Im kinda wanting to use two separate recursion functions. One for the stars and one for the spacing but cant seem to wrap my head around it

##### Share on other sites
That doesn't really make sense, one for the stars and one for the spaces between the stars?
Perhaps the instructions actually meant one recursive function to go through the lines, and then another one to print a * followed by a space as many time as necessary for that line. That would be more solvable I think if there is a requirement for 2 separate recursive functions...

##### Share on other sites
Is this a homework assignment or something and you're required to use 2 recursive functions, or are you just experimenting with it?

In either case, this problem can be solved more efficiently with an iterative solution, with <= 2 lines of code.

A good problem to solve using recursion would be a directory search.

Cheers

##### Share on other sites
Well every one except the last on the line is followed by a space, so if your using recursion or iteration, it should be fairly simple to make all but the last print "* ".

##### Share on other sites
for x in range(6): print "* " * x

but that's python..

Edit:

woops, heres one without the blank line before the asterisks:

for x in xrange(1, 6): print '* ' * x

##### Share on other sites

Well every one except the last on the line is followed by a space, so if your using recursion or iteration, it should be fairly simple to make all but the last print "* ".

It's also the case that every one except the first is preceded by a space, so you could add " *" to your string for each call, then lop off the initial space when it comes time to print (in C I'd just printf &mystring[1], don't know if that's possible in Java).

##### Share on other sites
Recursion is fun. Here's my first draft:

public class RecTest { static final String star = "* "; static void printRecStars(int from, int to) { if (!(from > 0 && from <= to)) return; if (from <= to) { StringBuilder sb = new StringBuilder(); buildLineOfStars(1, from, sb); System.out.println(sb.toString()); printRecStars(from+1, to); } } static void buildLineOfStars(int i, int n, StringBuilder sb) { if (i < n) { sb.append(star); buildLineOfStars(i+1, n, sb); } else { sb.append(star.charAt(0)); } } public static void main(String[] args) { printRecStars(1, 1); printRecStars(1, 5); printRecStars(10, 42); printRecStars(-5, -1); // not defined printRecStars(5, 1); // not defined } } 

##### Share on other sites
So, you want to print all that with recursion?

Well, without giving you code because it's something you need to figure out as you go, write out the steps in pseudo code.
You'll need to keep track of how many levels "deep" you go, also notice that you have n-1 spaces between the asterisks; Where n is the number of asterisks you'd like to display.

printAsterisk would require the following parameter: the count of how deep you want to go. This is important, otherwise you will get a stack over flow.

Now, basically, you print (note, create a string) your asterisk as well as the level - 1 stars, so if you're on level 1 you print nothing but asterisk and the new line; meanwhile on level 2, a star, a space, another star and then the new line.

I guess it helps to think each row as a separate string appended into one as you go back up the stack.
Keep this up until you reach your desired level.

Hint: % operator may be your friend. (if you want to place every odd / even ) character.

Hope this was clear enough for you.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634136
• Total Posts
3015756
×