Now a days almost all scientific calculators have the property of solving a system of simultaneous equations. Code a program to do the same for the following and find the value of X,Y and Z.
a1X + b1Y + c1Z = d1
a2X + b2Y + c2Z = d2
a3X + b3Y + c3Z = d3
Output:
Print 0 in case the system is inconsistent and 1 in case the system is consistent and has infinitely many solutions.
In case the system is consistent and has a unique solution, print 3 space separated integers denoting the floor values of X, Y and Z respectively.
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
//code
int t;
cin>>t;
while(t--){
double mat[3][3],d[3],inv[3][3];
int i, j f=1;
double determinant = 0;
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
cin>>mat[i][j];
}
cin>>d[i];
}
//finding determinant
for(i = 0; i < 3; i++)
determinant = determinant + (mat[0][i] * (mat[1][(i+1)%3] * mat[2][(i+2)%3] - mat[1][(i+2)%3] * mat[2][(i+1)%3]));
if(determinant==0){
f=0;
f=0;
cout<<"0"<<endl;
}
else{
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
inv[i][j]=((mat[(j+1)%3][(i+1)%3] * mat[(j+2)%3][(i+2)%3]) - (mat[(j+1)%3][(i+2)%3] * mat[(j+2)%3][(i+1)%3]))/ determinant;
}
}
double ans[3]={0};
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
ans[i]+=inv[i][j]*d[j];
}
}
if(f){
for(i = 0; i < 3; i++){
cout<<floor(ans[i])<<" ";
}
cout<<endl;
}
}
//cout<<"\n\nInverse of matrix is: \n";
}
return 0;
}