-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrixCalculator.c
377 lines (353 loc) · 7.45 KB
/
matrixCalculator.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include<stdio.h>
#include<math.h>
float determinant(float[][25],float);
void cofactor(float[][25],float);
void transpose(float[][25],float[][25],float);
void inverse();
void matrixmultiply();
void multireadmatrix(int[][10],int[][10],int,int,int,int);
void multishowmatrix(int[][10],int,int);
void multiprocessmatrix(int[][10],int[][10],int[][10],int,int,int,int);
void matrixadd(float[][25],float[][25],int,int);
void matrixsub(float[][25],float[][25],int,int);
void matrix_output(float[][25],int,int);
void matrixaddsub();
void determinant_input();
void transpose_input();
void main()
{
int a;char str;
printf("--------------WELCOME TO MATRIX CALCULATION--------------\n");
printf("::::1.Calulate Addition or Subtraction of matrix::::::::\n");
printf("::::2.Calculate Determinant of matrix::::\n");
printf("::::3.Calculate Inverse of a matrix::::::\n");
printf("::::4.Calculate Multiplication of matrix:\n");
printf("::::5.Calculate Transpose of matrix::::::\n");
printf("Enter any choices from 1 to 5::\t");
scanf("%d",&a);
switch (a)
{
case 1:
matrixaddsub();
break;
case 2:
determinant_input();
break;
case 3:
inverse();
break;
case 4:
matrixmultiply();
break;
case 5:
transpose_input();
break;
default:
printf("Invalid choice\n");
printf("Do you want to Re-enter.\n");
printf("Press Y to continue or Press N to exit.\t");
scanf("%c",&str);
if (str=='y'||str=='Y')
{
main();
}
break;
}
}
void transpose_input()
{
int i,j,n;float a[25][25],b[25][25];
printf("Calculation of Matrix Transpose\n");
printf("Enter the dimension of square Matrix:\t");
scanf("%d",&n);
printf("\nEnter the Matrix element of %dX%d matrix\n",n,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
}
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
b[i][j]=a[j][i];
}
}
printf("The transpose of a matrix is:\n");
matrix_output(b,n,n);
}
void determinant_input()
{
float a[25][25],k,d;
int i,j;
printf("\n Calculation of determinant of matrix\n\n");
printf("Enter the order of the Square Matrix : ");
scanf("%f",&k);
printf("Enter the elements of %.0fX%.0f Matrix : \n",k,k);
for (i=0;i<k;i++)
{
for (j=0;j<k;j++)
{
scanf("%f",&a[i][j]);
}
}
d=determinant(a,k);
printf("Determinant of matrix is %f",d);
}
void matrixadd(float a[][25],float b[][25],int m, int n)
{
int i,j;float integral[25][25];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
integral[i][j]=a[i][j] +b[i][j];
}
printf("The addition of the two matrix is \n");
matrix_output(integral,m,n);
}
void matrixsub(float a[][25],float b[][25],int m, int n)
{
int i,j;float integral[25][25];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
integral[i][j]=a[i][j] - b[i][j];
}
printf("The subtraction of the two matrix is \n");
matrix_output(integral,m,n);
}
void matrix_output(float dis[][25],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%10f",dis[i][j]);
}
printf("\n");
}
}
void matrixaddsub()
{
int i,j,m,n,cho; float a[25][25],b[25][25];
printf("Enter the dimension of Matrix as MXN");
scanf("%d %d",&m,&n);
printf("Enter the element of 1st Matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&a[i][j]);
}
}
printf("Enter the element of 2nd Matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&b[i][j]);
}
}
printf("1.Calculate Matrix Addition.\n");
printf("2.Calculate Matrix Subtraction.\n");
scanf("%d",&cho);
switch(cho)
{
case 1:
matrixadd(a,b,m,n);
break;
case 2:
matrixsub(a,b,m,n);
break;
}
}
void inverse()
{
float a[25][25],k,d;
int i,j;
printf("-------------------------------------------------------------\n");
printf("-------------------------------------------------------------\n");
printf("-------------------------------------------------------------\n");
printf("\n C Program to find inverse of Matrix\n\n");
printf("Enter the order of the Matrix : ");
scanf("%f",&k);
printf("Enter the elements of %.0fX%.0f Matrix : \n",k,k);
for (i=0;i<k;i++)
{
for (j=0;j<k;j++)
{
scanf("%f",&a[i][j]);
}
}
d=determinant(a,k);
printf("Determinant of the Matrix = %f",d);
if (d==0)
printf("\nInverse of Entered Matrix is not possible\n");
else
cofactor(a,k);
printf("\n\n**** Thanks for using the program!!! ****");
}
//For calculating Determinant of the Matrix
float determinant(float a[25][25],float k)
{
float s=1,det=0,b[25][25];
int i,j,m,n,c;
if (k==1)
{
return (a[0][0]);
}
else
{
det=0;
for (c=0;c<k;c++)
{
m=0;
n=0;
for (i=0;i<k;i++)
{
for (j=0;j<k;j++)
{
b[i][j]=0;
if (i != 0 && j != c)
{
b[m][n]=a[i][j];
if (n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det + s * (a[0][c] * determinant(b,k-1));
s=-1 * s;
}
}
return (det);
}
void cofactor(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
for (q=0;q<f;q++)
{
for (p=0;p<f;p++)
{
m=0;
n=0;
for (i=0;i<f;i++)
{
for (j=0;j<f;j++)
{
if (i != q && j != p)
{
b[m][n]=num[i][j];
if (n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q + p) * determinant(b,f-1);
}
}
transpose(num,fac,f);
}
//Finding transpose of matrix
void transpose(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inverse[25][25],d;
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=determinant(num,r);
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
inverse[i][j]=b[i][j] / d;
}
}
printf("\n\n\nThe inverse of matrix is : \n");
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
printf("\t%f",inverse[i][j]);
}
printf("\n");
}
}
void matrixmultiply()
{
int matrix1[10][10],matrix2[10][10],m,n,p,q,multi[10][10];
printf("enter dimension of matrix1 MXN\n");
scanf("%d %d",&m,&n);
printf("enter dimension of matrix2 PXQ\n");
scanf("%d %d",&p,&q);
if (n==p)
{
printf("\nEnter elements of matrices\n");
multireadmatrix(matrix1,matrix2,m,n,p,q);
multiprocessmatrix(matrix1,matrix2,multi,m,n,p,q);
printf("\nMatrix multiplication is ::::\n");
multishowmatrix(multi,m,q);
}
else
{
printf("Matrix multiplication not possible");
}
}
void multishowmatrix(int multi[][10],int n,int p)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
printf("%10d ",multi[i][j]);
}
printf("\n");
}
}
void multireadmatrix(int matrix1[][10],int matrix2[][10],int m,int n,int p,int q)
{
int i,j;
printf("\nEnter 1st Matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&matrix1[i][j]);
}
printf("\nEnter 2nd Matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&matrix2[i][j]);
}
}
void multiprocessmatrix(int matrix1[][10],int matrix2[][10],int multi[][10],int m, int n, int p, int q)
{
int i,j,k;
for (i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
multi[i][j]=0;
for(k=0;k<n;k++)
multi[i][j]=multi[i][j]+(matrix1[i][k]*matrix2[k][j]);
}
}
}