-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix_Multiplication.c
62 lines (59 loc) · 1.74 KB
/
Matrix_Multiplication.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
#include <stdio.h>
#include <stdlib.h>
void print_matrix(int **matrix, int m, int n) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int **matrix_multiply(int **matrix1, int **matrix2, int m1, int n1, int m2, int n2) {
if (n1 != m2) {
printf("Error: Invalid matrix dimensions.\n");
return NULL;
}
int **result = malloc(m1 * sizeof(int *));
int i, j, k;
for (i = 0; i < m1; i++) {
result[i] = malloc(n2 * sizeof(int));
for (j = 0; j < n2; j++) {
result[i][j] = 0;
for (k = 0; k < n1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return result;
}
int main() {
int m1, n1, m2, n2;
printf("Enter dimensions of first matrix (m1 x n1): ");
scanf("%d %d", &m1, &n1);
int **matrix1 = malloc(m1 * sizeof(int *));
int i, j;
for (i = 0; i < m1; i++) {
matrix1[i] = malloc(n1 * sizeof(int));
for (j = 0; j < n1; j++) {
printf("Enter element (%d, %d): ", i, j);
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter dimensions of second matrix (m2 x n2): ");
scanf("%d %d", &m2, &n2);
int **matrix2 = malloc(m2 * sizeof(int *));
for (i = 0; i < m2; i++) {
matrix2[i] = malloc(n2 * sizeof(int));
for (j = 0; j < n2; j++) {
printf("Enter element (%d, %d): ", i, j);
scanf("%d", &matrix2[i][j]);
}
}
int **result = matrix_multiply(matrix1, matrix2, m1, n1, m2, n2);
if (result != NULL) {
printf("Result of matrix multiplication:\n");
print_matrix(result, m1, n2);
}
return 0;
}