Advertisement
dmilicev

number_triangle_pattern_from_top.c

Jan 24th, 2024 (edited)
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.65 KB | None | 0 0
  1. /*
  2.  
  3.     number_triangle_pattern_interesting.c
  4.  
  5. https://www.facebook.com/groups/3092811364081112/permalink/7605244639504406/
  6.  
  7. numbers           indexes           matrix
  8.  
  9. 1                 00                00 01 02 03 04
  10. 2 6               10 11             10 11 12 13 14
  11. 3 7 10            20 21 22          20 21 22 23 24
  12. 4 8 11 13         30 31 32 33       30 31 32 33 34
  13. 5 9 12 14 15      40 41 42 43 44    40 41 42 43 44
  14.  
  15. r - row
  16. c - column
  17.  
  18. There are n rows and at most n columns.
  19.  
  20. The number of columns in a row is the row number (r)
  21.  
  22. The number in the first  column is the row number (r)
  23.  
  24. The number in the n-th   column is ...
  25.  
  26. In order to calculate the corresponding number to be written
  27. in the place whose indices are r1,c1,
  28. we will use the indices of the virtual matrix M[n][n].
  29.  
  30. See separation_of_matrix_elements.c
  31. https://pastebin.com/7AbM4rB1
  32.  
  33. An square matrix M[r][c] of characters of dimension 5 x 5 is given.
  34. Separate various elements of matrix to an array.
  35.  
  36. r is index of rows.
  37. c is index of columns.
  38.  
  39. elements of the main diagonal have the property:    r = c
  40.  
  41.  
  42. elements above the main diagonal have the property: r < c
  43.  
  44.  
  45. elements of the first diagonal above the main diagonal have the property:      r-c = -1
  46.  
  47. elements of the second diagonal above the main diagonal have the property:     r-c = -2
  48.  
  49. elements of the third diagonal above the main diagonal have the property:      r-c = -3
  50.  
  51. the elements of the fourth diagonal above the main diagonal have the property: r-c = -4
  52.  
  53.  
  54. elements below the main diagonal have the property: r > c
  55.  
  56.  
  57. elements of the first diagonal below the main diagonal have the property:  r-c = 1
  58.  
  59. elements of the second diagonal below the main diagonal have the property: r-c = 2
  60.  
  61. elements of the third diagonal below the main diagonal have the property:  r-c = 3
  62.  
  63. elements of the fourth diagonal below the main diagonal have the property: r-c = 4
  64.  
  65.  
  66.  
  67.  
  68. elements of the side diagonal have the property: r+c = n-1
  69.  
  70.  
  71. elements above the side diagonal have the property: r+c < n-1
  72.  
  73.  
  74. elements of the first diagonal above the side diagonal have the property:  r+c = n-1
  75.  
  76. elements of the second diagonal above the side diagonal have the property: r+c = n-2
  77.  
  78. elements of the third diagonal above the side diagonal have the property:  r+c = n-3
  79.  
  80. elements of the fourth diagonal above the side diagonal have the property: r+c = n-4
  81.  
  82.  
  83. elements below the side diagonal have the property: r+c >= n
  84.  
  85.  
  86. elements of the first diagonal below the side diagonal have the property:  r+c = 5
  87.  
  88. elements of the second diagonal below the side diagonal have the property: r+c = 6
  89.  
  90. elements of the third diagonal below the side diagonal have the property:  r+c = 7
  91.  
  92. elements of the fourth diagonal below the side diagonal have the property: r+c = 8
  93.  
  94.  
  95.  
  96.  
  97.     You can find all my C programs at Dragan Milicev's pastebin:
  98.  
  99.     https://pastebin.com/u/dmilicev
  100.  
  101. */
  102.  
  103. #include <stdio.h>
  104.  
  105. /*
  106. numbers           indexes           matrix
  107. 1                 00                00 01 02 03 04
  108. 2 6               10 11             10 11 12 13 14
  109. 3 7 10            20 21 22          20 21 22 23 24
  110. 4 8 11 13         30 31 32 33       30 31 32 33 34
  111. 5 9 12 14 15      40 41 42 43 44    40 41 42 43 44
  112. */
  113. // counts elements up to element r1,c1
  114. int calculate_num(int n, int r1, int c1){
  115.     int r, c, num=0, fromWhichRow=0;
  116.  
  117.     for(c=0;c<=c1;c++){ // for all columns up to the column in which the requested element is
  118.         if(c<c1){
  119.             for(r=fromWhichRow;r<n;r++) // from which row to count elements
  120.                 num++;
  121.             fromWhichRow++;             // the next first row from which it is counted
  122.         }
  123.         else if(c==c1)  // for the column in which the requested element is
  124.             for(r=fromWhichRow;r<=r1;r++)
  125.                 num++;
  126.     }
  127.     return num;
  128. }
  129.  
  130. // print triangle of numbers
  131. void number_triangle_pattern_interesting(int n){
  132.     int r, c, num;          // r - row, c - column, num - number to be printed
  133.  
  134.     for(r=0;r<n;r++){       // for all rows
  135.         for(c=0;c<n;c++)    // for all columns
  136.             if(r>=c){       // elements on the main diagonal and below the main diagonal have a property r >= c
  137.                 num = calculate_num(n,r,c);
  138.                 printf("%3d",num);  // calculate and print the corresponding number num
  139.             }
  140.         printf("\n");       // new row
  141.     }
  142. }
  143.  
  144.  
  145. int main(void){
  146.     int n;                  // number of rows and columns
  147.  
  148.     for(n=3;n<7;n++){       // a few examples
  149.         printf("\n  n = %d \n\n",n);
  150.         number_triangle_pattern_interesting(n);
  151.     }
  152.  
  153.     printf("\n\n");
  154.     return 0;
  155. } // main()
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement