forked from vinay-kumar99/Hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixmult.c
173 lines (161 loc) · 3.87 KB
/
matrixmult.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* incorrect*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,k,m,n,p,q,a[100][100],b[100][100],c[100][100],opt1,opt2,s=0;
printf("\nEnter the dimension of the 1st matrix : ");
scanf("%d%d",&m,&n);
printf("\nEnter the elements of the 1st matrix : ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the dimension of the 2nd matrix : ");
scanf("%d%d",&p,&q);
printf("\nEnter the elements of the 2nd matrix : ");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
}
printf("\n");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("%d ",b[i][j]);
}
printf("\n");
/*------------------------------------------------------------------------------------------------------------------------------------------------------------*/
X : printf("\n\nSelect an option from the following");
printf("\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exit");
printf("\nEnter : ");
scanf("%d",&opt1);
switch(opt1)
{
case 1: //Addition
{
if(m==p&&n==q)
{
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",(a[i][j]+b[i][j]));
}
}
}
else
printf("\nThe matrix is not compatable for addition.");
break;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------*/
case 2: //Subtraction
{
if(m==p&&n==q)
{
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",(a[i][j]-b[i][j]));
}
}
}
else
printf("\nThe matrix is not compatable for subtraction.");
break;
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------*/
case 3: //Multiplication
{
Y : printf("\nSelect an option from the following");
printf("\n1.Standard product\n2.Kronecker product\n3.Inner product\n4.Return to main menu\n5.Exit");
printf("\nEnter : ");
scanf("%d",&opt2);
switch(opt2)
{
case 1: //Standard product
{
if(n==p)
{
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]+=(a[i][k]*b[k][j]);
}
}
}
printf("\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d ",c[i][j]);
}
}
}
else
printf("\nThe matrix is not compatable for standard multiplication.");
break;
}
case 2: //Kronecker product
{
if(m==p&&n==q)
{
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",(a[i][j]*b[i][j]));
}
}
}
else
printf("\nThe matrix is not compatable for kronecker product.");
}
case 3: //Inner product
{
if(m==p&&n==q)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
s+=(a[i][j]*b[i][j]);
}
}
printf("\nThe inner product = %d",s);
}
else
printf("\nThe matrix is not compatable for inner product.");
}
case 4:
goto X;
case 5:
exit(0);
default:
goto Y;
}
}
/*----------------------------------------------------------------------------------------------------------------------------------------------------*/
case 5:
exit(0);
default:
goto X;
}
return 0;
}