fork download
  1. /* Recursive definition of determinate using expansion by minors.*/
  2. #define NULL 0
  3. double Determinant(double **a,int n)
  4. {
  5. int i,j,j1,j2 ; // general loop and matrix subscripts
  6. double det = 0 ; // init determinant
  7. double **m = NULL ; // pointer to pointers to implement 2D square array
  8.  
  9. if (n < 1) // error condition, should never get here
  10. {
  11. error("Number of Rows and Columns is less than 1");
  12. }
  13. else if (n == 1)
  14. { // should not get here
  15. det = a[0][0] ;
  16. }
  17. else if (n == 2) // basic 2X2 sub-matrix determinate definition.
  18. { // When n==2, this ends the the recursion series
  19.  
  20. det = a[0][0] * a[1][1] - a[1][0] * a[0][1];
  21. }
  22.  
  23. else // recursion continues, solve next sub-matrix
  24. { // solve the next minor by building a
  25. // sub matrix
  26. det = 0 ; // initialize determinant of sub-matrix
  27.  
  28. // for each column in sub-matrix
  29. for (j1 = 0 ; j1 < n ; j1++)
  30. {
  31. // get space for the pointer list
  32. m = (double **) malloc((n-1)* sizeof(double *)) ;
  33.  
  34. for (i = 0 ; i < n-1 ; i++)
  35. m[i] = (double*) malloc((n-1)* sizeof(double)) ;
  36. // i[0][1][2][3] first malloc
  37. // m -> + + + + space for 4 pointers
  38. // | | | | j second malloc
  39. // | | | +-> _ _ _ [0] pointers to
  40. // | | +----> _ _ _ [1] and memory for
  41. // | +-------> _ a _ [2] 4 doubles
  42. // +----------> _ _ _ [3]
  43. //
  44. // a[1][2]
  45. // build sub-matrix with minor elements excluded
  46. for (i = 1 ; i < n ; i++)
  47. {
  48. j2 = 0 ; // start at first sum-matrix column position
  49. // loop to copy source matrix less one column
  50. for (j = 0 ; j < n ; j++)
  51. {
  52. if (j == j1)
  53. continue ; // don't copy the minor column element
  54.  
  55. m[i-1][j2] = a[i][j] ; // copy source element into new sub-matrix
  56. // i-1 because new sub-matrix is one row
  57. // (and column) smaller with excluded minors
  58. j2++ ; // move to next sub-matrix column position
  59. }
  60. }
  61.  
  62. det += (double)pow(-1.0,1.0 + j1 + 1.0) * a[0][j1] * Determinant(m,n-1) ;
  63. // sum x raised to y power
  64. // recursively get determinant of next
  65. // sub-matrix which is now one
  66. // row & column smaller
  67.  
  68. for (i = 0 ; i < n-1 ; i++) // free the storage allocated to
  69. free(m[i]) ; // to this minor's set of pointers
  70. free(m) ; // free the storage for the original
  71. } // pointer to pointer
  72.  
  73. }
  74. return(det) ;
  75. }
  76.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘Determinant’:
prog.c:11: warning: implicit declaration of function ‘error’
prog.c:32: warning: implicit declaration of function ‘malloc’
prog.c:32: warning: incompatible implicit declaration of built-in function ‘malloc’
prog.c:62: warning: implicit declaration of function ‘pow’
prog.c:62: warning: incompatible implicit declaration of built-in function ‘pow’
prog.c:69: warning: implicit declaration of function ‘free’
prog.c:69: warning: incompatible implicit declaration of built-in function ‘free’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
stdout
Standard output is empty