A recursive function

January 1, 2011 at 08:12:22
Specs: Windows Vista
I wanna write a program that use a recursive function to prints triangle shape
and ask the user to enter height of the triangle
I start and I wrote this is it right or not? what do you think?


void triangle(int n)
{
if (n==1)
{
// base case
cout << "*" << endl;
}
else
{
triangle(n-1); // draw top (which is a smaller
// triangle)
for (int i=0;i<2*n-1;i++) // draw bottom row of
// his triangle
cout << "*";
cout << endl;
}
}


See More: A recursive function

Report •

#1
January 1, 2011 at 08:42:03
It compiles and it runs. I'm not sure why you want twice the width - 1, but it's your call, and it does print a triangle.

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question