Advertisement
dmilicev

diamond_number_pattern_with_abs()_shortest_code.c

Apr 16th, 2021
1,567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.89 KB | None | 0 0
  1. /*
  2.  
  3.   diamond_number_pattern_with_abs()_shortest_code.c
  4.  
  5. Enter the number of rows of triangle above longest row: 3
  6.  
  7.  
  8. diamond_pattern_with_abs(h);
  9.  
  10. ---*---
  11. --***--
  12. -*****-
  13. *******
  14. -*****-
  15. --***--
  16. ---*---
  17.  
  18.  
  19. diamond_number_pattern_with_abs_1(h);
  20.  
  21. ---4---
  22. --343--
  23. -23432-
  24. 1234321
  25. -23432-
  26. --343--
  27. ---4---
  28.  
  29.  
  30. diamond_number_pattern_with_abs_2(h);
  31.  
  32. ---1---
  33. --212--
  34. -32123-
  35. 4321234
  36. -32123-
  37. --212--
  38. ---1---
  39.  
  40.  
  41. diamond_number_pattern_with_abs_3(h);
  42.  
  43. ---4---
  44. --345--
  45. -23456-
  46. 1234567
  47. -23456-
  48. --345--
  49. ---4---
  50.  
  51.  
  52. diamond_number_pattern_with_abs_31(h);
  53.  
  54. ---4---
  55. --543--
  56. -65432-
  57. 7654321
  58. -65432-
  59. --543--
  60. ---4---
  61.  
  62.  
  63. diamond_number_pattern_with_abs_4(h);
  64.  
  65. ---1---
  66. --222--
  67. -33333-
  68. 4444444
  69. -33333-
  70. --222--
  71. ---1---
  72.  
  73.  
  74. diamond_number_pattern_with_abs_5(h);
  75.  
  76. ---4---
  77. --333--
  78. -22222-
  79. 1111111
  80. -22222-
  81. --333--
  82. ---4---
  83.  
  84.  
  85. You can find all my C programs at Dragan Milicev's pastebin:
  86.  
  87. https://pastebin.com/u/dmilicev
  88.  
  89. */
  90.  
  91. #include<stdio.h>
  92.  
  93. /*
  94. ---*---
  95. --***--
  96. -*****-
  97. *******
  98. -*****-
  99. --***--
  100. ---*---
  101. */
  102. void diamond_pattern_with_abs( int h )
  103. {                   // h - number of rows of triangle above longest row
  104.     int row, col;
  105.     char c;         // the character by which the diamond is printed
  106.  
  107.     for (row=-h; row<=h; row++)
  108.     {
  109.         for (col=-h; col<=h; col++)
  110.         {
  111.             // calculate the current char to print diamond, '-' or '*'
  112.             // c = abs(col) + abs(row) <= h ? '*' : '-';  // or
  113.             if( abs(col) + abs(row) <= h )
  114.                 c = '*';
  115.             else
  116.                 c = '-';
  117.  
  118.             printf("%c",c);
  119.         }
  120.         printf("\n");   // new row
  121.     }
  122. }
  123.  
  124. /*
  125. ---4---
  126. --343--
  127. -23432-
  128. 1234321
  129. -23432-
  130. --343--
  131. ---4---
  132. */
  133. void diamond_number_pattern_with_abs_1( int h )
  134. {                   // h - number of rows of triangle above longest row
  135.     int num, row, col;
  136.  
  137.     for (row=-h; row<=h; row++)
  138.     {
  139.         num = 0;
  140.         for (col=-h; col<=h; col++)
  141.         {
  142.             if( col <= 0 )
  143.               num++;
  144.             else
  145.               num--;
  146.  
  147.             if( abs(col) + abs(row) <= h )
  148.                 printf("%d",num);
  149.             else
  150.                 printf("-");
  151.         }
  152.         printf("\n");   // new row
  153.     }
  154. }
  155.  
  156. /*
  157. ---1---
  158. --212--
  159. -32123-
  160. 4321234
  161. -32123-
  162. --212--
  163. ---1---
  164. */
  165. void diamond_number_pattern_with_abs_2( int h )
  166. {                   // h - number of rows of triangle above longest row
  167.     int num, row, col;
  168.  
  169.     for (row=-h; row<=h; row++)
  170.     {
  171.         num = h+2;
  172.         for (col=-h; col<=h; col++)
  173.         {
  174.             if( col <= 0 )
  175.               num--;
  176.             else
  177.               num++;
  178.  
  179.             if( abs(col) + abs(row) <= h )
  180.                 printf("%d",num);
  181.             else
  182.                 printf("-");
  183.         }
  184.         printf("\n");   // new row
  185.     }
  186. }
  187.  
  188. /*
  189. ---4---
  190. --345--
  191. -23456-
  192. 1234567
  193. -23456-
  194. --345--
  195. ---4---
  196. */
  197. void diamond_number_pattern_with_abs_3( int h )
  198. {                   // h - number of rows of triangle above longest row
  199.     int num, row, col;
  200.  
  201.     for (row=-h; row<=h; row++)
  202.     {
  203.         num = 0;
  204.         for (col=-h; col<=h; col++)
  205.         {
  206.               num++;
  207.  
  208.             if( abs(col) + abs(row) <= h )
  209.                 printf("%d",num);
  210.             else
  211.                 printf("-");
  212.         }
  213.         printf("\n");   // new row
  214.     }
  215. }
  216.  
  217. /*
  218. ---4---
  219. --543--
  220. -65432-
  221. 7654321
  222. -65432-
  223. --543--
  224. ---4---
  225. */
  226. void diamond_number_pattern_with_abs_31( int h )
  227. {                   // h - number of rows of triangle above longest row
  228.     int num, row, col;
  229.  
  230.     for (row=-h; row<=h; row++)
  231.     {
  232.         num = 2*h+2;
  233.         for (col=-h; col<=h; col++)
  234.         {
  235.             num--;
  236.  
  237.             if( abs(col) + abs(row) <= h )
  238.                 printf("%d",num);
  239.             else
  240.                 printf("-");
  241.         }
  242.         printf("\n");   // new row
  243.     }
  244. }
  245.  
  246. /*
  247. ---1---
  248. --222--
  249. -33333-
  250. 4444444
  251. -33333-
  252. --222--
  253. ---1---
  254. */
  255. void diamond_number_pattern_with_abs_4( int h )
  256. {                   // h - number of rows of triangle above longest row
  257.     int num, row, col;
  258.  
  259.     num=0;
  260.     for (row=-h; row<=h; row++)
  261.     {
  262.         if( row <= 0 )
  263.           num++;
  264.         else
  265.           num--;
  266.  
  267.         for (col=-h; col<=h; col++)
  268.         {
  269.             if( abs(col) + abs(row) <= h )
  270.                 printf("%d",num);
  271.             else
  272.                 printf("-");
  273.         }
  274.         printf("\n");   // new row
  275.     }
  276. }
  277.  
  278. /*
  279. ---4---
  280. --333--
  281. -22222-
  282. 1111111
  283. -22222-
  284. --333--
  285. ---4---
  286. */
  287. void diamond_number_pattern_with_abs_5( int h )
  288. {                   // h - number of rows of triangle above longest row
  289.     int num, row, col;
  290.  
  291.     num=2*h-1;
  292.     for (row=-h; row<=h; row++)
  293.     {
  294.         if( row <= 0 )
  295.           num--;
  296.         else
  297.           num++;
  298.  
  299.         for (col=-h; col<=h; col++)
  300.         {
  301.             if( abs(col) + abs(row) <= h )
  302.                 printf("%d",num);
  303.             else
  304.                 printf("-");
  305.         }
  306.         printf("\n");   // new row
  307.     }
  308. }
  309.  
  310. int main(void)
  311. {
  312.     int h = 3;      // h - number of rows of triangle above longest row of diamond
  313.  
  314.     printf("\n diamond_pattern_with_abs_1(%d) \n\n", h);
  315.     diamond_pattern_with_abs(h);
  316.  
  317.     printf("\n diamond_number_pattern_with_abs_1(%d) \n\n", h);
  318.     diamond_number_pattern_with_abs_1(h);
  319.  
  320.     printf("\n diamond_number_pattern_with_abs_2(%d) \n\n", h);
  321.     diamond_number_pattern_with_abs_2(h);
  322.  
  323.     printf("\n diamond_number_pattern_with_abs_3(%d) \n\n", h);
  324.     diamond_number_pattern_with_abs_3(h);
  325.  
  326.     printf("\n diamond_number_pattern_with_abs_31(%d) \n\n", h);
  327.     diamond_number_pattern_with_abs_31(h);
  328.  
  329.     printf("\n diamond_number_pattern_with_abs_4(%d) \n\n", h);
  330.     diamond_number_pattern_with_abs_4(h);
  331.  
  332.     printf("\n diamond_number_pattern_with_abs_5(%d) \n\n", h);
  333.     diamond_number_pattern_with_abs_5(h);
  334.  
  335.  
  336.     return 0;
  337. }
  338.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement