Guest User

Untitled

a guest
Nov 3rd, 2015
25
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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[10][10];
  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. }
RAW Paste Data