Advertisement
dmilicev

separation_of_matrix_elements.c

Nov 29th, 2019
305
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.20 KB | None
  1. /*
  2.  
  3.     separation_of_matrix_elements.c     by Dragan Milicev
  4.  
  5.     Printing various elements of a matrix relative to diagonals using functions.
  6.  
  7.  
  8. An input square matrix M[i][j] of integers of dimension 5 x 5 is given.
  9. Separate various elements of matrix to an array.
  10.  
  11. Given matrix 5 x 5 is :
  12.  
  13.    1   2   3   4   5
  14.  
  15.    6   7   8   9  10
  16.  
  17.   11  12  13  14  15
  18.  
  19.   16  17  18  19  20
  20.  
  21.   21  22  23  24  25
  22.  
  23.  
  24. Output array for main diagonal is:      1  7  13  19  25
  25.  
  26. i is index of rows.
  27. j is index of columns.
  28.  
  29.  
  30. elements of the main diagonal have the property: i = j
  31.  
  32.  
  33. elements above the main diagonal have the property: i < j
  34.  
  35.  
  36. elements of the first diagonal above the main diagonal have the property: j-i = 1
  37.  
  38. elements of the second diagonal above the main diagonal have the property: j-i = 2
  39.  
  40. elements of the third diagonal above the main diagonal have the property: j-i = 3
  41.  
  42. the elements of the fourth diagonal above the main diagonal have the property: j-i = 4
  43.  
  44.  
  45. elements below the main diagonal have the property: i > j
  46.  
  47.  
  48. elements of the first diagonal below the main diagonal have the property: i-j = 1
  49.  
  50. elements of the second diagonal below the main diagonal have the property: i-j = 2
  51.  
  52. elements of the third diagonal below the main diagonal have the property: i-j = 3
  53.  
  54. elements of the fourth diagonal below the main diagonal have the property: i-j = 4
  55.  
  56.  
  57.  
  58.  
  59. elements of the side diagonal have the property: i+j = n-1
  60.  
  61.  
  62. elements above the side diagonal have the property: i+j < n-1
  63.  
  64.  
  65. elements of the first diagonal above the side diagonal have the property: i+j = 3
  66.  
  67. elements of the second diagonal above the side diagonal have the property: i+j = 2
  68.  
  69. elements of the third diagonal above the side diagonal have the property: i+j = 1
  70.  
  71. elements of the fourth diagonal above the side diagonal have the property: i+j = 0
  72.  
  73.  
  74. elements below the side diagonal have the property: i+j >= n
  75.  
  76.  
  77. elements of the first diagonal below the side diagonal have the property: i+j = 5
  78.  
  79. elements of the second diagonal below the side diagonal have the property: i+j = 6
  80.  
  81. elements of the third diagonal below the side diagonal have the property: i+j = 7
  82.  
  83. elements of the fourth diagonal below the side diagonal have the property: i+j = 8
  84.  
  85.  
  86.  
  87. You can find all my C programs at Dragan Milicev's pastebin:
  88.  
  89.     https://pastebin.com/u/dmilicev
  90.  
  91.     https://www.facebook.com/dmilicev
  92.  
  93. */
  94.  
  95. #include <stdio.h>
  96.  
  97. // These are global variables, which means they are seen by all functions
  98.  
  99. int M[5][5]={ 1,  2,  3,  4,  5,
  100.               6,  7,  8,  9, 10,
  101.              11, 12, 13, 14, 15,
  102.              16, 17, 18, 19, 20,
  103.              21, 22, 23, 24, 25  };
  104.  
  105. int i, j, n=5, arr[25], num;
  106.  
  107.  
  108. void PrintMatrix(void)
  109. {
  110.  
  111.     printf("\n********************************\n");
  112.  
  113.     for(i=0;i<n;i++)
  114.     {
  115.         for(j=0;j<n;j++)
  116.             printf("%4d",M[i][j]);
  117.  
  118.         printf("\n\n");
  119.     }
  120. }
  121.  
  122.  
  123. void PrintArray(void)
  124. {
  125.     for(i=0;i<num;i++)
  126.             printf("%3d",arr[i]);
  127.  
  128.     printf("\n");
  129. }
  130.  
  131.  
  132. // elements of the main diagonal have the property: i = j
  133. void MainDiagonaleElements(void)
  134. {
  135.     num = 0;
  136.  
  137.     for(i=0;i<n;i++)
  138.     {
  139.         for(j=0;j<n;j++)
  140.         {
  141.             if ( i == j )
  142.             {
  143.                 arr[num] = M[i][j];
  144.                 num++;
  145.             }
  146.         }
  147.     }
  148.  
  149.     printf("\n Elements of the main diagonal, there are num = %d \n\n", num);
  150.     PrintArray();
  151. }
  152.  
  153.  
  154. // elements of the side diagonal have the property: i+j = n-1
  155. void SideDiagonaleElements(void)
  156. {
  157.     num = 0;
  158.  
  159.     for(i=0;i<n;i++)
  160.     {
  161.         for(j=0;j<n;j++)
  162.         {
  163.             if ( i+j == n-1 )
  164.             {
  165.                 arr[num] = M[i][j];
  166.                 num++;
  167.             }
  168.         }
  169.     }
  170.  
  171.     printf("\n Side diagonal elements, there are num = %d \n\n", num);
  172.     PrintArray();
  173. }
  174.  
  175.  
  176. // elements above the main diagonal have the property: i < j
  177. void ElementsAboveMainDiagonal(void)
  178. {
  179.     num = 0;
  180.  
  181.     for(i=0;i<n;i++)
  182.     {
  183.         for(j=0;j<n;j++)
  184.         {
  185.             if ( i < j )
  186.             {
  187.                 arr[num] = M[i][j];
  188.                 num++;
  189.             }
  190.         }
  191.     }
  192.  
  193.     printf("\n Elements above the main diagonal, there are num = %d \n\n", num);
  194.     PrintArray();
  195. }
  196.  
  197.  
  198. // elements of the first diagonal above the main diagonal have the property: j-i = 1
  199. void ElementsFirstDiagonalAboveMain(void)
  200. {
  201.     num = 0;
  202.  
  203.     for(i=0;i<n;i++)
  204.     {
  205.         for(j=0;j<n;j++)
  206.         {
  207.             if ( j - i == 1 )
  208.             {
  209.                 arr[num] = M[i][j];
  210.                 num++;
  211.             }
  212.         }
  213.     }
  214.  
  215.     printf("\n Elements of the first diagonal above the main, there are num = %d \n\n", num);
  216.     PrintArray();
  217. }
  218.  
  219.  
  220. // elements of the second diagonal above the main have the property: j-i = 2
  221. void ElementsSecondDiagonalAboveMain(void)
  222. {
  223.     num = 0;
  224.  
  225.     for(i=0;i<n;i++)
  226.     {
  227.         for(j=0;j<n;j++)
  228.         {
  229.             if ( j - i == 2 )
  230.             {
  231.                 arr[num] = M[i][j];
  232.                 num++;
  233.             }
  234.         }
  235.     }
  236.  
  237.     printf("\n Elements of the second diagonal above the main, there are num = %d \n\n", num);
  238.     PrintArray();
  239. }
  240.  
  241.  
  242. // elements of the third diagonal above the main have the property: j-i = 3
  243. void ElementsThirdDiagonalAboveMain(void)
  244. {
  245.     num = 0;
  246.  
  247.     for(i=0;i<n;i++)
  248.     {
  249.         for(j=0;j<n;j++)
  250.         {
  251.             if ( j - i == 3 )
  252.             {
  253.                 arr[num] = M[i][j];
  254.                 num++;
  255.             }
  256.         }
  257.     }
  258.  
  259.     printf("\n Elements of the third diagonal above the main, there are num = %d \n\n", num);
  260.     PrintArray();
  261. }
  262.  
  263.  
  264. // the elements of the fourth diagonal above the main diagonal have the property: j-i = 4
  265. void ElementsFourthDiagonalAboveMain(void)
  266. {
  267.     num = 0;
  268.  
  269.     for(i=0;i<n;i++)
  270.     {
  271.         for(j=0;j<n;j++)
  272.         {
  273.             if ( j - i == 4 )
  274.             {
  275.                 arr[num] = M[i][j];
  276.                 num++;
  277.             }
  278.         }
  279.     }
  280.  
  281.     printf("\n Elements of the fourth diagonal above the main, there are num = %d \n\n", num);
  282.     PrintArray();
  283. }
  284.  
  285.  
  286. // elements below the main diagonal have the property: i > j
  287. void ElementsBelowMainDiagonal(void)
  288. {
  289.     num = 0;
  290.  
  291.     for(i=0;i<n;i++)
  292.     {
  293.         for(j=0;j<n;j++)
  294.         {
  295.             if ( i > j )
  296.             {
  297.                 arr[num] = M[i][j];
  298.                 num++;
  299.             }
  300.         }
  301.     }
  302.  
  303.     printf("\n Elements below the main diagonal, there are num = %d \n\n", num);
  304.     PrintArray();
  305. }
  306.  
  307.  
  308. // elements of the first diagonal below the main diagonal have the property: i-j = 1
  309. void ElementsFirstDiagonalBelowMain(void)
  310. {
  311.     num = 0;
  312.  
  313.     for(i=0;i<n;i++)
  314.     {
  315.         for(j=0;j<n;j++)
  316.         {
  317.             if ( i - j == 1 )
  318.             {
  319.                 arr[num] = M[i][j];
  320.                 num++;
  321.             }
  322.         }
  323.     }
  324.  
  325.     printf("\n Elements of the first diagonal below the main, there are num = %d \n\n", num);
  326.     PrintArray();
  327. }
  328.  
  329.  
  330. // elements of the second diagonal below the main diagonal have the property: i-j = 2
  331. void ElementsSecondDiagonalBelowMain(void)
  332. {
  333.     num = 0;
  334.  
  335.     for(i=0;i<n;i++)
  336.     {
  337.         for(j=0;j<n;j++)
  338.         {
  339.             if ( i - j == 2 )
  340.             {
  341.                 arr[num] = M[i][j];
  342.                 num++;
  343.             }
  344.         }
  345.     }
  346.  
  347.     printf("\n Elements of the second diagonal below the main, there are num = %d \n\n", num);
  348.     PrintArray();
  349. }
  350.  
  351.  
  352. // elements of the third diagonal below the main diagonal have the property: i-j = 3
  353. void ElementsThirdDiagonalBelowMain(void)
  354. {
  355.     num = 0;
  356.  
  357.     for(i=0;i<n;i++)
  358.     {
  359.         for(j=0;j<n;j++)
  360.         {
  361.             if ( i - j == 3 )
  362.             {
  363.                 arr[num] = M[i][j];
  364.                 num++;
  365.             }
  366.         }
  367.     }
  368.  
  369.     printf("\n Elements of the third diagonal below the main, there are num = %d \n\n", num);
  370.     PrintArray();
  371. }
  372.  
  373.  
  374. // elements of the fourth diagonal below the main diagonal have the property: i-j = 4
  375. void ElementsFourthDiagonalBelowMain(void)
  376. {
  377.     num = 0;
  378.  
  379.     for(i=0;i<n;i++)
  380.     {
  381.         for(j=0;j<n;j++)
  382.         {
  383.             if ( i - j == 4 )
  384.             {
  385.                 arr[num] = M[i][j];
  386.                 num++;
  387.             }
  388.         }
  389.     }
  390.  
  391.     printf("\n Elements of the fourth diagonal below the main, there are num = %d \n\n", num);
  392.     PrintArray();
  393. }
  394.  
  395.  
  396. // elements below the side diagonal have the property: i+j >= n
  397. void ElementsBelowSideDiagonal(void)
  398. {
  399.     num = 0;
  400.  
  401.     for(i=0;i<n;i++)
  402.     {
  403.         for(j=0;j<n;j++)
  404.         {
  405.             if ( i+j >= n )
  406.             {
  407.                 arr[num] = M[i][j];
  408.                 num++;
  409.             }
  410.         }
  411.     }
  412.  
  413.     printf("\n Elements below the side diagonal, there are num = %d \n\n", num);
  414.     PrintArray();
  415. }
  416.  
  417.  
  418. // elements above the side diagonal have the property: i+j < n-1
  419. void ElementsAboveSideDiagonal(void)
  420. {
  421.     num = 0;
  422.  
  423.     for(i=0;i<n;i++)
  424.     {
  425.         for(j=0;j<n;j++)
  426.         {
  427.             if ( i+j < n-1 )
  428.             {
  429.                 arr[num] = M[i][j];
  430.                 num++;
  431.             }
  432.         }
  433.     }
  434.  
  435.     printf("\n Elements above the side diagonal, there are num = %d \n\n", num);
  436.     PrintArray();
  437. }
  438.  
  439.  
  440. // elements of the first diagonal above the side diagonal have the property: i+j = 3
  441. void ElementsFirstDiagonaleAboveSideDiagonal(void)
  442. {
  443.     num = 0;
  444.  
  445.     for(i=0;i<n;i++)
  446.     {
  447.         for(j=0;j<n;j++)
  448.         {
  449.             if ( i+j == 3 )
  450.             {
  451.                 arr[num] = M[i][j];
  452.                 num++;
  453.             }
  454.         }
  455.     }
  456.  
  457.     printf("\n Elements of the first diagonal above the side diagonal, there are num = %d \n\n", num);
  458.     PrintArray();
  459. }
  460.  
  461.  
  462. // elements of the second diagonal above the side diagonal have the property: i+j = 2
  463. void ElementsSecondDiagonaleAboveSideDiagonal(void)
  464. {
  465.     num = 0;
  466.  
  467.     for(i=0;i<n;i++)
  468.     {
  469.         for(j=0;j<n;j++)
  470.         {
  471.             if ( i+j == 2 )
  472.             {
  473.                 arr[num] = M[i][j];
  474.                 num++;
  475.             }
  476.         }
  477.     }
  478.  
  479.     printf("\n Elements of the second diagonal above the side diagonal, there are num = %d \n\n", num);
  480.     PrintArray();
  481. }
  482.  
  483.  
  484. // elements of the third diagonal above the side diagonal have the property: i+j = 1
  485. void ElementsThirdDiagonaleAboveSideDiagonal(void)
  486. {
  487.     num = 0;
  488.  
  489.     for(i=0;i<n;i++)
  490.     {
  491.         for(j=0;j<n;j++)
  492.         {
  493.             if ( i+j == 1 )
  494.             {
  495.                 arr[num] = M[i][j];
  496.                 num++;
  497.             }
  498.         }
  499.     }
  500.  
  501.     printf("\n Elements of the third diagonal above the side diagonal, there are num = %d \n\n", num);
  502.     PrintArray();
  503. }
  504.  
  505.  
  506. // elements of the fourth diagonal above the side diagonal have the property: i+j = 0
  507. void ElementsFourthDiagonaleAboveSideDiagonal(void)
  508. {
  509.     num = 0;
  510.  
  511.     for(i=0;i<n;i++)
  512.     {
  513.         for(j=0;j<n;j++)
  514.         {
  515.             if ( i+j == 0 )
  516.             {
  517.                 arr[num] = M[i][j];
  518.                 num++;
  519.             }
  520.         }
  521.     }
  522.  
  523.     printf("\n Elements of the fourth diagonal above the side diagonal, there are num = %d \n\n", num);
  524.     PrintArray();
  525. }
  526.  
  527.  
  528. // elements of the first diagonal below the side diagonal have the property: i+j = 5
  529. void ElementsFirstDiagonaleBelowSideDiagonal(void)
  530. {
  531.     num = 0;
  532.  
  533.     for(i=0;i<n;i++)
  534.     {
  535.         for(j=0;j<n;j++)
  536.         {
  537.             if ( i+j == 5 )
  538.             {
  539.                 arr[num] = M[i][j];
  540.                 num++;
  541.             }
  542.         }
  543.     }
  544.  
  545.     printf("\n Elements of the first diagonal below the side diagonal, there are num = %d \n\n", num);
  546.     PrintArray();
  547. }
  548.  
  549.  
  550. // elements of the second diagonal below the side diagonal have the property: i+j = 6
  551. void ElementsSecondDiagonaleBelowSideDiagonal(void)
  552. {
  553.     num = 0;
  554.  
  555.     for(i=0;i<n;i++)
  556.     {
  557.         for(j=0;j<n;j++)
  558.         {
  559.             if ( i+j == 6 )
  560.             {
  561.                 arr[num] = M[i][j];
  562.                 num++;
  563.             }
  564.         }
  565.     }
  566.  
  567.     printf("\n Elements of the second diagonal below the side diagonal, there are num = %d \n\n", num);
  568.     PrintArray();
  569. }
  570.  
  571.  
  572. // elements of the third diagonal below the side diagonal have the property: i+j = 7
  573. void ElementsThirdDiagonaleBelowSideDiagonal(void)
  574. {
  575.     num = 0;
  576.  
  577.     for(i=0;i<n;i++)
  578.     {
  579.         for(j=0;j<n;j++)
  580.         {
  581.             if ( i+j == 7 )
  582.             {
  583.                 arr[num] = M[i][j];
  584.                 num++;
  585.             }
  586.         }
  587.     }
  588.  
  589.     printf("\n Elements of the third diagonal below the side diagonal, there are num = %d \n\n", num);
  590.     PrintArray();
  591. }
  592.  
  593.  
  594. // elements of the fourth diagonal below the side diagonal have the property: i+j = 8
  595. void ElementsFourthDiagonaleBelowSideDiagonal(void)
  596. {
  597.     num = 0;
  598.  
  599.     for(i=0;i<n;i++)
  600.     {
  601.         for(j=0;j<n;j++)
  602.         {
  603.             if ( i+j == 8 )
  604.             {
  605.                 arr[num] = M[i][j];
  606.                 num++;
  607.             }
  608.         }
  609.     }
  610.  
  611.     printf("\n Elements of the fourth diagonal below the side diagonal, there are num = %d \n\n", num);
  612.     PrintArray();
  613. }
  614.  
  615.  
  616.  
  617.  
  618. int main(void)
  619. {
  620.  
  621.     PrintMatrix();
  622.  
  623.     MainDiagonaleElements();
  624.  
  625.     ElementsAboveMainDiagonal();
  626.  
  627.  
  628.     ElementsFirstDiagonalAboveMain();
  629.  
  630.     ElementsSecondDiagonalAboveMain();
  631.  
  632.     ElementsThirdDiagonalAboveMain();
  633.  
  634.     ElementsFourthDiagonalAboveMain();
  635.  
  636.  
  637.     ElementsBelowMainDiagonal();
  638.  
  639.  
  640.     ElementsFirstDiagonalBelowMain();
  641.  
  642.     ElementsSecondDiagonalBelowMain();
  643.  
  644.     ElementsThirdDiagonalBelowMain();
  645.  
  646.     ElementsFourthDiagonalBelowMain();
  647.  
  648.  
  649.     PrintMatrix();
  650.  
  651.  
  652.     SideDiagonaleElements();
  653.  
  654.  
  655.     ElementsAboveSideDiagonal();
  656.  
  657.  
  658.     ElementsFirstDiagonaleAboveSideDiagonal();
  659.  
  660.     ElementsSecondDiagonaleAboveSideDiagonal();
  661.  
  662.     ElementsThirdDiagonaleAboveSideDiagonal();
  663.  
  664.     ElementsFourthDiagonaleAboveSideDiagonal();
  665.  
  666.  
  667.     ElementsBelowSideDiagonal();
  668.  
  669.  
  670.     ElementsFirstDiagonaleBelowSideDiagonal();
  671.  
  672.     ElementsSecondDiagonaleBelowSideDiagonal();
  673.  
  674.     ElementsThirdDiagonaleBelowSideDiagonal();
  675.  
  676.     ElementsFourthDiagonaleBelowSideDiagonal();
  677.  
  678.  
  679.     PrintMatrix();
  680.  
  681.  
  682.     printf("\n\n");
  683.     //system("PAUSE");
  684.  
  685.     return 0;
  686. }
Advertisement
RAW Paste Data Copied
Advertisement