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
*****
****
***
Maybe anyone here knows what is missing here and/or went wrong...? :-/
matti
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