Print the Determinant
In this final step, we'll enhance our matrix determinant program by adding more detailed output and error handling to improve the user experience.
Update the matrix_determinant.c
file:
nano matrix_determinant.c
Replace the previous content with the following improved code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SIZE 10
// Function to calculate determinant recursively
int determinant(int matrix[MAX_SIZE][MAX_SIZE], int n) {
int det = 0;
int submatrix[MAX_SIZE][MAX_SIZE];
// Base case for 1x1 matrix
if (n == 1) {
return matrix[0][0];
}
// Base case for 2x2 matrix
if (n == 2) {
return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
}
// Recursive case for larger matrices
int sign = 1;
for (int k = 0; k < n; k++) {
// Create submatrix
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == k) continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
// Recursive calculation
det += sign * matrix[0][k] * determinant(submatrix, n - 1);
sign = -sign;
}
return det;
}
// Function to print matrix with formatting
void print_matrix(int matrix[MAX_SIZE][MAX_SIZE], int n) {
printf("\nMatrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%5d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int n, i, j;
int matrix[MAX_SIZE][MAX_SIZE];
bool valid_input = false;
while (!valid_input) {
// Read matrix dimension
printf("Enter the size of the square matrix (1-%d): ", MAX_SIZE);
if (scanf("%d", &n) != 1) {
printf("Invalid input. Please enter a number.\n");
while (getchar() != '\n'); // Clear input buffer
continue;
}
// Validate matrix size
if (n < 1 || n > MAX_SIZE) {
printf("Invalid matrix size. Please enter a size between 1 and %d.\n", MAX_SIZE);
continue;
}
valid_input = true;
}
// Read matrix elements
printf("Enter the matrix elements:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Enter element [%d][%d]: ", i, j);
while (scanf("%d", &matrix[i][j]) != 1) {
printf("Invalid input. Please enter an integer for element [%d][%d]: ", i, j);
while (getchar() != '\n'); // Clear input buffer
}
}
}
// Print the input matrix
print_matrix(matrix, n);
// Calculate and print determinant
int det = determinant(matrix, n);
// Formatted determinant output
printf("\n--- Determinant Calculation ---\n");
printf("Matrix Dimension: %d x %d\n", n, n);
printf("Determinant: %d\n", det);
return 0;
}
Compile and run the program:
gcc matrix_determinant.c -o matrix_determinant
./matrix_determinant
Example output:
Enter the size of the square matrix (1-10): 3
Enter the matrix elements:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [0][2]: 3
Enter element [1][0]: 4
Enter element [1][1]: 5
Enter element [1][2]: 6
Enter element [2][0]: 7
Enter element [2][1]: 8
Enter element [2][2]: 9
Matrix:
1 2 3
4 5 6
7 8 9
--- Determinant Calculation ---
Matrix Dimension: 3 x 3
Determinant: 0
Key improvements:
- Added input validation to handle invalid inputs
- Created a separate function to print the matrix with better formatting
- Enhanced output to show matrix dimension and determinant clearly
- Improved error handling for user inputs