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

Write a C program that uses a recursive function to print a triangle pattern sim

ID: 3926796 • Letter: W

Question

Write a C program that uses a recursive function to print a triangle pattern similar to Homework 7 - Using a Static Local Variable. The function will take two integer arguments, the first argument tells it how many asterisk characters (*) and a space characters that should be printed by the function directly and the second argument tells it the desired size of the triangle. The triangle pattern should be largest in the middle row, thus two lines should be printed directly by functions that make recursive function calls. It may be desired to use another function to print the lines For example calling the function as shown below, would print the output shown below it. triangle(l, 5);

Explanation / Answer

#include "stdio.h"
void triangle(int,int);
void printLine(int,int);
int main(void) {
triangle(1,5);
return 0;
}
void triangle(int c,int n){
   static int flag=-1;
   if(flag<-1){ // base condition to break loop
       return;
   }
   else if(n<0){ // for printing second half
       printLine(c,flag--);
       triangle(c,n);
   }
   else{
       printLine(c,flag++); // for printing the first half
       triangle(c,n-1);
   }
  
}
// print a line of astricks
void printLine(int c,int n){
   int i,j;
   for(j=0;j<n;j++){
       for(i=0;i<c;i++){
           printf("*");
       }
       for(i=0;i<c;i++){
           printf(" ");
       }
   }
   printf(" ");
}

/*

sample output

  

*                                                                                                                                                                                                             

* *                                                                                                                                                                                                           

* * *                                                                                                                                                                                                         

* * * *                                                                                                                                                                                                       

* * * * *                                                                                                                                                                                                     

* * * *                                                                                                                                                                                                       

* * *                                                                                                                                                                                                         

* *                                                                                                                                                                                                           

*

*/

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