#include <stdio.
h>
// Function to input matrix
void inputMatrix(int rows, int cols, int mat[rows][cols]) {
printf("Enter elements of matrix (%dx%d):\n", rows, cols);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &mat[i][j]);
}
}
}
// Function to display matrix
void displayMatrix(int rows, int cols, int mat[rows][cols]) {
printf("\nMatrix (%dx%d):\n", rows, cols);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}
// Function for matrix addition
void addMatrices(int rows, int cols, int A[rows][cols], int B[rows][cols], int result[rows]
[cols]) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
}
// Function for matrix multiplication
int multiplyMatrices(int r1, int c1, int A[r1][c1], int r2, int c2, int B[r2][c2], int
result[r1][c2]) {
if(c1 != r2) {
printf("\nError: Matrix multiplication not possible!\n");
printf("Columns of first matrix must equal rows of second matrix.\n");
return 0;
}
for(int i = 0; i < r1; i++) {
for(int j = 0; j < c2; j++) {
result[i][j] = 0;
for(int k = 0; k < c1; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
return 1;
}
// Function for matrix transpose
void transposeMatrix(int rows, int cols, int mat[rows][cols], int trans[cols][rows]) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
trans[j][i] = mat[i][j];
}
}
}
// Operation 1: Addition
void performAddition() {
int rows, cols;
printf("\n--- MATRIX ADDITION ---\n");
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
int A[rows][cols], B[rows][cols], result[rows][cols];
printf("\nMatrix A:\n");
inputMatrix(rows, cols, A);
printf("\nMatrix B:\n");
inputMatrix(rows, cols, B);
addMatrices(rows, cols, A, B, result);
printf("\n--- RESULT ---");
displayMatrix(rows, cols, A);
printf("+");
displayMatrix(rows, cols, B);
printf("=");
displayMatrix(rows, cols, result);
}
// Operation 2: Multiplication
void performMultiplication() {
int r1, c1, r2, c2;
printf("\n--- MATRIX MULTIPLICATION ---\n");
printf("Matrix A dimensions:\n");
printf("Enter number of rows: ");
scanf("%d", &r1);
printf("Enter number of columns: ");
scanf("%d", &c1);
printf("\nMatrix B dimensions:\n");
printf("Enter number of rows: ");
scanf("%d", &r2);
printf("Enter number of columns: ");
scanf("%d", &c2);
int A[r1][c1], B[r2][c2], result[r1][c2];
printf("\nMatrix A:\n");
inputMatrix(r1, c1, A);
printf("\nMatrix B:\n");
inputMatrix(r2, c2, B);
if(multiplyMatrices(r1, c1, A, r2, c2, B, result)) {
printf("\n--- RESULT ---");
displayMatrix(r1, c1, A);
printf("×");
displayMatrix(r2, c2, B);
printf("=");
displayMatrix(r1, c2, result);
}
}
// Operation 3: Transpose
void performTranspose() {
int rows, cols;
printf("\n--- MATRIX TRANSPOSE ---\n");
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
int mat[rows][cols], trans[cols][rows];
inputMatrix(rows, cols, mat);
transposeMatrix(rows, cols, mat, trans);
printf("\n--- RESULT ---");
printf("\nOriginal Matrix:");
displayMatrix(rows, cols, mat);
printf("\nTranspose Matrix:");
displayMatrix(cols, rows, trans);
}
int main() {
int choice;
do {
printf("\n");
printf("=====================================\n");
printf(" MATRIX OPERATIONS MENU\n");
printf("=====================================\n");
printf("1. Addition of Two Matrices\n");
printf("2. Multiplication of Two Matrices\n");
printf("3. Transpose of Matrix\n");
printf("4. Exit\n");
printf("=====================================\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
performAddition();
break;
case 2:
performMultiplication();
break;
case 3:
performTranspose();
break;
case 4:
printf("\nExiting program. Goodbye!\n");
break;
default:
printf("\nInvalid choice! Enter 1-4.\n");
}
} while(choice != 4);
return 0;
}