dmilicev

lets_learn_c_matrices_with_integers_v1.c

Nov 5th, 2019
204
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.25 KB | None
  1. /*
  2.  
  3.     lets_learn_c_matrices_with_integers_v1.c
  4.  
  5. Work in C with matrices with integers.
  6.  
  7. The idea is that this program should cover everything about matrices in C.
  8.  
  9. The point of the functions is to divide a big problem
  10. into several smaller problems.
  11. Each function should solve only one problem and nothing more.
  12. Functions should therefore not have more than a couple dozen lines of code.
  13. The features of the functions are arguments (parameters, input values)
  14. and the value returned by the function to the part of the program
  15. from which it was called.
  16. If we pass to the function the pointers to the variables as arguments,
  17. function then has the ability to change the value of the arguments
  18. beyond its scope, and in that case it does not even have to have
  19. its own return value.
  20. Our conversations should soon come down to questions about how to write
  21. a function that receives those arguments and has that return value.
  22.  
  23. Please understand that English is not my native language and
  24. please feel free to correct me about it.
  25.  
  26. Feel free to let me know if you think I left out something,
  27. or if you find some bugs in this program.
  28.  
  29. This program will be updated according to your suggestions.
  30.  
  31. Author Dragan Milicev
  32. https://www.facebook.com/dmilicev
  33. You can find all my C programs at Dragan Milicev's pastebin:
  34. https://pastebin.com/u/dmilicev
  35.  
  36. -----------------------------------------------------------------------------
  37.  
  38. Let's understand what matrices are:
  39.  
  40. ( Yes, it's math, but it's not scary )
  41.  
  42.  
  43. https://en.wikipedia.org/wiki/Matrix_(mathematics)
  44.  
  45. http://www.maths.surrey.ac.uk/explore/emmaspages/option1.html
  46.  
  47. https://www.wikihow.com/Understand-the-Basics-of-Matrices
  48.  
  49.  Matrix calculator
  50. https://matrixcalc.org/en/
  51. https://www.symbolab.com/solver/matrix-calculator
  52.  
  53.  Determinant calculator
  54. https://matrixcalc.org/en/det.html
  55.  
  56. https://www.khanacademy.org/math/precalculus/x9e81a4f98389efdf:matrices/x9e81a4f98389efdf:mat-intro/a/intro-to-matrices
  57.  
  58. https://www.mathplanet.com/education/algebra-2/matrices/how-to-operate-with-matrices
  59.  
  60. https://www.mathsisfun.com/algebra/matrix-introduction.html
  61.  
  62. https://www.sanfoundry.com/c-programming-examples-matrix/
  63.  
  64. https://www.math24.net/properties-matrices/
  65.  
  66. https://brilliant.org/wiki/matrices/
  67.  
  68. https://math.oregonstate.edu/home/programs/undergrad/CalculusQuestStudyGuides/vcalc/matrix/matrix.html
  69.  
  70. https://www.ditutor.com/matrix/matrices.html
  71.  
  72. tutorialspoint.com did not process matrices in programming language C,
  73. but did it in programming language R (you may be interested in looking)
  74. https://www.tutorialspoint.com/r/r_matrices.htm
  75.  
  76.  
  77. */
  78.  
  79.  
  80. #include <stdio.h>
  81. #include <stdlib.h>     // for exit()
  82.  
  83. #define MAX_SIZE 30
  84.  
  85.  
  86. // Displays a matrix M [] [] that has r rows and c columns
  87. void display_matrix( char *text, int M[][MAX_SIZE], int r, int c )
  88. {
  89.     int i, j;
  90.  
  91.     printf("\n%s\n\n",text);
  92.  
  93.     for(i=0;i<r;i++) {                  // print matrix M[][]
  94.         for(j=0;j<c;j++)
  95.             printf(" %4d",M[i][j]);
  96.  
  97.         printf("\n\n");                 // new row of matrix
  98.     }
  99. }
  100.  
  101.  
  102. // Forms an matrix M [] [] that has r rows and c columns
  103. // The elements of matrix are ordinal numbers.
  104. void form_matrix( int M[][MAX_SIZE], int r, int c )
  105. {
  106.     int i, j;
  107.  
  108.     for(i=0;i<r;i++)            // for all rows
  109.         for(j=0;j<c;j++)            // for all columns
  110.             M[i][j] = i*c + j+1;
  111. }
  112.  
  113.  
  114. // Displays the matrix M without row WhichRow. It does not change the matrix M.
  115. void display_matrix_without_WhichRow( int M[][MAX_SIZE], int r, int c, int WhichRow )
  116. {
  117.     int i,j;
  118.  
  119.     printf("\n");
  120.  
  121.     for(i=0;i<r;i++){           // for all rows
  122.         for(j=0;j<c;j++){           // for all columns
  123.             if(i != WhichRow-1)         // WhichRow-1 is index of that row
  124.                 printf("%4d",M[i][j]);
  125.         }
  126.         if(i != WhichRow-1)
  127.             printf("\n\n");
  128.     }
  129. }
  130.  
  131.  
  132. // Display matrix without column WhichColumn. It does not change the matrix M.
  133. void display_matrix_without_WhichColumn( int M[][MAX_SIZE], int r, int c, int WhichColumn )
  134. {
  135.     int i,j;
  136.  
  137.     printf("\n");
  138.  
  139.     for(i=0;i<r;i++){           // for all rows
  140.         for(j=0;j<c;j++){           // for all columns
  141.             if(j != WhichColumn-1){
  142.                 printf("%4d",M[i][j]);
  143.             }
  144.         }
  145.         printf("\n\n");
  146.     }
  147. }
  148.  
  149.  
  150. // Display matrix without main diagonal. It does not change the matrix M.
  151. // The displayed matrix has the same number of rows r, the number of columns is 1 smaller.
  152. void display_matrix_without_main_diagonal( int M[][MAX_SIZE], int r, int c )
  153. {
  154.     int i, j;
  155.  
  156.     printf("\n");
  157.  
  158.     for(i=0;i<r;i++){
  159.         for(j=0;j<c;j++)
  160.             if( i != j )   // if this is not an element of the main diagonal of the matrix M
  161.                 printf(" %4d",M[i][j]);
  162.  
  163.         printf("\n\n");     // new row of matrix M
  164.     }
  165. }
  166.  
  167.  
  168. // Display matrix without side diagonal. It does not change the matrix M.
  169. // The displayed matrix has the same number of rows r, the number of columns is 1 smaller.
  170. void display_matrix_without_side_diagonal( int M[][MAX_SIZE], int r, int c )
  171. {
  172.     int i, j;
  173.  
  174.     printf("\n");
  175.  
  176.     for(i=0;i<r;i++){
  177.         for(j=0;j<c;j++)
  178.             // should also be i+j != n-1, but this is also true in the case with rectangular matrices where r<c
  179.             if( i+j != r-1 )   // if this is not an element of the main diagonal of the matrix M
  180.                 printf(" %4d",M[i][j]);
  181.  
  182.         printf("\n\n");     // new row of matrix M
  183.     }
  184. }
  185.  
  186.  
  187. // Delete row WhichRow from matrix M. The new matrix has one fewer rows.
  188. // function gets an address of r (int *r) to be able to change it
  189. void delete_WhichRow_in_matrix( int M[][MAX_SIZE], int *r, int c, int WhichRow )
  190. {
  191.     int i,j,r1=0;                   // r1 is the row index of the new matrix
  192.  
  193.     for(i=0;i<*r;i++){              // for all rows
  194.         for(j=0;j<c;j++){           // for all columns
  195.             if(i != WhichRow-1){    // if it's not the row we're deleting
  196.                 M[r1][j] = M[i][j]; // we put the elements in the new matrix
  197.             }
  198.         }
  199.         if(i != WhichRow-1)         // if it's not the row we're deleting
  200.             r1++;                   // increase the row index of the new matrix
  201.     }
  202.     (*r)--;     // since we deleted one row, we reduce the number of rows of the new matrix
  203. }
  204.  
  205.  
  206. // // Delete column WhichColumn in matrix. The new matrix has one fewer columns.
  207. void delete_WhichColumn_in_matrix( int M[][MAX_SIZE], int r, int *c, int WhichColumn )
  208. {
  209.     int i,j,k1;                         // k1 is the column index of the new matrix
  210.  
  211.     for(i=0;i<r;i++){                   // for all rows
  212.         k1=0;                           // at the beginning of each row, index of the column is zero
  213.         for(j=0;j<*c;j++){              // for all columns
  214.             if(j != WhichColumn-1){     // if it's not the column we're deleting
  215.                 M[i][k1++] = M[i][j];   // we put the elements in the new matrix
  216.             }
  217.         }
  218.     }
  219.     (*c)--;     // since we deleted one column, we reduce the number of columns of the new matrix
  220. }
  221.  
  222.  
  223. // Delete row WhichRow and column WhichColumn in matrix M
  224. // The new matrix has a smaller number of rows and a number of columns.
  225. // Function gets address of r (int *r) and c (int *c) to be able to change them.
  226. void delete_WhichRow_and_WhichColumn( int M[][MAX_SIZE], int *r, int *c, int WhichRow, int WhichColumn )
  227. {
  228.     int i,j,r1=0,k1;  // k1 and r1 are the index for the columns and rows of the new matrix
  229.  
  230.     for(i=0;i<*r;i++){                  // for all rows
  231.  
  232.         for(j=0;j<*c;j++){              // for all columns
  233.             if(i != WhichRow-1){
  234.                 M[r1++][j]=M[i][j];
  235.             }
  236.         }
  237.         if(i != WhichRow-1)
  238.             printf("\n");
  239.     }
  240.  
  241.     (*r)--; // since we deleted one row, we reduce the number of rows of the new matrix
  242.     (*c)--; // since we deleted one column, we reduce the number of columns of the new matrix
  243. }
  244.  
  245.  
  246. // Display matrix without row WhichRow and column WhichColumn. It does not change the matrix M.
  247. void display_matrix_without_WhichRow_and_WhichColumn( int M[][MAX_SIZE], int r, int c, int WhichRow, int WhichColumn )
  248. {
  249.     int i,j;
  250.  
  251.     printf("\n\n");
  252.  
  253.     for(i=0;i<r;i++){                   // for all rows
  254.  
  255.         for(j=0;j<c;j++){               // for all columns
  256.             if(i != WhichRow-1 )
  257.                 if(j != WhichColumn-1)
  258.                     printf("%4d",M[i][j]);
  259.         }
  260.         if(i != WhichRow-1)
  261.             printf("\n\n");
  262.     }
  263. }
  264.  
  265.  
  266. // Deletes the main diagonal of the matrix M[r][c] having r rows and c columns.
  267. // New matrix without main diagonal has the same number of rows,
  268. // and the number of columns is 1 less.
  269. // Function gets address of c (int *c) to be able to change it.
  270. void delete_main_diagonal_of_matrix( int M[][MAX_SIZE], int r, int *c )
  271. {
  272.     int i, j, k1;   // k1 is the number of columns of the matrix M with the main diagonal ejected
  273.  
  274.     for(i=0;i<r;i++){
  275.  
  276.         k1=0;       // initial value for the column counter of the new matrix
  277.  
  278.         for(j=0;j<*c;j++)
  279.             if( i != j )   // if this is not an element of the main diagonal of the matrix M[][]
  280.                 M[i][k1++] = M[i][j];  // and increase k1, number of columns of new matrix
  281.     }
  282.  
  283.     (*c)--;                             // reduce the number of columns
  284. }
  285.  
  286.  
  287. // Delete side diagonal of matrix M[r][c] having r rows and c columns.
  288. // New matrix without side diagonal has the same number of rows,
  289. // and the number of columns is 1 less.
  290. // Function gets address of c (int *c) to be able to change it.
  291. void delete_side_diagonal_of_matrix( int M[][MAX_SIZE], int r, int *c )
  292. {
  293.     int i, j, k1;   // k1 is the number of columns of the matrix M with the side diagonal ejected
  294.  
  295.     for(i=0;i<r;i++){
  296.  
  297.         k1=0;       // initial value for the column counter of the new matrix
  298.  
  299.         for(j=0;j<*c;j++)
  300.             // should also be i+j != n-1, but this is also true in the case with rectangular matrices where r<c
  301.             if( i+j != r-1 )   // if this is not an element of the side diagonal of the matrix M[][]
  302.                 M[i][k1++] = M[i][j];  // and increase k1, number of columns of new matrix
  303.     }
  304.  
  305.     (*c)--;     // reduce the number of columns
  306. }
  307.  
  308.  
  309. // In matrix M, rows r1 and r2 swap places.
  310. void swap_places_of_rows_r1_and_r2( int M[][MAX_SIZE], int r, int c, int r1, int r2 )
  311. {
  312.     int j, mem;     // mem is a temporary memory (cache) for the integer
  313.  
  314.     r1--;   // Convert row number to index
  315.     r2--;   // Convert row number to index
  316.  
  317.     for(j=0;j<c;j++){       // repeat c times:
  318.         mem = M[r1][j];     // elements of two rows r1 and r2 interchange places
  319.         M[r1][j] = M[r2][j];
  320.         M[r2][j] = mem;
  321.     }
  322. }
  323.  
  324.  
  325. // In matrix M, columns c1 and c2 swap places.
  326. void swap_places_of_columns_c1_and_c2( int M[][MAX_SIZE], int r, int c, int c1, int c2 )
  327. {
  328.     int i, mem;
  329.  
  330.     c1--;   // Convert column number to index
  331.     c2--;   // Convert column number to index
  332.  
  333.     for(i=0;i<r;i++){       // repeat r times:
  334.         mem = M[i][c1];     // elements of two columns r1 and r2 interchange places
  335.         M[i][c1] = M[i][c2];
  336.         M[i][c2] = mem;
  337.     }
  338. }
  339.  
  340.  
  341.  
  342. int main_many(void)
  343. {
  344.     int M[MAX_SIZE][MAX_SIZE];
  345.     int i, r=4, c=4, WhichRow, WhichColumn;
  346.     int choice;     // r and c are the row and column numbers of the matrix M
  347.  
  348.     form_matrix(M,r,c);
  349.  
  350.     while( 1 ) {    // an endless loop that exits with a choice of 0 or Escape
  351.  
  352.         //system("CLS");          // clear command window
  353.  
  354.         display_matrix(" Matrix is: ",M,r,c);
  355.  
  356.         printf("\n \t MAIN MENY \n\n"
  357.                "  1  Form matrix of ordinal numbers \n"
  358.                "  2  Display matrix without row r \n"
  359.                "  3  Delete row r in matrix \n"
  360.                "  4  Display matrix without column c \n"
  361.                "  5  Delete column c in matrix \n"
  362.                "  6  Display matrix without row r and column c \n"
  363.                "  7  Delete row r and column c in matrix \n"
  364.                "  8  Display matrix without main diagonal \n"
  365.                "  9  Delete main diagonal in matrix \n"
  366.                "  a  Display matrix without side diagonal \n"
  367.                "  b  Delete side diagonal of matrix \n"
  368.                "  c  Interchange of two row positions \n"
  369.                "  d  Interchange of two column positions \n"
  370.                "  e  Form different matrix of ordinal numbers \n"
  371.                "  0  End of work (0 or Escape ESC) \n\n"
  372.                " \t Your choice is: "
  373.                );
  374.  
  375.         switch(choice = getche()){
  376.  
  377.             case '1':         // reset matrix, form matrix of ordinal numbers
  378.                     form_matrix(M,r,c);
  379.                     break;
  380.             case '2':         // Display matrix without row r
  381.                     printf("\n Which row you don't want to show, r = ");
  382.                     scanf("%d", &WhichRow);
  383.                     if ( WhichRow < 0 || WhichRow > r ) {
  384.                         printf("\n Row %d was not entered correctly! \n",WhichRow);
  385.                         exit(1);
  386.                     }
  387.                     display_matrix_without_WhichRow(M,r,c,WhichRow);
  388.                     break;
  389.             case '3':         // Delete row WhichRow in matrix
  390.                     printf("\n Koji red zelis da obrises, r = ");
  391.                     scanf("%d", &WhichRow);
  392.                     delete_WhichRow_in_matrix(M,&r,c,WhichRow);
  393.                     break;
  394.             case '4':         // Display matrix without column c
  395.                     printf("\n Which column you do not want to display, c = ");
  396.                     scanf("%d", &WhichColumn);
  397.                     if ( WhichColumn < 0 || WhichColumn > c ) {
  398.                         printf("\n Column %d was not entered correctly! \n",WhichColumn);
  399.                         exit(1);
  400.                     }
  401.                     display_matrix_without_WhichColumn(M,r,c,WhichColumn);
  402.                     break;
  403.             case '5':         // Delete column WhichColumn in matrix
  404.                     printf("\n Which column do you want to delete, c = ");
  405.                     scanf("%d", &WhichColumn);
  406.                     delete_WhichColumn_in_matrix(M,r,&c,WhichColumn);
  407.                     break;
  408.             case '6':         // Display matrix without row WhichRow and column WhichColumn
  409.                     printf("\n Which row do you want to delete,    r = ");
  410.                     scanf("%d", &WhichRow);
  411.                     printf("\n Which column do you want to delete, c = ");
  412.                     scanf("%d", &WhichColumn);
  413.                     display_matrix_without_WhichRow_and_WhichColumn(M,r,c,WhichRow,WhichColumn);
  414.                     break;
  415.             case '7':         // Delete row WhichRow and column WhichColumn in matrix
  416.                     printf("\n Which row do you want to delete, r = ");
  417.                     scanf("%d", &WhichRow);
  418.                     if ( WhichRow < 0 || WhichRow > r ) {
  419.                         printf("\n Row %d was not entered correctly! \n",WhichRow);
  420.                         exit(1);
  421.                     }
  422.                     printf("\n Which column do you want to delete, c = ");
  423.                     scanf("%d", &WhichColumn);
  424.                     if ( WhichColumn < 0 || WhichColumn > c ) {
  425.                         printf("\n Column %d was not entered correctly! \n",WhichColumn);
  426.                         exit(1);
  427.                     }
  428.                     delete_WhichRow_and_WhichColumn(M,&r,&c,WhichRow,WhichColumn);
  429.                     break;
  430.             case '8':         // Display matrix without main diagonal
  431.                     display_matrix_without_main_diagonal(M,r,c);
  432.                     break;
  433.             case '9':         // Delete main diagonal in matrix
  434.                     delete_main_diagonal_of_matrix(M,r,&c);
  435.                     break;
  436.             case 'a':        // Display matrix without side diagonal
  437.                     display_matrix_without_side_diagonal(M,r,c);
  438.                     break;
  439.             case 'b':        // Delete side diagonal of matrix
  440.                     delete_side_diagonal_of_matrix(M,r,&c);
  441.                     break;
  442.             case 'c':        // Interchange of two row positions
  443.                     printf("\n Enter the first row interchange number, r1 = ");
  444.                     scanf("%d", &WhichRow);
  445.                     if ( WhichRow < 0 || WhichRow > r ) {
  446.                         printf("\n Row %d was not entered correctly! \n",WhichRow);
  447.                         exit(1);
  448.                     }
  449.                     printf("\n Enter the second row interchange number, r2 = ");
  450.                     scanf("%d", &WhichColumn); // the WhichColumn variable was used here to enter the queue
  451.                     if ( WhichColumn < 0 || WhichColumn > r ) {
  452.                         printf("\n Row %d was not entered correctly! \n",WhichColumn);
  453.                         exit(1);
  454.                     }
  455.                     swap_places_of_rows_r1_and_r2(M,r,c,WhichRow,WhichColumn);
  456.                     break;
  457.             case 'd':        // Interchange of two column positions
  458.                     printf("\n Enter the number of the first interchange column, k1 = ");
  459.                     scanf("%d", &WhichRow); // The WhichRow variable for column input was used here
  460.                     if ( WhichRow < 0 || WhichRow > c ) {
  461.                         printf("\n Column %d was not entered correctly! \n",WhichRow);
  462.                         exit(1);
  463.                     }
  464.                     printf("\n Enter the number of the second interchange column, k2 = ");
  465.                     scanf("%d", &WhichColumn);
  466.                     if ( WhichColumn < 0 || WhichColumn > c ) {
  467.                         printf("\n Column %d was not entered correctly! \n",WhichColumn);
  468.                         exit(1);
  469.                     }
  470.                     swap_places_of_columns_c1_and_c2(M,r,c,WhichRow,WhichColumn);
  471.                     break;
  472.             case 'e':        // form different matrix of ordinal numbers
  473.                     printf("\n Enter the number of rows of the new matrix, r = ");
  474.                     scanf("%d", &r);
  475.                     printf("\n Enter the number of columns of the new matrix, c = ");
  476.                     scanf("%d", &c);
  477.                     form_matrix(M,r,c);
  478.                     break;
  479.             case '0' :                      // 0 to end of work
  480.                     return(choice);
  481.                     break;
  482.  
  483.             case 27  :                      // ESC to end of work
  484.                     return(choice);
  485.                     break;
  486.  
  487.             case  0  :                      // if a function key is pressed
  488.                     choice = getche();      // let's empty that 0
  489.  
  490.             case 224 :                      // if a special function key is pressed
  491.                     choice = getche();      // let's empty that 224
  492.                     break;
  493.  
  494.             default:                        // if entered character is not from the meny
  495.                     if( !(choice<'0' && choice>'9') && !(choice<'a' && choice>'e') ) {
  496.                         printf("\n\n Wrong choose, entered character is not from the meny !!! \n");
  497.                     }
  498.                     break;
  499.  
  500.         } // switch(choice = getche())
  501.     } // while( 1 )
  502.     return choice;
  503. } // main_many()
  504.  
  505.  
  506.  
  507.  
  508. int main(void)
  509. {
  510.  
  511.     main_many();
  512.  
  513.  
  514.     return 0;
  515. }
RAW Paste Data Copied