Advertisement
dmilicev

determinant_of_square_nxn_matrix.c

Dec 3rd, 2019
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. /*
  2.  
  3.     determinant_of_square_nxn_matrix.c
  4.  
  5.     n is rang of square matrix M[n][n].
  6.  
  7.     Calculate determinant of matrix M[n][n] by recursive function determinant().
  8.  
  9.  
  10.     You can find all my C programs at Dragan Milicev's pastebin:
  11.  
  12.     https://pastebin.com/u/dmilicev
  13.  
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>     // for exit()
  18. #include <math.h>       // for pow()
  19.  
  20. #define MAX_SIZE 10
  21.  
  22.  
  23. // Displays a square double matrix M[n][n] that has n rows and n columns
  24. void display_square_double_matrix( char *text, double M[][MAX_SIZE], int n )
  25. {
  26.     int i, j;
  27.  
  28.     printf("\n%s\n\n",text);
  29.  
  30.     for(i=0;i<n;i++) {                  // print matrix M[n][n]
  31.         for(j=0;j<n;j++)
  32.             printf("\t%.2lf", M[i][j]);
  33.  
  34.         printf("\n\n");                 // new row of matrix
  35.     }
  36. }
  37.  
  38.  
  39. // Recursive definition of determinate using expansion by minors.
  40. // http://paulbourke.net/miscellaneous/determinant/determinant.c
  41. double determinant( double M[][MAX_SIZE], int n )
  42. {
  43.     int i, j, j1, j2;
  44.     double det = 0;
  45.     double Sub[MAX_SIZE][MAX_SIZE];
  46.  
  47.     if ( n < 1 )                // Error
  48.     {
  49.         printf("\n Error: rang of matrix M[][] is less than 1 !!! \n");
  50.         exit(1);
  51.     }
  52.     else if ( n == 1 )      // Shouldn't get used
  53.     {
  54.         det = M[0][0];
  55.     }
  56.     else if ( n == 2 )
  57.     {
  58.         det = M[0][0] * M[1][1] - M[1][0] * M[0][1];
  59.     }
  60.     else
  61.     {
  62.         det = 0;
  63.  
  64.         for ( j1=0; j1<n; j1++ )
  65.         {
  66.             for ( i=1; i<n; i++ )
  67.             {
  68.                 j2 = 0;
  69.  
  70.                 for ( j=0; j<n; j++ )
  71.                 {
  72.                     if ( j == j1 )
  73.                         continue;
  74.  
  75.                     Sub[i-1][j2] = M[i][j];
  76.                     j2++;
  77.                 }
  78.             }
  79.  
  80.             det += pow(-1.0,1.0+j1+1.0) * M[0][j1] * determinant( Sub, n-1 );
  81.         }
  82.     }
  83.     return( det );
  84. } // determinant()
  85.  
  86.  
  87. int main()
  88. {
  89.     int i, j, n=3;              // n is rang of square matrix M[n][n]
  90.     double det;
  91.     double M[MAX_SIZE][MAX_SIZE];
  92. /*
  93.     double M[MAX_SIZE][MAX_SIZE] = {
  94.                                     { 1.0, 2.0, 3.0 },
  95.                                     { 4.0, 5.0, 6.0 },
  96.                                     { 7.0, 8.0, 1.0 },
  97.                                                         };
  98. */
  99.  
  100.     printf("\n Enter number of rows and columns of square matrix M[n][n], rang n = ");
  101.     scanf("%d", &n );
  102.  
  103.     for( i=0; i<n; i++ )                    // input matrix M[n][n]
  104.     {
  105.         for( j=0; j<n; j++ )
  106.         {
  107.             printf("\n Enter M[%d][%d] = ", i, j );
  108.             scanf("%lf", &M[i][j] );
  109.         }
  110.  
  111.         printf("\n");                       // new row of matrix
  112.     }
  113.  
  114.     display_square_double_matrix("\n Square matrix M[n][n] is: ", M, n );
  115.  
  116.     det = determinant( M, n );
  117.  
  118.     printf("\n determinant = %.2lf \n", det );
  119.  
  120.  
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement