Ask for input in main, then pass the int entered as a parameter to the Asterisk function.
When n is passed into Asterisk(n), Asterisk(n) recursively calls Asterisk(n-1) after drawing a single row of asterisks.
The other important thing in recursion is a base case. In this case, it would be when n == 0, you're done making the triangle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void Asterisk(int n)
{
if (n <= 0)
{
return; // base case (you don't need two branches, but just for clarity)
}
else
{
// draw single row
// ...
Asterisk(n - 1); // recursively draw the remaining rows.
}
}
#include <iostream>
usingnamespace std;
void line( int n )
{
cout << ( n ? "* " : "\n" );
if ( n ) line( n - 1 );
}
void asterisk( int n )
{
if ( n ) line( n ), asterisk( n - 1 );
}
int main()
{
int n;
cout << "Number of rows: "; cin >> n;
asterisk( n );
}
Its probably the assignment point, to do it recursively.
it can probably condense even more than this but..
string s(num_rows, '*');
while(s.length())
{
cout << s << endl;
s.pop_back();
}