PDA

View Full Version : Recursion help


Que
02-17-2009, 09:12 PM
I'm trying to get some recursion experience but having trouble starting up.

I want to print the following:


*
**
* //one space
****
*//3 spaces
**//3 spaces
*//4 spaces
******** //max stars printed here
*//5 spaces
**//5 spaces
*//6 spaces
****//5spaces
*//7 spaces
**//7 spaces
*//8 spaces


I apologize if my indentation is off slightly but I have left in comments as to how many spaces there should be before a * is printed.
The method gets passed along 2 ints, an indentation and a number that prints the max number of stars in the middle. The above output is made when you pass the method (0, 8).

Trouble is, I am not sure how to start the recursive call. I know i can split this problem into 3 parts, Top, Middle and Bottom.
Middle doesn't look like a problem, a simple for loop to print that # of stars.
But I am kind of confused as to how I should start this off so it prints the top part, let alone how to print the bottom. Any help is appreciated.

Old Pedant
02-18-2009, 02:02 AM
If you can't explain the algorithm, I don't see how we can help you write it.

Just for starters, how would you know WHEN to output these lines:
*
**
* //one space
****
*//3 spaces
**//3 spaces
*//4 spaces
******** //max stars printed here
*//5 spaces
**//5 spaces
*//6 spaces
****//5spaces
*//7 spaces
**//7 spaces
*//8 spaces

I, for one, can't make heads or tails of why you would output those lines in red at the points in time that you say you should.

Why max starts after [4 spaces one star]???
Why [0 spaces 4 stars] after [one space one star]???
Why [5 spaces 4 stars] after [6 spaces one star]???

THere's no rhyme or reason in that so far as I can see.

Old Pedant
02-18-2009, 02:04 AM
And why would you assume you would need/want recursion?

And why would you use a LOOP to print a given number of stars? (That is, why not just

String stars = "****************"
...
String ThreeStars = stars.substring(0,3); // just as an example

Que
02-18-2009, 03:12 AM
I got this problem from a coding book where the chapter talks about recursion. Unfortunately the book does not do a very good job of explaining recursion which is why I am a little lost.

EDIT: According to the problem, this method needs only 7 or 8 lines of code with 2 recursive calls.

Old Pedant
02-18-2009, 11:23 PM
Do they give an answer? That is, do they show the code???

If so, I'd be happy to explain it. But man as the problem is presented so far, I just don't really know what the *QUESTION* is!

*IF* we ignore the extraneous strings of stars and only need the lines with 1 or 2 stars, then it is easy:

void spacesAndStars( int spaces )
{
if ( spaces > 0 ) spacesAndStars( spaces - 1 );
if ( spaces == 0 )
{
OUT.writeline("*");
return;
}
OUT.writeline( spaces.substring(0,spaces-1) + "**" );
OUT.writeline( spaces.substring(0,spaces) + "*" );
}

// and then invoke it via something like
spaceAndStars(8);

I just used OUT as a generic name. You'd use whatever kind of output is available in the environment you are in.

But the clearly won't give the results you showed.

And that could just as clearly be written without recursion.