-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_operations.c
270 lines (209 loc) · 8.15 KB
/
matrix_operations.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <stdio.h>
void readMatrix(int mat[10][10], int rows, int cols);
void displayMatrix(int mat[10][10], int rows, int cols);
void addMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows, int cols);
void subtractMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows, int cols);
void multiplyMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows1, int cols1, int cols2);
void multiplyByScaler(int mat[10][10], int scaler, int result[10][10], int rows, int cols);
void divideByScaler(int mat[10][10], int scaler, int result[10][10], int rows, int cols);
void transposeMatrix(int mat[10][10], int result[10][10], int rows, int cols);
int main() {
int matrixA[10][10];
int matrixB[10][10];
int result[10][10];
int rowsA, colsA, rowsB, colsB;
int ch;
printf("********************\n");
printf("Select one of the options given below:\n");
printf("1. Matrix addition.\n");
printf("2. Matrix subtraction.\n");
printf("3. Matrix multiplication.\n");
printf("4. Multiply by a scalar.\n");
printf("5. Divide by a scalar.\n");
printf("6. Transpose of a matrix.\n");
printf("7. Exit.\n");
printf("********************\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the number of rows and columns for matrix A: ");
scanf("%d %d", &rowsA, &colsA);
printf("Enter the number of rows and the number of columns for matrix B: ");
scanf("%d %d", &rowsB, &colsB);
if (rowsA != rowsB || colsA != colsB) {
printf("\nMatrices must be of the same size for addition.\n");
break;
}
printf("Enter elements of matrix A:\n");
readMatrix(matrixA, rowsA, colsA);
printf("Enter elements of matrix B:\n");
readMatrix(matrixB, rowsB, colsB);
printf("Matrix A:\n");
displayMatrix(matrixA, rowsA, colsA);
printf("Matrix B:\n");
displayMatrix(matrixB, rowsB, colsB);
printf("Result of addition:\n");
addMatrices(matrixA, matrixB, result, rowsA, colsA);
displayMatrix(result, rowsA, colsA);
break;
case 2:
printf("Enter the number of rows and columns for matrix A: ");
scanf("%d %d", &rowsA, &colsA);
printf("Enter the number of rows and the number of columns for matrix B: ");
scanf("%d %d", &rowsB, &colsB);
if (rowsA != rowsB || colsA != colsB) {
printf("\nMatrices must be of the same size for subtraction.\n");
break;
}
printf("Enter elements of matrix A:\n");
readMatrix(matrixA, rowsA, colsA);
printf("Enter elements of matrix B:\n");
readMatrix(matrixB, rowsB, colsB);
printf("Matrix A:\n");
displayMatrix(matrixA, rowsA, colsA);
printf("Matrix B:\n");
displayMatrix(matrixB, rowsB, colsB);
printf("Result of subtraction:\n");
subtractMatrices(matrixA, matrixB, result, rowsA, colsA);
displayMatrix(result, rowsA, colsA);
break;
case 3:
printf("Enter the number of rows and columns for matrix A: ");
scanf("%d %d", &rowsA, &colsA);
printf("Enter the number of rows and the number of columns for matrix B: ");
scanf("%d %d", &rowsB, &colsB);
if (colsA != rowsB) {
printf("\nThe number of columns in matrix A must be equal to the number of rows in matrix B for multiplication.\n");
break;
}
printf("Enter elements of matrix A:\n");
readMatrix(matrixA, rowsA, colsA);
printf("Enter elements of matrix B:\n");
readMatrix(matrixB, rowsB, colsB);
printf("Matrix A:\n");
displayMatrix(matrixA, rowsA, colsA);
printf("Matrix B:\n");
displayMatrix(matrixB, rowsB, colsB);
printf("Result of multiplication:\n");
multiplyMatrices(matrixA, matrixB, result, rowsA, colsA, colsB);
displayMatrix(result, rowsA, colsB);
break;
case 4:
printf("Enter the number of rows and columns for matrix A: ");
scanf("%d %d", &rowsA, &colsA);
printf("Enter the scaler: ");
int scaler;
scanf("%d", &scaler);
printf("Enter elements of matrix A:\n");
readMatrix(matrixA, rowsA, colsA);
printf("Matrix A:\n");
displayMatrix(matrixA, rowsA, colsA);
printf("Result of scalar multiplication:\n");
multiplyByScaler(matrixA, scaler, result, rowsA, colsA);
displayMatrix(result, rowsA, colsA);
break;
case 5:
printf("Enter the number of rows and columns for matrix A: ");
scanf("%d %d", &rowsA, &colsA);
printf("Enter the scaler: ");
scanf("%d", &scaler);
if (scaler == 0) {
printf("\nCannot divide by zero.\n");
break;
}
printf("Enter elements of matrix A:\n");
readMatrix(matrixA, rowsA, colsA);
printf("Matrix A:\n");
displayMatrix(matrixA, rowsA, colsA);
printf("Result of scalar division:\n");
divideByScaler(matrixA, scaler, result, rowsA, colsA);
displayMatrix(result, rowsA, colsA);
break;
case 6:
printf("Enter the number of rows and columns for matrix A: ");
scanf("%d %d", &rowsA, &colsA);
printf("Enter elements of matrix A:\n");
readMatrix(matrixA, rowsA, colsA);
printf("Matrix A:\n");
displayMatrix(matrixA, rowsA, colsA);
printf("Result of transposition:\n");
transposeMatrix(matrixA, result, rowsA, colsA);
displayMatrix(result, colsA, rowsA);
break;
case 7:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice.\n");
}
return 0;
}
void readMatrix(int mat[10][10], int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("\t%d", &mat[i][j]);
}
}
}
void displayMatrix(int mat[10][10], int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("\t%d ", mat[i][j]);
}
printf("\n");
}
}
void addMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
void subtractMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}
void multiplyMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows1, int cols1, int cols2) {
int i, j, k;
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols2; j++) {
result[i][j] = 0;
for (k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
void multiplyByScaler(int mat[10][10], int scaler, int result[10][10], int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
result[i][j] = mat[i][j] * scaler;
}
}
}
void divideByScaler(int mat[10][10], int scaler, int result[10][10], int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
result[i][j] = mat[i][j] / scaler;
}
}
}
void transposeMatrix(int mat[10][10], int result[10][10], int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
result[j][i] = mat[i][j];
}
}
}