Write a C program that uses a recursive function to print a

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);

Solution

#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

  

*                                                                                                                                                                                                             

* *                                                                                                                                                                                                           

* * *                                                                                                                                                                                                         

* * * *                                                                                                                                                                                                       

* * * * *                                                                                                                                                                                                     

* * * *                                                                                                                                                                                                       

* * *                                                                                                                                                                                                         

* *                                                                                                                                                                                                           

*

*/

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site