#include double det(double arr[][3]) { double ans; ans=(((arr[0][0]*arr[1][1]*arr[2][2])+ (arr[0][1]*arr[1][2]*arr[2][0])+ (arr[0][2]*arr[1][0]*arr[2][1]))- ((arr[0][2]*arr[1][1]*arr[2][0])+ (arr[0][0]*arr[1][2]*arr[2][1])+ (arr[0][1]*arr[1][0]*arr[2][2]))); return ans; } void sol(double x,double y,double z) { printf("Solution:\n"); printf("x = %g\n",x); printf("y = %g\n",y); printf("z = %g\n",z); } void accept(double arr [][3],double arr1[3]) { int i,j; printf("\n\nAccepted equations:\n"); for (i=0;i<3;i++) { j=0; if (arr[i][j]!=0 || arr[i][j+1]!=0 || arr[i][j+2]!=0) { for (j=0;j<3;j++) { switch (j) { case 0: if (arr[i][j]!=0) if (arr[i][j]==1) printf("x",arr[i][j]); else printf("%gx",arr[i][j]); break; case 1: if (arr[i][j]>0) { if ((arr[i][j-1]!=0) && (arr[i][j]==1)) printf("+y",arr[i][j]); else if (arr[i][j-1]!=0) printf("+%gy",arr[i][j]); else printf("%gy",arr[i][j]); } else if (arr[i][j]<0) printf("%gy",arr[i][j]); break; case 2: if (arr[i][j]>0) { if (((arr[i][j-1]!=0) || (arr[i][j-2]!=0)) && (arr[i][j]==1)) printf("+z",arr[i][j]); else if (arr[i][j-1]!=0) printf("+%gz",arr[i][j]); else printf("%gz",arr[i][j]); } else if (arr[i][j]<0) printf("%gz",arr[i][j]); break; } } } else printf("0"); printf("=%g\n",arr1[i]); } } void input(double A[3][3],double X[3]) { printf("Enter coefficients and constants:\n"); fflush(stdin); scanf("{{%lf,%lf,%lf},{%lf,%lf,%lf},{%lf,%lf,%lf}}={%lf,%lf,%lf}", &A[0][0], &A[0][1], &A[0][2], &A[1][0], &A[1][1], &A[1][2], &A[2][0], &A[2][1], &A[2][2], &X[0], &X[1], &X[2]); } int main() { double coef[3][3]={0}; double temp[3][3]={0}; double con[3]={0}; double main_det=0,solX=0,solY=0,solZ=0; int i=0,j=0,choice=0; while (choice!=2) { printf("\nMain Menu\n---------\n"); printf("(1)Solve 3x3 linear equations system\n"); printf("(2)Quit\n"); printf("Enter your choice:"); fflush(stdin); scanf("%d", &choice); if (choice==1) { input(coef,con); accept(coef,con); main_det=det(coef); if (main_det==0) { printf("\nThe metrix doesn't have one solution\n"); } else { for (i=0;i<3;i++) for (j=0;j<3;j++) temp[i][j]=coef[i][j]; for (i=0;i<3;i++) temp[i][0]=con[i]; solX=det(temp)/main_det; for (i=0;i<3;i++) { temp[i][1]=con[i]; temp[i][0]=coef[i][0]; } solY=det(temp)/main_det; for (i=0;i<3;i++) { temp[i][2]=con[i]; temp[i][1]=coef[i][1]; } solZ=det(temp)/main_det; sol(solX,solY,solZ); choice=0; } } else if (choice==2) printf("bye bye...\n"); else printf("\nIncorrect Choice\n\n"); } return 0; }