Write a recursive function called DrawTriangle() that outputs lines of ‘‘ to form a right side up isosceles triangle. Function DrawTriangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first ‘‘ on the first line for correct formatting.

Hint: The number of ‘*’ increases by 2 for every line drawn.

Ex: If the input of the program is:

3
the function DrawTriangle() outputs:

     *
    ***

Ex: If the input of the program is:

19
the function DrawTriangle() outputs:

     *
    ***
   *****
  *******
 *********
***********




Note: No space is output before the first ‘*’ on the last line when the base length is 19.

I was able to make the triangle correctly with the following recursive function:

void DrawTriangle(int baseLength, int spaces = 0) {
   if(baseLength <= 0){
    return;  
   }  
   
   DrawTriangle(baseLength - 2, spaces + 1);
   
   for(int i = 0; i < spaces; i++){
    cout << ' ';  
   }   
   
  for(int i = 0; i < baseLength; i++){
   cout << '*';  
  } 
  cout << endl;
}

The problem I am having is that I have no idea how to get the correct spacing that is required, which is 9 spaces before the singular star

New contributor

user23998448 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.