Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I write a recursive function in C++ to display a triangle of* like this:

ID: 3615215 • Letter: H

Question

How do I write a recursive function in C++ to display a triangle of* like this:
*
**
***
****
 
I am able to write a recursive function to display an inverted triangle like this:
****
***
**
*
 
The code for the inverted triangle function is:
  1. void inverted(int a)
  2. {
  3. if (a==1)
  4. {
  5.    cout<<"*"<<endl;
  6. }
  7. else
  8. {
  9.    for (int i=0; i<a; i++)
  10.    {
  11.    cout<<"*";
  12.    }
  13.    cout<<endl;
  14.    inverted(a-1);
  15. }
  16. }
 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.

For the uninitiated, a recursive function is a function that calls itself.

Please, provide some insight into the problem.
*
**
***
****
****
***
**
*
  1. void inverted(int a)
  2. {
  3. if (a==1)
  4. {
  5.    cout<<"*"<<endl;
  6. }
  7. else
  8. {
  9.    for (int i=0; i<a; i++)
  10.    {
  11.    cout<<"*";
  12.    }
  13.    cout<<endl;
  14.    inverted(a-1);
  15. }
  16. }
  1. void inverted(int a)
  2. {
  3. if (a==1)
  4. {
  5.    cout<<"*"<<endl;
  6. }
  7. else
  8. {
  9.    for (int i=0; i<a; i++)
  10.    {
  11.    cout<<"*";
  12.    }
  13.    cout<<endl;
  14.    inverted(a-1);
  15. }
  16. }
void inverted(int a) { if (a==1) {    cout<<"*"<<endl; } else {    for (int i=0; i<a; i++)    {    cout<<"*";    }    cout<<endl;    inverted(a-1); } }

Explanation / Answer

for(i=1;i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote