fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. void zerar(int n,int m[][n]) {
  5. int i, j;
  6. for (i=0; i<n; i++) {
  7. for (j=0; j<n; j++) {
  8. m[i][j] = 0;
  9. }
  10. }
  11. }
  12.  
  13. void printm(int n, int matriz[][n]) {
  14. int i,j;
  15. for (i=0; i<n; i++) {
  16. for (j=0; j<n; j++) {
  17. printf("\t%d", matriz[i][j]);
  18. }
  19. printf("\n");
  20. }
  21. }
  22.  
  23. int det(int n, int matriz[][n]) {
  24. int i, j, k, x, y, soma=0, aux[n-1][n-1];
  25. zerar(n, aux);
  26. if (n < 1) {}
  27. else if (n == 1) {
  28. return matriz[0][0];
  29. }
  30. else if (n == 2) {
  31. soma = (matriz[0][0] * matriz[1][1]) - (matriz[0][1] * matriz[1][0]);
  32. return soma;
  33. }
  34. else {
  35. for (i=0; i<n; i++) {
  36. for (j=1, x=0; j<n; j++) {
  37. for (k=0, y=0; k<n; k++) {
  38. if (k == i) {
  39. continue;
  40. }
  41. else {
  42. printf("\n\n");
  43. printm(n-1, aux);
  44. aux[x][y] = matriz[j][k];
  45. printf("\nx=%d, y=%d, j=%d, k=%d, i=%d\n", x, y, j, k, i);
  46. y++;
  47. }
  48. }
  49. x++;
  50. }
  51. soma += matriz[0][i]*pow(-1, i+2)*det(n-1, aux);
  52. }
  53. return soma;
  54. }
  55. }
  56.  
  57.  
  58. int main()
  59. {
  60. int m[3][3] = {{4, 3, 2}, {1, 4, 5}, {2, 1, 2}};
  61. det(3, m);
  62. printf("%d", det(3, m));
  63. printf("\n\n");
  64. printm(3, m);
  65. printf("\n\n");
  66. }
Success #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout

	0	0
	0	0

x=0, y=0, j=1, k=1, i=0


	4	0
	0	0

x=0, y=1, j=1, k=2, i=0


	4	5
	0	0

x=1, y=0, j=2, k=1, i=0


	4	5
	1	0

x=1, y=1, j=2, k=2, i=0


	4	5
	1	2

x=0, y=0, j=1, k=0, i=1


	1	5
	1	2

x=0, y=1, j=1, k=2, i=1


	1	5
	1	2

x=1, y=0, j=2, k=0, i=1


	1	5
	2	2

x=1, y=1, j=2, k=2, i=1


	1	5
	2	2

x=0, y=0, j=1, k=0, i=2


	1	5
	2	2

x=0, y=1, j=1, k=1, i=2


	1	4
	2	2

x=1, y=0, j=2, k=0, i=2


	1	4
	2	2

x=1, y=1, j=2, k=1, i=2


	0	0
	0	0

x=0, y=0, j=1, k=1, i=0


	4	0
	0	0

x=0, y=1, j=1, k=2, i=0


	4	5
	0	0

x=1, y=0, j=2, k=1, i=0


	4	5
	1	0

x=1, y=1, j=2, k=2, i=0


	4	5
	1	2

x=0, y=0, j=1, k=0, i=1


	1	5
	1	2

x=0, y=1, j=1, k=2, i=1


	1	5
	1	2

x=1, y=0, j=2, k=0, i=1


	1	5
	2	2

x=1, y=1, j=2, k=2, i=1


	1	5
	2	2

x=0, y=0, j=1, k=0, i=2


	1	5
	2	2

x=0, y=1, j=1, k=1, i=2


	1	4
	2	2

x=1, y=0, j=2, k=0, i=2


	1	4
	2	2

x=1, y=1, j=2, k=1, i=2
22

	4	3	2
	1	4	5
	2	1	2