How do I write a recursive function in C++ to display a triangle of * like this using a parameter size (e.g. 4 in the following example):

*
**
***
****

I am able to write a recursive function to display an inverted triangle like this:

****
***
**
*

The code for the inverted triangle function is:

void inverted(int a)
{
	if (a==1)
	{
		cout<<"*"<<endl;
	}
	else
	{
		for (int i=0; i<a; i++)
		{
			cout<<"*";
		}
		cout<<endl;
		inverted(a-1);
	}
}

I am allowed to use a loop (e.g. for loop) to print a row of * but I must use a recursive function to control the number of rows.

Please, provide some insight into the problem. I need it to work urgently but I am not able to figure out a solution.

Please note that the code displayed above is the one for an inverted triangle like this:

****
***
**
*

But I need the opposite of it, that is:

*
**
***
****

I don't know if the function inverted() can be modified into a function upright() easily to do this.

Do you want the one function to be able to output both shapes?

Do you want the one function to be able to output both shapes?

No, sfuo. There should be two different recursive functions in my program to output two types of triangles (as shown). I have been able to successfully code one of the two functions, i.e., the one called inverted() which outputs something like this parameter 4.

****
***
**
*

I still need another function that displays a triangle like this when the parameter is 4 (for example):

*
**
***
****

Does that parameter have to be 4? I dont think that is even possible without having a 2nd parameter. Or having the value set as 1 and the function has a cap of 4 in it.

Pretty much what I'm saying is how is the function going to know that when I put in 4 that I want 1 '*' on the 1st row and then add an extra '*' every time I decrease the value.

This is probably not what you want but this is the only way I can think of, at this hour, this being done doing recursion.

void upright(int a, int startedAt)
{
	if (a==1)
	{
		for( int i = 0; i < startedAt; i++ )
		{
			cout << "*";
		}
		cout << endl;
	}
	else
	{
		for (int i = a; i <= startedAt; i++)
		{
			cout<<"*";
		}
		cout<<endl;
		upright(a-1, startedAt);
	}
}

Hope it helps.

Every recursive function can be transformed into an iterative function by using a stack.

void inverted(int a)
{
	if (a!=-1)
	{
		for (int i=0; i<=a; i++)
		{
			cout<<"*";
		}
		cout<<endl;
		inverted(a-1);
	}
	for (int i=0; i<=a; i++)
	{
			cout<<"*";
	}
	cout << endl;
}

Take a look at this tutorial - http://www.cprogramming.com/tutorial/lesson16.html

The problem is that you don't know when to stop. Currently, you are using hard coded 0, but there is nothing to keep you from using an extra parameter in your function call:

void triangle(int max_size, int current_size = 1)
{
    for (int i=0; i<current_size; i++)
    {
        cout<<"*";
    }
    cout<<endl;

    if(current_size != max_size)
        triangle(max_size, current_size+1)
}

so then your main will end up like this:
int main()
{
int size = 5;
triangle(5); // short for triangle(5,1);
}

I was not aware that I could use two parameters in a function definition while I was calling it with only one.

sfuo, your function uses a parameter startedAt. Does it not need to be initialized with a value as monarch_dodra does in his code.

ruhul.kulshreshtha uses one parameter in his working code:

void inverted(int a)
{
	if (a==1)
	{
		cout<<"*"<<endl;
	}
	else
	{
		inverted(a-1);
		for (int i=0; i<a; i++)
		{
			
			cout<<"*";
		}
		cout<<endl;		
	}
}

Thanks, guys. I really appreciate the help and now it is time for me to figure out how individual methods of each user actually works.

Every recursive function can be transformed into an iterative function by using a stack.

void inverted(int a)
{
	if (a!=-1)
	{
		for (int i=0; i<=a; i++)
		{
			cout<<"*";
		}
		cout<<endl;
		inverted(a-1);
	}
	for (int i=0; i<=a; i++)
	{
			cout<<"*";
	}
	cout << endl;
}

Take a look at this tutorial - http://www.cprogramming.com/tutorial/lesson16.html

Hi, adatapost!
Thank you for the insight. I should get some concrete understanding of "iterative functions" and "stacks".

I totally didn't think of it like that. Use his code its way better =(

commented: Your post are very helpful. +6

I'm sure there are a lot of better answers than mine, just wanted to let you see it:

// i will be the limit to finish the recursion
// j will be the limit to paint the line
void triangle(int i, int j){
	if(j < i){ // If the limit has not been reached
		for(int a = 0; a < j; a++)
			cout << "*";
		cout << endl;
		triangle(i, j + 1); // Recall the function with line limit increased
	}
}

3 year old thread, but only noticed after already writing the below, muh!

#include <iostream>

using namespace std;

void PrintStarLine(const int size)
{
    if (size > 0)
    {
        cout << "*";
        PrintStarLine(size - 1);
    }
    else
    {
        cout << endl;
    }
}

void PrintStarTriangle(const int size)
{
    if (size > 0)
    {
        PrintStarLine(size);
        PrintStarTriangle(size - 1);
    }
}

int main ()
{
    PrintStarTriangle(10);

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.