forked from Sniper7sumit/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix Multiplication
79 lines (67 loc) · 2.53 KB
/
Matrix Multiplication
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
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, m, c, d, p, q, k, first[10][10], second[10][10], pro[10][10],sum = 0;
printf("\nEnter the number of rows and columns of the first matrix: \n\n");
scanf("%d%d", &m, &n);
printf("\nEnter the %d elements of the first matrix: \n\n", m*n);
for(c = 0; c < m; c++) // to iterate the rows
for(d = 0; d < n; d++) // to iterate the columns
scanf("%d", &first[c][d]);
printf("\nEnter the number of rows and columns of the first matrix: \n\n");
scanf("%d%d", &p, &q);
if(n != p)
printf("Matrices with the given order cannot be multiplied with each other.\n\n");
else // matrices can be multiplied
{
printf("\nEnter the %d elements of the second matrix: \n\n",m*n);
for(c = 0; c < p; c++) // to iterate the rows
for(d = 0; d < q; d++) // to iterate the columns
scanf("%d", &second[c][d]);
// printing the first matrix
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < n; d++) // to iterate the columns
{
printf("%d\t", first[c][d]);
}
printf("\n");
}
// printing the second matrix
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < p; c++) // to iterate the rows
{
for(d = 0; d < q; d++) // to iterate the columns
{
printf("%d\t", second[c][d]);
}
printf("\n");
}
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < q; d++) // to iterate the columns
{
for(k = 0; k < p; k++)
{
sum = sum + first[c][k]*second[k][d];
}
pro[c][d] = sum; // resultant element of pro after multiplication
sum = 0; // to find the next element from scratch
}
}
// printing the elements of the product matrix
printf("\n\nThe multiplication of the two entered matrices is: \n\n");
for(c = 0; c < m; c++) // to iterate the rows
{
for(d = 0; d < q; d++) // to iterate the columns
{
printf("%d\t", pro[c][d]);
}
printf("\n"); // to take the control to the next row
}
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}