Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <stdio.h>
- int det(int A[100][100], int n)
- {
- int Minor[100][100];
- int i,j,k,c1,c2;
- int determinant;
- int c[100];
- int O=1;
- determinant = 0;
- if(n == 2)
- {
- determinant = A[0][0]*A[1][1]-A[0][1]*A[1][0];
- return determinant;
- }
- else
- {
- for(i = 0 ; i < n ; i++)
- {
- c1 = 0, c2 = 0;
- for(j = 0 ; j < n ; j++)
- {
- for(k = 0 ; k < n ; k++)
- {
- if(j != 0 && k != i)
- {
- Minor[c1][c2] = A[j][k];
- c2++;
- if(c2>n-2)
- {
- c1++;
- c2=0;
- }
- }
- }
- }
- determinant = determinant + O*(A[0][i]*det(Minor,n-1));
- O=-1*O;
- }
- }
- return determinant;
- }
- double determinantOfMatrix(double mat[3][3])
- {
- double ans;
- ans = mat[0][0] * (mat[1][1] * mat[2][2] - mat[2][1] * mat[1][2])
- - mat[0][1] * (mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0])
- + mat[0][2] * (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]);
- return ans;
- }
- void findSolution(double coeff[3][4])
- {
- double d[3][3] = {
- { coeff[0][0], coeff[0][1], coeff[0][2] },
- { coeff[1][0], coeff[1][1], coeff[1][2] },
- { coeff[2][0], coeff[2][1], coeff[2][2] },
- };
- double d1[3][3] = {
- { coeff[0][3], coeff[0][1], coeff[0][2] },
- { coeff[1][3], coeff[1][1], coeff[1][2] },
- { coeff[2][3], coeff[2][1], coeff[2][2] },
- };
- double d2[3][3] = {
- { coeff[0][0], coeff[0][3], coeff[0][2] },
- { coeff[1][0], coeff[1][3], coeff[1][2] },
- { coeff[2][0], coeff[2][3], coeff[2][2] },
- };
- double d3[3][3] = {
- { coeff[0][0], coeff[0][1], coeff[0][3] },
- { coeff[1][0], coeff[1][1], coeff[1][3] },
- { coeff[2][0], coeff[2][1], coeff[2][3] },
- };
- double D = determinantOfMatrix(d);
- double D1 = determinantOfMatrix(d1);
- double D2 = determinantOfMatrix(d2);
- double D3 = determinantOfMatrix(d3);
- // Case 1
- if (D != 0) {
- printf("\nMatricea este inversabila!\n");
- double x = D1 / D;
- double y = D2 / D;
- double z = D3 / D;
- printf("Determinatul: %.1lf \n", D);
- printf("DeterminatX : %.1lf \n", D1);
- printf("DeterminatY : %.1lf \n", D2);
- printf("DeterminatZ : %.1lf \n", D3);
- printf("X : %.1lf\n", x);
- printf("Y : %.1lf\n", y);
- printf("Z: %.1lf\n", z);
- }
- else {
- if (D1 == 0 && D2 == 0 && D3 == 0)
- {
- printf("Matricea nu este inversabila!\n");
- printf("Nu se poate rezolva cu metoda Cramer\n");
- }
- else
- {
- if (D1 != 0 || D2 != 0 || D3 != 0)
- printf("Fara solutii\n");
- }
- }
- }
- int main()
- {
- double coeff[3][4];
- int A[100][100];
- int i,j,k,n,res;
- printf("Introdu ordinul matricei: ");
- scanf("%d",&n);
- printf("\nIntrodu elementele matricei: \n");
- for(i = 0 ; i < n ; i++)
- {
- for(j = 0 ; j < n ; j++)
- {
- scanf("%d",&A[i][j]);
- }
- }
- for(i = 0 ; i < n ; i++)
- {
- for(j = 0 ; j < n ; j++)
- {
- printf("%5d",A[i][j]);
- }
- printf("\n");
- }
- printf("\nIntrodu ecuatiile de forma urmatoare !\n");
- printf("\nax+by+cz=du\n");
- for(int i=0;i<3;i++)
- for(int j=0;j<4;j++)
- scanf("%lf",&coeff[i][j]);
- printf("\n");
- printf("\nMatricea introdusa !\n");
- printf("\n\n");
- for(int i=0;i<3;i++)
- {
- for(int j=0;j<3;j++)
- {
- printf("%3.0lf ",coeff[i][j]);
- }
- printf("\n");
- }
- printf("Matricea unitate !\n");
- for(int i=0,j=3;i<3;i++)
- {
- printf("%3.0lf\n",coeff[i][j]);
- }
- findSolution(coeff);
- res = det(A,n);
- printf("Determinantul rezultat din prima introducere este Det = %d",res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment