Print Solutions or Special Cases
In this final step, you will enhance the program to provide more detailed output for different types of solutions in a system of two linear equations.
Open the existing file and make final modifications:
cd ~/project
nano linear_equations.c
Update the code with improved output formatting:
#include <stdio.h>
#include <math.h>
// Function to calculate determinant
float determinant(float a1, float b1, float a2, float b2) {
return a1 * b2 - a2 * b1;
}
int main() {
float a1, b1, c1; // Coefficients for first equation
float a2, b2, c2; // Coefficients for second equation
float det, detX, detY;
float x, y;
float EPSILON = 1e-6; // Small value for floating-point comparison
// Prompt and read coefficients for the first equation
printf("Linear Equation Solver\n");
printf("Enter coefficients for the first equation (ax + by = c):\n");
printf("a1: ");
scanf("%f", &a1);
printf("b1: ");
scanf("%f", &b1);
printf("c1: ");
scanf("%f", &c1);
// Prompt and read coefficients for the second equation
printf("Enter coefficients for the second equation (ax + by = c):\n");
printf("a2: ");
scanf("%f", &a2);
printf("b2: ");
scanf("%f", &b2);
printf("c2: ");
scanf("%f", &c2);
// Print input equations
printf("\nInput Equations:\n");
printf("Equation 1: %.2fx + %.2fy = %.2f\n", a1, b1, c1);
printf("Equation 2: %.2fx + %.2fy = %.2f\n", a2, b2, c2);
// Calculate main determinant
det = determinant(a1, b1, a2, b2);
// Determine and print solution type
if (fabs(det) > EPSILON) {
// Unique solution case
detX = determinant(c1, b1, c2, b2);
detY = determinant(a1, c1, a2, c2);
x = detX / det;
y = detY / det;
printf("\n--- Solution Type: Unique Solution ---\n");
printf("Solution:\n");
printf("x = %.2f\n", x);
printf("y = %.2f\n", y);
} else {
// Check for no solution or infinite solutions
detX = determinant(c1, b1, c2, b2);
detY = determinant(a1, c1, a2, c2);
if (fabs(detX) > EPSILON || fabs(detY) > EPSILON) {
printf("\n--- Solution Type: No Solution ---\n");
printf("The system of equations has no solution.\n");
printf("Equations are inconsistent and parallel.\n");
} else {
printf("\n--- Solution Type: Infinite Solutions ---\n");
printf("The system of equations has infinitely many solutions.\n");
printf("Equations are equivalent and dependent.\n");
}
}
return 0;
}
Compile and run the program:
gcc linear_equations.c -o linear_equations
./linear_equations
Example output for unique solution:
Linear Equation Solver
Enter coefficients for the first equation (ax + by = c):
a1: 2
b1: 3
c1: 8
Enter coefficients for the second equation (ax + by = c):
a2: 1
b2: 4
c2: 10
Input Equations:
Equation 1: 2.00x + 3.00y = 8.00
Equation 2: 1.00x + 4.00y = 10.00
--- Solution Type: Unique Solution ---
Solution:
x = 2.00
y = 2.00
Example output for no solution:
Linear Equation Solver
Enter coefficients for the first equation (ax + by = c):
a1: 2
b1: 3
c1: 8
Enter coefficients for the second equation (ax + by = c):
a2: 4
b2: 6
c2: 16
Input Equations:
Equation 1: 2.00x + 3.00y = 8.00
Equation 2: 4.00x + 6.00y = 16.00
--- Solution Type: No Solution ---
The system of equations has no solution.
Equations are inconsistent and parallel.