-
Notifications
You must be signed in to change notification settings - Fork 1
/
1_6_matrix.c
126 lines (108 loc) · 3.43 KB
/
1_6_matrix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
int matrix1[MAX_ROWS][MAX_COLS];
int matrix2[MAX_ROWS][MAX_COLS];
int result[MAX_ROWS][MAX_COLS];
int rows, cols;
void readMatrix(int matrix[MAX_ROWS][MAX_COLS], int rows, int cols) {
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
}
void displayMatrix(int matrix[MAX_ROWS][MAX_COLS], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
void addMatrices() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
void subMatrices() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
}
void mulMatrices() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = 0;
for (int k = 0; k < cols; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
void transposeMatrix(int matrix[MAX_ROWS][MAX_COLS], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = i + 1; j < cols; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
int main() {
int choice;
printf("Enter the number of rows and columns for the matrices (both should be <= 10): ");
scanf("%d %d", &rows, &cols);
printf("Enter elements for the first matrix:\n");
readMatrix(matrix1, rows, cols);
printf("Enter elements for the second matrix:\n");
readMatrix(matrix2, rows, cols);
do {
printf("\n======= MENU =======\n");
printf("1. Add Matrices\n");
printf("2. Subtract Matrices\n");
printf("3. Multiply Matrices\n");
printf("4. Transpose Matrix\n");
printf("5. Display Result Matrix\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addMatrices();
printf("Result of Addition:\n");
displayMatrix(result, rows, cols);
break;
case 2:
subMatrices();
printf("Result of Subtraction:\n");
displayMatrix(result, rows, cols);
break;
case 3:
mulMatrices();
printf("Result of Multiplication:\n");
displayMatrix(result, rows, cols);
break;
case 4:
printf("Transpose of Matrix 1:\n");
transposeMatrix(matrix1, rows, cols);
displayMatrix(matrix1, cols, rows);
break;
case 5:
printf("Result Matrix:\n");
displayMatrix(result, rows, cols);
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid Choice\n");
}
} while (choice != 6);
return 0;
}