Corresponding Code scanf("%d%d", &r1, &c1);
#include <stdio.h> printf("Enter first matrix:\n");
// Function prototypes inputMatrix(A, r1, c1);
void inputMatrix(int mat[10][10], int r, int c); printf("Enter second matrix:\n");
void printMatrix(double mat[10][10], int r, int c); inputMatrix(B, r1, c1);
void addMatrix(int a[10][10], int b[10][10], int r, int c); subMatrix(A, B, r1, c1);
void subMatrix(int a[10][10], int b[10][10], int r, int c); break;
void mulMatrix(int a[10][10], int r1, int c1, int b[10][10], int
r2, int c2); case 5: // Linear Equation
void transposeMatrix(int a[10][10], int r, int c); solveLinearEquations();
void solveLinearEquations(); break;
int main() { case 0:
int choice; printf("Exiting program. Goodbye!\n");
int r1, c1, r2, c2; return 0;
int A[10][10], B[10][10];
default:
while(1) { printf("Invalid choice! Try again.\n");
printf("\n=== Math Presentation Topic ===\n"); }
printf("1) Addition of Matrix\n"); }
printf("2) Multiplication of Matrix\n");
printf("3) Transpose of Matrix\n"); return 0;
printf("4) Subtraction of Matrix\n"); }
printf("5) Solution of Linear Equation by applying Matrix
Method\n"); // Function Definitions
printf("0) Exit\n"); void inputMatrix(int mat[10][10], int r, int c) {
printf("Enter your choice: "); for(int i=0; i<r; i++) {
scanf("%d", &choice); for(int j=0; j<c; j++) {
scanf("%d", &mat[i][j]);
switch(choice) { }
case 1: // Addition }
printf("Enter rows and columns of matrices: "); }
scanf("%d%d", &r1, &c1);
printf("Enter first matrix:\n"); void printMatrix(double mat[10][10], int r, int c) {
inputMatrix(A, r1, c1); for(int i=0; i<r; i++) {
printf("Enter second matrix:\n"); for(int j=0; j<c; j++) {
inputMatrix(B, r1, c1); printf("%8.3lf ", mat[i][j]);
addMatrix(A, B, r1, c1); }
break; printf("\n");
}
case 2: // Multiplication }
printf("Enter rows and columns of first matrix: ");
scanf("%d%d", &r1, &c1); void addMatrix(int a[10][10], int b[10][10], int r, int c) {
printf("Enter first matrix:\n"); int result[10][10];
inputMatrix(A, r1, c1); for(int i=0; i<r; i++)
for(int j=0; j<c; j++)
printf("Enter rows and columns of second matrix: "); result[i][j] = a[i][j] + b[i][j];
scanf("%d%d", &r2, &c2);
printf("Enter second matrix:\n"); printf("Result of Addition:\n");
inputMatrix(B, r2, c2); for(int i=0; i<r; i++) {
for(int j=0; j<c; j++) printf("%d ", result[i][j]);
mulMatrix(A, r1, c1, B, r2, c2); printf("\n");
break; }
}
case 3: // Transpose
printf("Enter rows and columns of matrix: "); void subMatrix(int a[10][10], int b[10][10], int r, int c) {
scanf("%d%d", &r1, &c1); int result[10][10];
printf("Enter matrix:\n"); for(int i=0; i<r; i++)
inputMatrix(A, r1, c1); for(int j=0; j<c; j++)
transposeMatrix(A, r1, c1); result[i][j] = a[i][j] - b[i][j];
break;
printf("Result of Subtraction:\n");
case 4: // Subtraction for(int i=0; i<r; i++) {
printf("Enter rows and columns of matrices: "); for(int j=0; j<c; j++) printf("%d ", result[i][j]);
printf("\n");
} }
}
// Gaussian elimination to find inverse
void mulMatrix(int a[10][10], int r1, int c1, int b[10][10], int for(int i=0; i<n; i++) {
r2, int c2) { if(aug[i][i] == 0) {
if(c1 != r2) { printf("The operation is not possible (singular matrix).\
printf("The operation is not possible (matrix dimensions n");
mismatch).\n"); return;
return; }
}
int result[10][10] = {0}; double diag = aug[i][i];
for(int i=0; i<r1; i++) { for(int j=0; j<2*n; j++) aug[i][j] /= diag;
for(int j=0; j<c2; j++) {
for(int k=0; k<c1; k++) { for(int k=0; k<n; k++) {
result[i][j] += a[i][k] * b[k][j]; if(k != i) {
} double factor = aug[k][i];
} for(int j=0; j<2*n; j++) {
} aug[k][j] -= factor * aug[i][j];
printf("Result of Multiplication:\n"); }
for(int i=0; i<r1; i++) { }
for(int j=0; j<c2; j++) printf("%d ", result[i][j]); }
printf("\n"); }
}
} // Extract inverse from augmented matrix
double inv[10][10];
void transposeMatrix(int a[10][10], int r, int c) { for(int i=0; i<n; i++)
int result[10][10]; for(int j=0; j<n; j++)
for(int i=0; i<r; i++) inv[i][j] = aug[i][j+n];
for(int j=0; j<c; j++)
result[j][i] = a[i][j]; // Multiply inverse(A) * B
double X[10] = {0};
printf("Transpose of Matrix:\n"); for(int i=0; i<n; i++) {
for(int i=0; i<c; i++) { for(int j=0; j<n; j++) {
for(int j=0; j<r; j++) printf("%d ", result[i][j]); X[i] += inv[i][j] * B[j];
printf("\n"); }
} }
}
printf("Solution of Linear Equations:\n");
void solveLinearEquations() { for(int i=0; i<n; i++)
int n; printf("x%d = %.3lf\n", i+1, X[i]);
printf("Enter order of system (2 or 3): "); }
scanf("%d", &n);
if(n != 2 && n != 3) {
printf("Only 2x2 or 3x3 systems are supported.\n");
return;
}
double A[10][10], B[10], aug[10][20];
printf("Enter coefficient matrix A (%dx%d):\n", n, n);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
scanf("%lf", &A[i][j]);
printf("Enter constant vector B (%dx1):\n", n);
for(int i=0; i<n; i++)
scanf("%lf", &B[i]);
// Create augmented matrix [A | I]
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
aug[i][j] = A[i][j];
}
for(int j=n; j<2*n; j++) {
aug[i][j] = (i == (j-n)) ? 1.0 : 0.0;
}
Full Program Run (All Test Cases) 5) Solution of Linear Equation by applying Matrix Method
=== Math Presentation Topic === 0) Exit
1) Addition of Matrix Enter your choice: 4
2) Multiplication of Matrix Enter rows and columns of matrices: 2 2
3) Transpose of Matrix Enter first matrix:
4) Subtraction of Matrix 98
5) Solution of Linear Equation by applying Matrix Method 76
0) Exit Enter second matrix:
Enter your choice: 1 12
Enter rows and columns of matrices: 2 2 34
Enter first matrix: Result of Subtraction:
12 86
34 42
Enter second matrix:
56 === Math Presentation Topic ===
78 1) Addition of Matrix
Result of Addition: 2) Multiplication of Matrix
68 3) Transpose of Matrix
10 12 4) Subtraction of Matrix
5) Solution of Linear Equation by applying Matrix Method
=== Math Presentation Topic === 0) Exit
1) Addition of Matrix Enter your choice: 5
2) Multiplication of Matrix Enter order of system (2 or 3): 2
3) Transpose of Matrix Enter coefficient matrix A (2x2):
4) Subtraction of Matrix 23
5) Solution of Linear Equation by applying Matrix Method 12
0) Exit Enter constant vector B (2x1):
Enter your choice: 2 85
Enter rows and columns of first matrix: 2 3 Solution of Linear Equations:
Enter first matrix: x1 = 1.000
123 x2 = 2.000
456
Enter rows and columns of second matrix: 3 2 === Math Presentation Topic ===
Enter second matrix: 1) Addition of Matrix
78 2) Multiplication of Matrix
9 10 3) Transpose of Matrix
11 12 4) Subtraction of Matrix
Result of Multiplication: 5) Solution of Linear Equation by applying Matrix Method
58 64 0) Exit
139 154 Enter your choice: 5
Enter order of system (2 or 3): 3
=== Math Presentation Topic === Enter coefficient matrix A (3x3):
1) Addition of Matrix 111
2) Multiplication of Matrix 025
3) Transpose of Matrix 2 5 -1
4) Subtraction of Matrix Enter constant vector B (3x1):
5) Solution of Linear Equation by applying Matrix Method 6 -4 27
0) Exit Solution of Linear Equations:
Enter your choice: 3 x1 = 5.000
Enter rows and columns of matrix: 2 3 x2 = 3.000
Enter matrix: x3 = -2.000
123
456 === Math Presentation Topic ===
Transpose of Matrix: 1) Addition of Matrix
14 2) Multiplication of Matrix
25 3) Transpose of Matrix
36 4) Subtraction of Matrix
5) Solution of Linear Equation by applying Matrix Method
=== Math Presentation Topic === 0) Exit
1) Addition of Matrix Enter your choice: 0
2) Multiplication of Matrix Exiting program. Goodbye!
3) Transpose of Matrix
4) Subtraction of Matrix