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.

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

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.

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

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){ // …``````

## All 13 Replies

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

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 =(

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 learning and sharing knowledge.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7151815,"math_prob":0.919328,"size":675,"snap":"2021-43-2021-49","text_gpt3_token_len":179,"char_repetition_ratio":0.11773472,"word_repetition_ratio":0.0,"special_character_ratio":0.32296297,"punctuation_ratio":0.11971831,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.994097,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-21T18:18:15Z\",\"WARC-Record-ID\":\"<urn:uuid:d04245f6-f689-4a4c-bbd4-23972456cb3e>\",\"Content-Length\":\"111777\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:132518e7-dbc2-4560-b755-14a6962bd8db>\",\"WARC-Concurrent-To\":\"<urn:uuid:7f88e690-8735-480c-b64d-ce874c528cbb>\",\"WARC-IP-Address\":\"172.66.41.5\",\"WARC-Target-URI\":\"https://www.daniweb.com/programming/software-development/threads/239964/c-recursive-function-very-simple-one\",\"WARC-Payload-Digest\":\"sha1:FFOG6YOKQUZ7535CGZCVPEIUKEKB7OMZ\",\"WARC-Block-Digest\":\"sha1:CNR7HZJ7WA5CPBTA45DF2ANQV7X366Z6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585439.59_warc_CC-MAIN-20211021164535-20211021194535-00255.warc.gz\"}"}