Advertisement
dmilicev

diamond_paterns_v1.c

Oct 26th, 2023 (edited)
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. /*
  2.  
  3.     diamond_paterns_v1.c
  4.  
  5.  
  6.     You can find all my C programs at Dragan Milicev's pastebin:
  7.  
  8.     https://pastebin.com/u/dmilicev
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13.  
  14. // return current char to print diamond, ' ' or '*'
  15. char diamond_of_chars(int x, int y, int h) {
  16.     if(x < 0) x = -x;
  17.     if(y < 0) y = -y;
  18.     int value = h - x - y;
  19.     return (value <= 0) ? ' ' : '*';    // diamond of stars
  20. }
  21.  
  22. // return current number to print diamond
  23. char diamond_of_numbers(int x, int y, int h) {
  24.     if(x < 0) x = -x;
  25.     if(y < 0) y = -y;
  26.     int value = h - x - y;
  27.     return (value <= 0) ? ' ' : '0' + (char)value;  // diamond of numbers
  28. }
  29.  
  30. // return current char to print diamond, ' ' or '*'
  31. char empty_diamond_surrounded_by_stars(int x, int y, int h) {
  32.     if(x < 0) x = -x;
  33.     if(y < 0) y = -y;
  34.     int value = -h + x + y;
  35.     return (value <= 0) ? ' ' : '*';    // an empty diamond surrounded by stars
  36. }
  37.  
  38. // return current number to print diamond
  39. char empty_diamond_surrounded_by_numbers(int x, int y, int h) {
  40.     if(x < 0) x = -x;
  41.     if(y < 0) y = -y;
  42.     int value = -h + x + y;
  43.     return (value <= 0) ? ' ' : '0' + (char)value;  // an empty diamond surrounded by numbers
  44. }
  45.  
  46.  
  47. int main(void){
  48.     int x, y, h=4;
  49. /*
  50.     y   number of current row
  51.     x   number of chars in current row
  52.     h   number of rows in upper triangle
  53. */
  54.  
  55. /*
  56.     *
  57.    ***
  58.   *****
  59.  *******
  60.   *****
  61.    ***
  62.     *
  63. */
  64.     printf("\n Diamond of chars \n");
  65.     for(y = -h; y <= h; y++) {
  66.         for(x = -h; x <= h; x++)
  67.             putchar(diamond_of_chars(x, y, h));
  68.         putchar('\n');
  69.     }
  70.  
  71. /*
  72.     1
  73.    121
  74.   12321
  75.  1234321
  76.   12321
  77.    121
  78.     1
  79. */
  80.     printf("\n Diamond of numbers \n");
  81.     for(y = -h; y <= h; y++) {
  82.         for(x = -h; x <= h; x++)
  83.             putchar(diamond_of_numbers(x, y, h));
  84.         putchar('\n');
  85.     }
  86.  
  87. /*
  88. **** ****
  89. ***   ***
  90. **     **
  91. *       *
  92.  
  93. *       *
  94. **     **
  95. ***   ***
  96. **** ****
  97. */
  98.     printf("\n Empty diamond surrounded by stars \n\n");
  99.     for(y = -h; y <= h; y++) {
  100.         for(x = -h; x <= h; x++)
  101.             putchar(empty_diamond_surrounded_by_stars(x, y, h));
  102.         putchar('\n');
  103.     }
  104.  
  105. /*
  106. 4321 1234
  107. 321   123
  108. 21     12
  109. 1       1
  110.  
  111. 1       1
  112. 21     12
  113. 321   123
  114. 4321 1234
  115. */
  116.     printf("\n Empty diamond surrounded by numbers \n\n");
  117.     for(y = -h; y <= h; y++) {
  118.         for(x = -h; x <= h; x++)
  119.             putchar(empty_diamond_surrounded_by_numbers(x, y, h));
  120.         putchar('\n');
  121.     }
  122.  
  123.     printf("\n\n");
  124.     return 0;
  125. } // main()
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement