Solve system of two equations with two unknowns
we solve the linear system using Cramer's rule:
int main(int argc, char** argv) {
/* we solve the linear system
* ax+by=e
* cx+dy=f
*/
if(argc != 7) {
cerr<<"Cramer equations system: error,"
" we need a,b,c,d,e,f parameters.\n";
return -1;
}
double a,b,e;
double c,d,f;
sscanf(argv[1],"%lf",&a);
sscanf(argv[2],"%lf",&b);
sscanf(argv[3],"%lf",&e);
sscanf(argv[4],"%lf",&c);
sscanf(argv[5],"%lf",&d);
sscanf(argv[6],"%lf",&f);
double determinant = a*d - b*c;
if(determinant != 0) {
double x = (e*d - b*f)/determinant;
double y = (a*f - e*c)/determinant;
printf("Cramer equations system: result, x = %f, y = %f\n", x, y);
} else {
printf("Cramer equations system: determinant is zero\n"
"there are either no solutions or many solutions exist.\n");
}
return 0;
}
./cramer_equation_system 1 2 5 1 -1 -1
Cramer equations system: result, x = 1.000000, y = 2.000000