I started practicing some C and found this nice exercise when I need to print a triangle using input. for input 6 he will print
*
**
***
****
*****
******
*****
****
***
**
*
Now, looking at this, I thought it wasn’t such a difficult task. So I decided to try and write it using recursion, without loops and only 2 variables. the function should look like this:
void PrintTriangle(int iMainNumber, int iCurrNumber)
{
}
After a few hours, I realized that this is much more complicated than I thought, since I need to pass enough information so that the function can “remember” how many triangles should print.
So, now I decided to ask you if this is possible.
(remember, no loops, no other functions, only recursion).
EDIT:
, - . , , .
void PrintTriangle(int iMainNumber, int iCurrNumber)
{
if (iMainNumber == 0)
{
printf("\r\n");
}
else if (iMainNumber == iCurrNumber)
{
printf("\r\n");
PrintTriangle(iMainNumber - 1, 0);
}
else
{
printf("%s", MYCHAR);
PrintTriangle(iMainNumber, iCurrNumber + 1);
}
}
, , , , , iMainNumber iCurrNumber , .
, , , , .
, 2 .
, 2 , , , , .
, , , .
, .