fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. void print(double ** m, int N)
  6. {
  7. for(int i = 0; i < N; ++i)
  8. {
  9. for(int j = 0; j < N; ++j)
  10. {
  11. printf("%.1lf ",m[i][j]);
  12. }
  13. puts("");
  14. }
  15. puts("");
  16. }
  17.  
  18.  
  19. double GaussDet(double**m, int n)
  20. {
  21. double det = 1;
  22. for(int i = 0; i < n; ++i)
  23. {
  24. double mx = fabs(m[i][i]);
  25. int idx = i;
  26. for(int j = i+1; j < n; ++j)
  27. if (mx < fabs(m[i][j])) mx = fabs(m[i][idx = j]);
  28. if (idx != i)
  29. {
  30. for(int j = i; j < n; ++j)
  31. {
  32. double t = m[j][i];
  33. m[j][i] = m[j][idx];
  34. m[j][idx] = t;
  35. }
  36. det = -det;
  37. }
  38. for(int k = i+1; k < n; ++k)
  39. {
  40. double t = m[k][i]/m[i][i];
  41.  
  42. for(int j = i; j < n; ++j)
  43. m[k][j] -= m[i][j]*t;
  44. }
  45. }
  46. for(int i = 0; i < n; ++i) det *= m[i][i];
  47. return det;
  48. }
  49.  
  50.  
  51. int main(int argc, const char * argv[])
  52. {
  53. #define N 5
  54. double ** m = malloc(N*sizeof(double*));
  55. for(int i = 0; i < N; ++i) m[i] = malloc(N*sizeof(double));
  56.  
  57. for(int i = 0; i < N; ++i)
  58. {
  59. for(int j = 0; j < N; ++j)
  60. {
  61. m[i][j] = rand()%10;
  62. }
  63. }
  64. m[0][0] = 0;
  65. print(m,N);
  66.  
  67. printf("Det = %lf\n",GaussDet(m,N));
  68.  
  69. }
  70.  
Success #stdin #stdout 0s 5280KB
stdin
1 2 3 
4 5 6 
7 8 9
stdout
0.0 6.0 7.0 5.0 3.0 
5.0 6.0 2.0 9.0 1.0 
2.0 7.0 0.0 9.0 3.0 
6.0 0.0 6.0 2.0 6.0 
1.0 8.0 7.0 9.0 2.0 

Det = -2062.000000