forked from Tomkndn/C
-
Notifications
You must be signed in to change notification settings - Fork 2
/
multiplication_matrix.c
86 lines (79 loc) · 1.9 KB
/
multiplication_matrix.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h>
int main(){
// First Matrix
int row1,col1;
printf("Input rows and columns for First matrix: ");
scanf("%d %d",&row1,&col1);
int A[row1][col1];
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
printf("Element [%d][%d]: ",i,j);
scanf("%d", &A[i][j]);
}
}
// Second Matrix
int row2,col2;
printf("Input rows and columns for First matrix: ");
scanf("%d %d",&row2,&col2);
// Inputting the Wrong row.
if (col1 != row2)
{
printf("OOPS!!! Your Matrix is not suitable for Multiplication!!!!.");
return 0;
}
int B[row2][col2];
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
printf("Element [%d][%d]: ",i,j);
scanf("%d", &B[i][j]);
}
}
// Printing the Matrix.
printf("Your first Matrix is:\n");
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
printf("Your Second Matrix is:\n");
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
printf("%d ",B[i][j]);
}
printf("\n");
}
// Multiplying Matrix
int new[row1][col2];
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
int sum=0;
for (int k = 0; k < row2; k++)
{
sum = sum + (A[i][k] * B[k][j]);
}
new[i][j] = sum;
}
}
// Printing Final Matrix.
printf("Your Final Matrix is:\n");
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
printf("%d ",new[i][j]);
}
printf("\n");
}
return 0;
}