-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiply_Two_Matrices_Using_Arrays.c
More file actions
55 lines (43 loc) · 1.21 KB
/
Multiply_Two_Matrices_Using_Arrays.c
File metadata and controls
55 lines (43 loc) · 1.21 KB
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
#include <stdio.h>
int main() {
int m, n, p, q, i, j, k;
printf("Enter the number of rows and columns of the first matrix: ");
scanf("%d %d", &m, &n);
int A[m][n];
printf("Enter the elements of the first matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &A[i][j]);
}
}
printf("Enter the number of rows and columns of the second matrix: ");
scanf("%d %d", &p, &q);
if (n != p) {
printf("Matrices cannot be multiplied.\n");
return 0;
}
int B[p][q];
printf("Enter the elements of the second matrix:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < q; j++) {
scanf("%d", &B[i][j]);
}
}
int C[m][q];
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
C[i][j] = 0;
for (k = 0; k < n; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
printf("Resultant matrix after multiplication:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}