#include <stdio.h>
#define MAX_SIZE 10
void read_matrix(int matrix[][MAX_SIZE], int rows, int cols) {
printf("Enter matrix elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
}
void print_matrix(int matrix[][MAX_SIZE], int rows, int cols) {
printf("Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
void transpose_matrix(int matrix[][MAX_SIZE], int rows, int cols) {
int transposed[MAX_SIZE][MAX_SIZE];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposed[j][i] = matrix[i][j];
}
}
printf("Transposed matrix:\n");
print_matrix(transposed, cols, rows);
}
void add_matrices(int matrix1[][MAX_SIZE], int matrix2[][MAX_SIZE], int rows, int cols) {
int result[MAX_SIZE][MAX_SIZE];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("Sum of matrices:\n");
print_matrix(result, rows, cols);
}
void subtract_matrices(int matrix1[][MAX_SIZE], int matrix2[][MAX_SIZE], int rows, int cols) {
int result[MAX_SIZE][MAX_SIZE];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
printf("Difference of matrices:\n");
print_matrix(result, rows, cols);
}
int main() {
int matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE];
int rows1, cols1, rows2, cols2;
int choice;
printf("Enter dimensions of first matrix (rows columns): ");
scanf("%d %d", &rows1, &cols1);
read_matrix(matrix1, rows1, cols1);
printf("Enter dimensions of second matrix (rows columns): ");
scanf("%d %d", &rows2, &cols2);
read_matrix(matrix2, rows2, cols2);
printf("Select an operation:\n");
printf("1. Transpose of matrix\n");
printf("2. Addition of matrices\n");
printf("3. Subtraction of matrices\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
transpose_matrix(matrix1, rows1, cols1);
break;
case 2:
if (rows1 != rows2 || cols1 != cols2) {
printf("Error: matrices have different dimensions\n");
return 1;
}
add_matrices(matrix1, matrix2, rows1, cols1);
break;
case 3:
if (rows1 != rows2 || cols1 != cols2) {
printf("Error: matrices have different dimensions\n");
return 1;
}
subtract_matrices(matrix1, matrix2, rows1, cols1);
break;
default:
printf("Invalid choice\n");
return 1;
}
return 0;
}