Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- number_triangle_pattern_interesting.c
- https://www.facebook.com/groups/3092811364081112/permalink/7605244639504406/
- numbers indexes matrix
- 1 00 00 01 02 03 04
- 2 6 10 11 10 11 12 13 14
- 3 7 10 20 21 22 20 21 22 23 24
- 4 8 11 13 30 31 32 33 30 31 32 33 34
- 5 9 12 14 15 40 41 42 43 44 40 41 42 43 44
- r - row
- c - column
- There are n rows and at most n columns.
- The number of columns in a row is the row number (r)
- The number in the first column is the row number (r)
- The number in the n-th column is ...
- In order to calculate the corresponding number to be written
- in the place whose indices are r1,c1,
- we will use the indices of the virtual matrix M[n][n].
- See separation_of_matrix_elements.c
- https://pastebin.com/7AbM4rB1
- An square matrix M[r][c] of characters of dimension 5 x 5 is given.
- Separate various elements of matrix to an array.
- r is index of rows.
- c is index of columns.
- elements of the main diagonal have the property: r = c
- elements above the main diagonal have the property: r < c
- elements of the first diagonal above the main diagonal have the property: r-c = -1
- elements of the second diagonal above the main diagonal have the property: r-c = -2
- elements of the third diagonal above the main diagonal have the property: r-c = -3
- the elements of the fourth diagonal above the main diagonal have the property: r-c = -4
- elements below the main diagonal have the property: r > c
- elements of the first diagonal below the main diagonal have the property: r-c = 1
- elements of the second diagonal below the main diagonal have the property: r-c = 2
- elements of the third diagonal below the main diagonal have the property: r-c = 3
- elements of the fourth diagonal below the main diagonal have the property: r-c = 4
- elements of the side diagonal have the property: r+c = n-1
- elements above the side diagonal have the property: r+c < n-1
- elements of the first diagonal above the side diagonal have the property: r+c = n-1
- elements of the second diagonal above the side diagonal have the property: r+c = n-2
- elements of the third diagonal above the side diagonal have the property: r+c = n-3
- elements of the fourth diagonal above the side diagonal have the property: r+c = n-4
- elements below the side diagonal have the property: r+c >= n
- elements of the first diagonal below the side diagonal have the property: r+c = 5
- elements of the second diagonal below the side diagonal have the property: r+c = 6
- elements of the third diagonal below the side diagonal have the property: r+c = 7
- elements of the fourth diagonal below the side diagonal have the property: r+c = 8
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- /*
- numbers indexes matrix
- 1 00 00 01 02 03 04
- 2 6 10 11 10 11 12 13 14
- 3 7 10 20 21 22 20 21 22 23 24
- 4 8 11 13 30 31 32 33 30 31 32 33 34
- 5 9 12 14 15 40 41 42 43 44 40 41 42 43 44
- */
- // counts elements up to element r1,c1
- int calculate_num(int n, int r1, int c1){
- int r, c, num=0, fromWhichRow=0;
- for(c=0;c<=c1;c++){ // for all columns up to the column in which the requested element is
- if(c<c1){
- for(r=fromWhichRow;r<n;r++) // from which row to count elements
- num++;
- fromWhichRow++; // the next first row from which it is counted
- }
- else if(c==c1) // for the column in which the requested element is
- for(r=fromWhichRow;r<=r1;r++)
- num++;
- }
- return num;
- }
- // print triangle of numbers
- void number_triangle_pattern_interesting(int n){
- int r, c, num; // r - row, c - column, num - number to be printed
- for(r=0;r<n;r++){ // for all rows
- for(c=0;c<n;c++) // for all columns
- if(r>=c){ // elements on the main diagonal and below the main diagonal have a property r >= c
- num = calculate_num(n,r,c);
- printf("%3d",num); // calculate and print the corresponding number num
- }
- printf("\n"); // new row
- }
- }
- int main(void){
- int n; // number of rows and columns
- for(n=3;n<7;n++){ // a few examples
- printf("\n n = %d \n\n",n);
- number_triangle_pattern_interesting(n);
- }
- printf("\n\n");
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement