candrei98

Untitled

Jan 14th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. #include <conio.h>
  2. #include <stdio.h>
  3.  
  4. int det(int A[100][100], int n)
  5. {
  6. int Minor[100][100];
  7. int i,j,k,c1,c2;
  8. int determinant;
  9. int c[100];
  10. int O=1;
  11. determinant = 0;
  12. if(n == 2)
  13. {
  14.  
  15. determinant = A[0][0]*A[1][1]-A[0][1]*A[1][0];
  16. return determinant;
  17. }
  18. else
  19. {
  20. for(i = 0 ; i < n ; i++)
  21. {
  22. c1 = 0, c2 = 0;
  23. for(j = 0 ; j < n ; j++)
  24. {
  25. for(k = 0 ; k < n ; k++)
  26. {
  27. if(j != 0 && k != i)
  28. {
  29. Minor[c1][c2] = A[j][k];
  30. c2++;
  31. if(c2>n-2)
  32. {
  33. c1++;
  34. c2=0;
  35. }
  36. }
  37. }
  38. }
  39. determinant = determinant + O*(A[0][i]*det(Minor,n-1));
  40. O=-1*O;
  41. }
  42. }
  43. return determinant;
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. double determinantOfMatrix(double mat[3][3])
  80. {
  81. double ans;
  82. ans = mat[0][0] * (mat[1][1] * mat[2][2] - mat[2][1] * mat[1][2])
  83. - mat[0][1] * (mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0])
  84. + mat[0][2] * (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]);
  85. return ans;
  86. }
  87.  
  88.  
  89. void findSolution(double coeff[3][4])
  90. {
  91.  
  92. double d[3][3] = {
  93. { coeff[0][0], coeff[0][1], coeff[0][2] },
  94. { coeff[1][0], coeff[1][1], coeff[1][2] },
  95. { coeff[2][0], coeff[2][1], coeff[2][2] },
  96. };
  97.  
  98. double d1[3][3] = {
  99. { coeff[0][3], coeff[0][1], coeff[0][2] },
  100. { coeff[1][3], coeff[1][1], coeff[1][2] },
  101. { coeff[2][3], coeff[2][1], coeff[2][2] },
  102. };
  103.  
  104. double d2[3][3] = {
  105. { coeff[0][0], coeff[0][3], coeff[0][2] },
  106. { coeff[1][0], coeff[1][3], coeff[1][2] },
  107. { coeff[2][0], coeff[2][3], coeff[2][2] },
  108. };
  109.  
  110. double d3[3][3] = {
  111. { coeff[0][0], coeff[0][1], coeff[0][3] },
  112. { coeff[1][0], coeff[1][1], coeff[1][3] },
  113. { coeff[2][0], coeff[2][1], coeff[2][3] },
  114. };
  115.  
  116.  
  117. double D = determinantOfMatrix(d);
  118. double D1 = determinantOfMatrix(d1);
  119. double D2 = determinantOfMatrix(d2);
  120. double D3 = determinantOfMatrix(d3);
  121.  
  122.  
  123. // Case 1
  124. if (D != 0) {
  125. printf("\nMatricea este inversabila!\n");
  126. double x = D1 / D;
  127. double y = D2 / D;
  128. double z = D3 / D;
  129. printf("Determinatul: %.1lf \n", D);
  130. printf("DeterminatX : %.1lf \n", D1);
  131. printf("DeterminatY : %.1lf \n", D2);
  132. printf("DeterminatZ : %.1lf \n", D3);
  133.  
  134. printf("X : %.1lf\n", x);
  135. printf("Y : %.1lf\n", y);
  136. printf("Z: %.1lf\n", z);
  137. }
  138.  
  139. else {
  140.  
  141. if (D1 == 0 && D2 == 0 && D3 == 0)
  142. {
  143. printf("Matricea nu este inversabila!\n");
  144. printf("Nu se poate rezolva cu metoda Cramer\n");
  145. }
  146. else
  147. {
  148. if (D1 != 0 || D2 != 0 || D3 != 0)
  149. printf("Fara solutii\n");
  150. }
  151. }
  152. }
  153.  
  154.  
  155. int main()
  156. {
  157.  
  158.  
  159. double coeff[3][4];
  160. int A[100][100];
  161. int i,j,k,n,res;
  162. printf("Introdu ordinul matricei: ");
  163. scanf("%d",&n);
  164. printf("\nIntrodu elementele matricei: \n");
  165. for(i = 0 ; i < n ; i++)
  166. {
  167. for(j = 0 ; j < n ; j++)
  168. {
  169. scanf("%d",&A[i][j]);
  170. }
  171. }
  172. for(i = 0 ; i < n ; i++)
  173. {
  174. for(j = 0 ; j < n ; j++)
  175. {
  176. printf("%5d",A[i][j]);
  177. }
  178. printf("\n");
  179. }
  180.  
  181. printf("\nIntrodu ecuatiile de forma urmatoare !\n");
  182. printf("\nax+by+cz=du\n");
  183. for(int i=0;i<3;i++)
  184. for(int j=0;j<4;j++)
  185. scanf("%lf",&coeff[i][j]);
  186. printf("\n");
  187. printf("\nMatricea introdusa !\n");
  188. printf("\n\n");
  189. for(int i=0;i<3;i++)
  190. {
  191. for(int j=0;j<3;j++)
  192. {
  193. printf("%3.0lf ",coeff[i][j]);
  194. }
  195. printf("\n");
  196. }
  197. printf("Matricea unitate !\n");
  198. for(int i=0,j=3;i<3;i++)
  199. {
  200. printf("%3.0lf\n",coeff[i][j]);
  201. }
  202. findSolution(coeff);
  203. res = det(A,n);
  204. printf("Determinantul rezultat din prima introducere este Det = %d",res);
  205. return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment