0% found this document useful (0 votes)
7 views3 pages

Matrix Operations and Solutions

Uploaded by

asowadnoor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Matrix Operations and Solutions

Uploaded by

asowadnoor
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

Common questions

Powered by AI

The program explicitly checks that the number of columns in the first matrix matches the number of rows in the second matrix before performing multiplication. If the dimensions do not align, it outputs an error message stating that the operation is not possible due to a mismatch in dimensions, thus ensuring that only compatible matrices are multiplied .

The main constraint for multiplying two matrices is that the number of columns in the first matrix must equal the number of rows in the second matrix. This is necessary because the elements of the rows of the first matrix are multiplied by the corresponding elements of the columns of the second matrix, and each resulting product is then summed to contribute to an entry in the resulting matrix .

The process involves first forming an augmented matrix with the coefficients of the system on the left and the identity matrix on the right. Gaussian elimination is applied to convert the left part to an identity matrix, while altering the right part to what becomes the inverse of the original coefficient matrix. The result is then obtained by multiplying this inverse matrix by the vector of constants, yielding the solutions for the variables .

The result of a matrix transpose is a matrix with its rows and columns exchanged. If the original matrix has dimensions r x c, the transposed matrix will have dimensions c x r. This means that what was previously a row in the original matrix becomes a column in the transposed matrix, and vice versa .

The program uses a switch statement to handle inputs corresponding to various matrix operations and includes a default case that catches any invalid input. This default case outputs an error message and prompts the user to try again. This approach is effective as it provides immediate feedback on input errors and prevents the program from attempting operations outside predefined cases .

The diagonal element must be non-zero during Gaussian elimination to avoid division by zero, which would make the computation of the matrix inverse infeasible. A zero diagonal element indicates a singular matrix, which cannot be inverted, thereby preventing the application of the matrix method to solve the system of equations .

Matrix addition is not possible when the two matrices involved have different dimensions. The logic implemented in the program checks this requirement and only allows addition if the dimensions match (i.e., each matrix must have the same number of rows and the same number of columns).

When the user chooses to transpose a 2x3 matrix with elements '1 2 3 4 5 6', the output will be a 3x2 matrix, resulting in: 1 4 2 5 3 6. Each row of the original matrix becomes a column of the transposed matrix .

Using fixed-size arrays (10x10) limits flexibility as the program cannot handle matrices of sizes larger than this pre-defined maximum. It also means the program allocates memory for matrices regardless of their actual size, which could be inefficient. However, this approach simplifies memory management and avoids potential runtime errors related to dynamic memory allocation, offering predictable performance .

Subtraction of two matrices requires that both matrices have the same dimensions. This ensures that each element in one matrix has a corresponding element in the same position in the other matrix. During subtraction, each element of the first matrix is subtracted by the corresponding element in the second matrix to form the resulting matrix .

You might also like