-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiplication.c
More file actions
85 lines (83 loc) · 2.25 KB
/
MatrixMultiplication.c
File metadata and controls
85 lines (83 loc) · 2.25 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
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
/*
*
* Multiplication of two matrix
* Let Matrix A be of oreder m x n
* Let Matrix B be of order p x q
* We can only multiply A and B iff n == p
*
* Shubham Srivastava<shbm09@gmail.com>
*/
#include <stdio.h>
int main() {
printf("For FIRST MATRIX\n");
int a_row, a_col, b_row, b_col;
printf("Enter the number of rows\n");
scanf("%d", &a_row);
printf("Enter the number of colomns\n");
scanf("%d", &a_col);
printf("\n\n---\n\n");
printf("For SECOND MATRIX\n");
printf("Enter the number of rows\n");
scanf("%d", &b_row);
printf("Enter the number of colomns\n");
scanf("%d", &b_col);
if(a_col != b_row) {
printf("Matrices CAN NOT be multiplied\n");
return 0;
}
int matrixA[a_row][a_col];
int matrixB[b_row][b_col];
printf("Enter the elements of the FIRST MATRIX row wise\n");
int i = 0, j = 0;
for(i = 0; i < a_row; i++) {
for (j = 0; j < a_col; j++) {
scanf("%d", &matrixA[i][j]);
}
}
printf("Enter the elements of the SECOND MATRIX row wise\n");
i = 0;
j = 0;
for(i = 0; i < b_row; i++) {
for (j = 0; j < b_col; j++) {
scanf("%d", &matrixB[i][j]);
}
}
int matrixC[a_col][b_row];
//ALGORITHM FOR MULTIPLICATION
//for i =1 to rows [A]
// for j = 1 to columns [B]
// C[i, j] =0
// for k = 1 to columns [A]
// C[i, j]=C[i, j]+A[i, k]*B[k, j]
int k = 0;
for(i = 0; i < a_row; i++) {
for(j = 0; j < b_col; j++) {
matrixC[i][j] = 0;
for(k = 0; k < a_col; k++) {
matrixC[i][j] = matrixC[i][j] + matrixA[i][k]*matrixB[k][j];
}
}
}
printf("MATRIX A\n");
for (i = 0; i < a_row; i++) {
for (j = 0; j < a_col; j++) {
printf("%d ", matrixA[i][j]);
}
printf("\n\n");
}
printf("MATRIX B\n");
for (i = 0; i < b_row; i++) {
for (j = 0; j < b_col; j++) {
printf("%d ", matrixB[i][j]);
}
printf("\n\n");
}
printf("AxB =>\n");
for (i = 0; i < a_col; ++i) {
for (j = 0; j < b_row; j++) {
printf("%d ", matrixC[i][j]);
}
printf("\n\n");
}
return 0;
}