- #1
sandy.bridge
- 798
- 1
Homework Statement
Hey, this isn't really homework, but my professor stated that any loop can be represented by a recursive function. Here is an example I made:
Following is a loop inside of a recursive function. I have it set up such that it takes n asterisks and prints them on a single line; prints a new line; prints n-1 asterisks; stops at n=0.
Code:
void printStars(int n)
{
if (n > 0)
{
for(int i = 0; i < n; i++)
{
cout << '*';
}
cout << endl;
return printStars(n-1);
}
return;
}
If printStars(5) is called, then
Output:
*****
****
***
**
*
If I get rid of the for loop and attempt the utilize a recursive function for printing that line, the issue I have is that upon printing the line, once a new line is printed, n=0 and the second line cannot be printed. For example:
Code:
void printStars(int n)
{
if(n > 0)
{
cout << '*';
return printStars(n-1);
}
cout << endl;
return;
}