Advertisement
a3f

2n-1 recursive pyramid

a3f
Jan 27th, 2014
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. /* required is a function star (int n) that outputs n lines following this pattern:
  2. *
  3. ***
  4. *****
  5. *******
  6. .. etc, no loops or secondary functions allowed
  7. */
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. void star(int rows)
  12. {
  13.     if (rows == 0) return;
  14.    
  15.     static int b =1;
  16.     static char *stars;
  17.     if (b)
  18.     {
  19.         b=0;
  20.         stars = malloc(rows*rows+1);
  21.         strcpy(stars, "*");
  22.     }
  23.     star(rows-1);
  24.     printf("%s\n", stars);
  25.    
  26.     stars = strcat(stars, "**");
  27. }
  28. int main (void)
  29. {
  30. star(5);
  31. return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement