Advertisement
dmilicev

diamond_numbers_pattern_v1.c

Oct 30th, 2023 (edited)
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.44 KB | None | 0 0
  1. /*
  2.  
  3.     diamond_numbers_pattern_v1.c
  4.  
  5.  Diamond star pattern with abs().
  6.  
  7.  n is the number of stars in the upper triangle of diamond.
  8.  
  9.  Diamond will have 2*n-1 raws.
  10.  
  11.  Example: n = 4
  12.  
  13. ------*-
  14. ----*-*-*-
  15. --*-*-*-*-*-
  16. *-*-*-*-*-*-*-
  17. --*-*-*-*-*-
  18. ----*-*-*-
  19. ------*-
  20.  
  21. row  spaces     *          r
  22.  
  23.  0.     6       1         -3
  24.  1.     4       3         -2
  25.  2.     2       5         -1
  26.  3.     0       7          0
  27.  4.     2       5          1
  28.  5.     4       3          2
  29.  6.     6       1          3
  30.  
  31. */
  32.  
  33. #include "stdio.h"
  34. #include "stdlib.h"     // for function abs()
  35.  
  36. /*
  37. ---*
  38. --***
  39. -*****
  40. *******
  41. -*****
  42. --***
  43. ---*
  44. */
  45. void diamond_of_stars_v11(int n){
  46. // n - number of rows in upper triangle of diamond
  47.  
  48.     int r, c;   // r - row, c - column
  49.  
  50.     for (r = -n+1; r < n; r++){                 // loop for rows
  51.         for (c = 0; c < abs(r); c++)            // print "-"
  52.             printf("-");
  53.  
  54.         for (c = 0; c < 2*(n-abs(r))-1; c++)    // print "*"
  55.             printf("*");
  56.  
  57.         printf("\n");                           // new row
  58.     }
  59. }
  60.  
  61. /*
  62. *********
  63. ****-****
  64. ***---***
  65. **-----**
  66. *-------*
  67. **-----**
  68. ***---***
  69. ****-****
  70. *********
  71. */
  72. void hollow_diamond_of_stars_v11(int n){
  73. // n - number of rows in upper triangle of diamond
  74.  
  75.     int r, c;   // r - row, c - column
  76.  
  77.     for (r = -n; r <= n; r++){                  // loop for rows
  78.         for (c = 0; c <= abs(r); c++)           // print "*"
  79.             printf("*");
  80.  
  81.         for (c = 0; c < 2*(n-abs(r))-1; c++)    // print "-"
  82.             printf("-");
  83.  
  84.         for (c = 0; c <= abs(r); c++)           // print "*"
  85.             if( (r==-n || r==n) && c > n-1 )
  86.                 ;                               // do nothing
  87.             else
  88.                 printf("*");
  89.  
  90.         printf("\n");                           // new row
  91.     }
  92. }
  93.  
  94. /*
  95. ------*-
  96. ----*-*-*-
  97. --*-*-*-*-*-
  98. *-*-*-*-*-*-*-
  99. --*-*-*-*-*-
  100. ----*-*-*-
  101. ------*-
  102. */
  103. void diamond_of_stars_v12(int n){
  104. // n - number of rows in upper triangle of diamond
  105.  
  106.     int r, c;   // r - row, c - column
  107.  
  108.     for (r = -n+1; r < n; r++){                 // loop for rows
  109.         for (c = 0; c < abs(r); c++)            // print "--"
  110.             printf("--");
  111.  
  112.         for (c = 0; c < 2*(n-abs(r))-1; c++)    // print "*-"
  113.             printf("*-");
  114.  
  115.         printf("\n");                           // new row
  116.     }
  117. }
  118.  
  119. /*
  120. * * * * * * * * *
  121. * * * * - * * * *
  122. * * * - - - * * *
  123. * * - - - - - * *
  124. * - - - - - - - *
  125. * * - - - - - * *
  126. * * * - - - * * *
  127. * * * * - * * * *
  128. * * * * * * * * *
  129. */
  130. void hollow_diamond_of_stars_v12(int n){
  131. // n - number of rows in upper triangle of diamond
  132.  
  133.     int r, c;   // r - row, c - column
  134.  
  135.     for (r = -n; r <= n; r++){                  // loop for rows
  136.         for (c = 0; c <= abs(r); c++)           // print "* "
  137.             printf("* ");
  138.  
  139.         for (c = 0; c < 2*(n-abs(r))-1; c++)    // print "- "
  140.             printf("- ");
  141.  
  142.         for (c = 0; c <= abs(r); c++)           // print "* "
  143.             if( (r==-n || r==n) && c > n-1 )
  144.                 ;                               // do nothing
  145.             else
  146.                 printf("* ");
  147.  
  148.         printf("\n");                           // new row
  149.     }
  150. }
  151.  
  152. /*
  153. ---1
  154. --121
  155. -12321
  156. 1234321
  157. -12321
  158. --121
  159. ---1
  160. */
  161. void diamond_of_numbers_v211(int n){
  162. // n - number of rows in upper triangle of diamond
  163.     int r, c, num;  // r - row, c - column, num - current number
  164.  
  165.     for (r = -n+1; r < n; r++){                 // loop for all rows of the diamond
  166.         for (c = 0; c < abs(r); c++)            // print blanks "-"
  167.             printf("-");
  168.  
  169.         num = 1;
  170.  
  171.         for (c = 0; c < 2*(n-abs(r))-1; c++){   // print numbers
  172.             if( c < n-abs(r)-1 )
  173.                 printf("%d", num++);
  174.             else
  175.                 printf("%d", num--);
  176.         }
  177.  
  178.         printf("\n");                           // new row
  179.     }
  180. }
  181.  
  182. /*
  183. ---4
  184. --434
  185. -43234
  186. 4321234
  187. -43234
  188. --434
  189. ---4
  190. */
  191. void diamond_of_numbers_v212(int n){
  192. // n - number of rows in upper triangle of diamond
  193.     int r, c, num;  // r - row, c - column, num - current number
  194.  
  195.     for (r = -n+1; r < n; r++){                 // loop for all rows of the diamond
  196.         for (c = 0; c < abs(r); c++)            // print blanks "-"
  197.             printf("-");
  198.  
  199.         num = n;
  200.  
  201.         for (c = 0; c < 2*(n-abs(r))-1; c++){   // print numbers
  202.             if( c < n-abs(r)-1 )
  203.                 printf("%d", num--);
  204.             else
  205.                 printf("%d", num++);
  206.         }
  207.  
  208.         printf("\n");                           // new row
  209.     }
  210. }
  211.  
  212. /*
  213. ------1-
  214. ----1-2-1-
  215. --1-2-3-2-1-
  216. 1-2-3-4-3-2-1-
  217. --1-2-3-2-1-
  218. ----1-2-1-
  219. ------1-
  220. */
  221. void diamond_of_numbers_v221(int n){
  222. // n - number of rows in upper triangle of diamond
  223.     int r, c, num;  // r - row, c - column, num - current number
  224.  
  225.     for (r = -n+1; r < n; r++){                 // loop for all rows of the diamond
  226.         for (c = 0; c < abs(r); c++)            // print blanks "--"
  227.             printf("--");
  228.  
  229.         num = 1;
  230.  
  231.         for (c = 0; c < 2*(n-abs(r))-1; c++){   // print numbers
  232.             if( c < n-abs(r)-1 )
  233.                 printf("%d-", num++);
  234.             else
  235.                 printf("%d-", num--);
  236.         }
  237.  
  238.         printf("\n");                           // new row
  239.     }
  240. }
  241.  
  242. /*
  243. ------1-
  244. ----1-2-1-
  245. --1-2-3-2-1-
  246. 1-2-3-4-3-2-1-
  247. --1-2-3-2-1-
  248. ----1-2-1-
  249. ------1-
  250. */
  251. void diamond_of_numbers_v222(int n){
  252. // n - number of rows in upper triangle of diamond
  253.     int r, c, num;  // r - row, c - column, num - current number
  254.  
  255.     for (r = -n+1; r < n; r++){                 // loop for all rows of the diamond
  256.         for (c = 0; c < abs(r); c++)            // print blanks "--"
  257.             printf("--");
  258.  
  259.         num = n;
  260.  
  261.         for (c = 0; c < 2*(n-abs(r))-1; c++){   // print numbers
  262.             if(c < n-abs(r)-1 )
  263.                 printf("%d-", num--);
  264.             else
  265.                 printf("%d-", num++);
  266.         }
  267.  
  268.         printf("\n");                           // new row
  269.     }
  270. }
  271.  
  272. /*
  273. ----------1
  274. -------2  3  4
  275. ----5  6  7  8  9
  276. 10 11 12 13 14 15 16
  277. ---17 18 19 20 21
  278. ------22 23 24
  279. ---------25
  280. */
  281. void diamond_of_numbers_v231(int n){
  282. // n - number of rows in upper triangle of diamond
  283.  
  284.     int r, c, num=1;   // r - row, c - column, num - current number
  285.  
  286.     for (r = -n+1; r < n; r++){                 // loop for rows
  287.         for (c = 0; c < 3*abs(r); c++)            // print "-"
  288.             printf("-");
  289.  
  290.         for (c = 0; c < 2*(n-abs(r))-1; c++)    // print current number
  291.             printf("%3d",num++);
  292.  
  293.         printf("\n\n");                           // new row
  294.     }
  295. }
  296.  
  297.  
  298. int main(void)
  299. {
  300.     int n=4;    // n - number of rows of the upper triangle of diamond
  301.  
  302.     printf("\n 1.1 \t n = %d \n\n", n);
  303.     diamond_of_stars_v11(n);
  304.  
  305.     printf("\n 1.1.1 \t n = %d \n\n", n);
  306.     hollow_diamond_of_stars_v11(n);
  307.  
  308.     printf("\n 1.2 \t n = %d \n\n", n);
  309.     diamond_of_stars_v12(n);
  310.  
  311.     printf("\n 1.2.1 \t n = %d \n\n", n);
  312.     hollow_diamond_of_stars_v12(n);
  313.  
  314.     printf("\n 2.1.1 \t n = %d \n\n", n);
  315.     diamond_of_numbers_v211(n);
  316.  
  317.     printf("\n 2.1.2 \t n = %d \n\n", n);
  318.     diamond_of_numbers_v212(n);
  319.  
  320.     printf("\n 2.2.1 \t n = %d \n\n", n);
  321.     diamond_of_numbers_v221(n);
  322.  
  323.     printf("\n 2.2.2 \t n = %d \n\n", n);
  324.     diamond_of_numbers_v222(n);
  325.  
  326.     printf("\n 2.3.1 \t n = %d \n\n", n);
  327.     diamond_of_numbers_v231(n);
  328.     return 0;
  329. }
  330.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement