-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatinv.c
More file actions
97 lines (76 loc) · 1.66 KB
/
matinv.c
File metadata and controls
97 lines (76 loc) · 1.66 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
86
87
88
89
90
91
92
93
94
95
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
int n;
printf("enter the n for nxn matrix : ");
scanf("%d",&n);
float b[n][n],a[n][2*n];
double x[n],c,d=0;
// matrix input
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf(" enter b[%d][%d] = ",i,j);
scanf("%f",&b[i][j]);
}
}
// augmented matrix formation
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 2*n; j++)
{
if (j<n)
{
a[i][j] = b[i][j];
}else if (j - n == i){
a[i][j] = 1;
}else{
a[i][j] = 0;
}
}
}
// print matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j <2*n; j++)
{
printf("%f\t",a[i][j]);
}
printf("\n");
}
// gauss jordan method
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i!=j)
{
c = a[j][i]/a[i][i];
for (int k = 0; k < 2*n; k++)
{
a[j][k] = a[j][k] - c*a[i][k];
}
}
}
}
// diagonal elements cinverted to 1
for (int i = 0; i < n; i++)
{
for (int j = n; j < 2*n; j++)
{
a[i][j] = a[i][j]/a[i][i];
}
}
// printing the inverted matrix
for (int i = 0; i < n; i++)
{
for (int j = n; j < 2*n; j++)
{
printf("%f\t",a[i][j]);
}
printf("\n");
}
return 0;
}