Print Solutions for Variables
In this step, you will complete the Cramer's Rule implementation by calculating and printing the solutions for variables in a system of linear equations.
Open the existing source file:
cd ~/project
nano cramer_solver.c
Update the code with Cramer's Rule solution calculation:
#include <stdio.h>
#define MAX_SIZE 3
// Previous computeDeterminant function remains the same
float computeDeterminant(float matrix[MAX_SIZE][MAX_SIZE], int n) {
if (n == 1) {
return matrix[0][0];
}
if (n == 2) {
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
}
float det = 0;
float submatrix[MAX_SIZE][MAX_SIZE];
int sign = 1;
for (int x = 0; x < n; x++) {
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x) continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
det += sign * matrix[0][x] * computeDeterminant(submatrix, n - 1);
sign = -sign;
}
return det;
}
// Function to solve linear equations using Cramer's Rule
void solveUsingCramersRule(float matrix[MAX_SIZE][MAX_SIZE], float constants[MAX_SIZE], int n) {
float mainDeterminant = computeDeterminant(matrix, n);
// Check if the system has a unique solution
if (mainDeterminant == 0) {
printf("The system has no unique solution (determinant is zero).\n");
return;
}
// Create temporary matrices for each variable
float tempMatrix[MAX_SIZE][MAX_SIZE];
float solutions[MAX_SIZE];
// Calculate solutions for each variable
for (int i = 0; i < n; i++) {
// Copy original matrix
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
tempMatrix[j][k] = matrix[j][k];
}
}
// Replace i-th column with constants
for (int j = 0; j < n; j++) {
tempMatrix[j][i] = constants[j];
}
// Calculate determinant for this variable
solutions[i] = computeDeterminant(tempMatrix, n) / mainDeterminant;
}
// Print solutions
printf("\nSolutions:\n");
for (int i = 0; i < n; i++) {
printf("x%d = %.2f\n", i+1, solutions[i]);
}
}
int main() {
float matrix[MAX_SIZE][MAX_SIZE];
float constants[MAX_SIZE];
int n;
// Input code remains the same
printf("Enter the number of equations (max 3): ");
scanf("%d", &n);
// Input coefficients
printf("Enter the coefficients of the matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("Enter coefficient a[%d][%d]: ", i+1, j+1);
scanf("%f", &matrix[i][j]);
}
}
// Input constants
printf("Enter the constants:\n");
for (int i = 0; i < n; i++) {
printf("Enter constant b[%d]: ", i+1);
scanf("%f", &constants[i]);
}
// Solve using Cramer's Rule
solveUsingCramersRule(matrix, constants, n);
return 0;
}
Compile the updated program:
gcc -o cramer_solver cramer_solver.c
Run the program:
./cramer_solver
Example output:
Enter the number of equations (max 3): 2
Enter the coefficients of the matrix:
Enter coefficient a[1][1]: 2
Enter coefficient a[1][2]: 1
Enter coefficient a[2][1]: -3
Enter coefficient a[2][2]: 4
Enter the constants:
Enter constant b[1]: 4
Enter constant b[2]: 5
Solutions:
x1 = 2.00
x2 = 1.00
Key points about the solution calculation:
- The
solveUsingCramersRule()
function implements Cramer's Rule.
- It checks if the system has a unique solution by verifying the main determinant.
- For each variable, it creates a temporary matrix and calculates its determinant.
- Solutions are computed by dividing each variable's determinant by the main determinant.
The program now fully solves a system of linear equations using Cramer's Rule.