Recursive Triangle Function
I need some help on a problem. I have an assignment where we are asked to write 8 separate functions. However, I am also supposed to write an iteration version and a recursive version for each function. I have finished all of the iteration version functions and 6 of 8 recursive functions, but I am having difficulty with creating the last two recursively. So, I am asking for a little insight here.
These last two functions ask for a function that takes input from the user and uses that input to print a Triangle. The first function prints a triangle that stands on it's tip and the second one stands on its base.
For example:
Triangle 1 with user input 4 prints:
****
***
**
*
and Triangle 2 with user input 4 prints:
*
**
***
****
I have written the iteration version of these functions as follows and they work great.
Code:
/* Print a Triangle of Stars on it's tip
Input: a positive integer
Output: an n-sized triangle */
void Triangle(int s)
{
int i = 0;
while (s > 0)
{
for (i = 0; i < s; i++)
{
printf("*");
}
printf("\n");
s = s - 1;
}
}
/* Print a Triangle of Stars on it's base
Input: a positive integer
Output: an n-sized triangle */
void TriangleTwo(int t)
{
int i = 0;
int x = 0;
int y = 1;
int counter = 0;
while (counter < t)
{
x = t - (t - y);
for (i = 0; i < x; i++)
{
printf("*");
}
printf("\n");
y++;
counter++;
}
}
My problem is that I need to convert these functions from iterations into recursive functions, but I can't seem to get them to work. I am not looking for a solution, just a helping hand to point me on the right track to finding a solution.
Any help is greatly appreciated.
this is much simpler... enjoy
void mirror_triangle(int n)
{
if (n==1) printf("*");
else
{
printstar(n);
printf("\n");
mirror_triangle(n-1);
printf("\n");
printstar(n);
}
}
it prints (ex: n=5):
*****
****
***
**
*
**
***
****
*****
/*
you can use this recursion to print one line of stars instead of a loop:
void printstar(int n)
{
if (n==1) printf("*");
else
{
printstar(n-1);
printf("*");
}
}
*/