Recursion

M

mathon

hi,

I wanted to solve a little problem with a recursion, so the number of
stars should be written to the output according to the parameter m and
n. I tried to implement it with a recursion but unfortunately, up to
now i only get this output:

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

but the other part

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

Code:
void triangle(ostream& outs, unsigned int m, unsigned int n)
{
  // Precondition: m <= n
  // Postcondition: The function has printed a pattern of 2*(n-m+1)
lines
  // to the output stream outs. The first line contains m asterisks,
the next
  // line contains m+1 asterisks, and so on up to a line with n
asterisks.
  // Then the pattern is repeated backwards, going n back down to m.
  /* Example output:
     triangle(cout, 3, 5) will print this to cout:
     ***
     ****
     *****
     *****
     ****
     ***
  */

	for(int i = 0; i < m; i++)
	{
		outs << "*";
	}
	outs << "" <<endl;

	if(m == n)
		return;

	triangle(outs, m+1,n);

}

Maybe anyone here knows what is missing here and/or went wrong...? :-/

matti
 
M

mathon

hi,

i finally got it working:

Code:
void triangle(ostream& outs, unsigned int m, unsigned int n)
{
  // Precondition: m <= n
  // Postcondition: The function has printed a pattern of 2*(n-m+1)
lines
  // to the output stream outs. The first line contains m asterisks,
the next
  // line contains m+1 asterisks, and so on up to a line with n
asterisks.
  // Then the pattern is repeated backwards, going n back down to m.
  /* Example output:
     triangle(cout, 3, 5) will print this to cout:
     ***
     ****
     *****
     *****
     ****
     ***
  */

	for(int i = 0; i < m; i++)
	{
		outs << "*";
	}
	outs << "" <<endl;

	if(m == n)
	{
		for(int i = 0; i < m; i++)
		{
			outs << "*";
		}
		outs << "" << endl;
		return;
	}

	triangle(outs, m+1,n);

	for(int i = 0; i < m; i++)
	{
		outs << "*";
	}
	outs << "" << endl;
}

Now my important question would be: Is this a good way to implement
this function or can i improve anything at this function...?

I appreciate every hint!

matti
 
A

Alf P. Steinbach

* (e-mail address removed):
hi,

i finally got it working:

Code:
void triangle(ostream& outs, unsigned int m, unsigned int n)
{
// Precondition: m <= n
// Postcondition: The function has printed a pattern of 2*(n-m+1)
lines
// to the output stream outs. The first line contains m asterisks,
the next
// line contains m+1 asterisks, and so on up to a line with n
asterisks.
// Then the pattern is repeated backwards, going n back down to m.
/* Example output:
triangle(cout, 3, 5) will print this to cout:
***
****
*****
*****
****
***
*/

	for(int i = 0; i < m; i++)
	{
		outs << "*";
	}
	outs << "" <<endl;

	if(m == n)
	{
		for(int i = 0; i < m; i++)
		{
			outs << "*";
		}
		outs << "" << endl;
		return;
	}

	triangle(outs, m+1,n);

	for(int i = 0; i < m; i++)
	{
		outs << "*";
	}
	outs << "" << endl;
}

Now my important question would be: Is this a good way to implement
this function or can i improve anything at this function...?

I appreciate every hint!

Start by removing the redundant output logic in the middle 'if'.
Instead, apply the 'if' to the recursive call. For that you need to
change the 'if' condition.

Then (but do the above first) perhaps get rid of the two remaining 'for'
loops. Hint: std::string( some arguments here ).

Having done all that you might tidy up the code by removing superflous
things.
 
M

mathon

Hmm...how do u mean that exactly...? - i need this if query, so the
recursion would stop and print one more time the n numbers of starts...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top