-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatricemultiiplication.c
49 lines (43 loc) · 1.22 KB
/
matricemultiiplication.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
#include <stdio.h>
void getMatrix(int matrix[5][5], int a, int b) {
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++ ) {
printf("enter the element [%d][%d]", i, j);
scanf("%d", &matrix[i][j]);
}
}
}
int main(void) {
int a, b, c, d;
printf("Enter matrix A dimensions AXB: ");
scanf("%dx%d", &a, &b);
printf("Enter matrix B dimensions CXD: ");
scanf("%dx%d", &c, &d);
if (b != c) {
printf("Multiplication is not possible between the 2 matrices");
return 0;
}
int array1[a][b];
int array2[c][d];
int result[a][d];
printf("\nEnter the coordinates of the first matrices.\n");
getMatrix(array1, a, b);
printf("\nEnter the coordinates of the second matrices.\n");
getMatrix(array2, c, d);
for (int i = 0; i < a; i++) {
for (int j = 0; j < d; j++) {
result[i][j] = 0;
for (int k = 0; k < a; k++) {
result[i][j] += array1[i][k] * array2[k][j];
}
}
}
printf("The result is\n");
for (int i = 0; i < a; i++ ) {
for (int j = 0; j < d; j++) {
printf(" %d ", result[i][j]);
}
printf("\n");
}
return 0;
}