Sign in to follow this  
l jsym l

Recursion

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 this post


Link to post
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 [i]lines[/i], 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 this post


Link to post
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 this post


Link to post
Share on other sites
[quote name='SyncViews' timestamp='1318365573' post='4871595']
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 "* ".
[/quote]

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 this post


Link to post
Share on other sites
Recursion is fun. Here's my first draft:

[code]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
}
}
[/code]

Share this post


Link to post
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 [b]n-1[/b] 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. [i]This is important, otherwise you will get a stack over flow. [/i]

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.



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this