-
Notifications
You must be signed in to change notification settings - Fork 1
/
guasselimination.c
161 lines (124 loc) · 2.41 KB
/
guasselimination.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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int d, n;
float **p;
float max[100];
int position[100];
void
multiplyadd (int r)
{
int i, j, m, s;
float factor;
for (i = (r + 1); i < n; i++)
{
factor = -(p[i][r] / p[r][r]);
for (j = 0; j < (d + 1); j++)
{
p[i][j] = factor * p[r][j] + p[i][j];
}
printf("the new matrix after multiplying row %d by %f and adding to row %d \n",
r, factor, i);
for (m = 0; m < n; m++)
{
for (s = 0; s < (d + 1); s++)
{
printf ("%f\t", p[m][s]);
}
printf ("\n");
}
}
}
void
pivot (int j)
{
int i;
max[j] = 0;
for (i = 0; i < n; i++)
{
if (p[i][j] > max[j])
{
max[j] = p[i][j];
position[j] = i;
}
}
printf ("the max element in column %d is %f at row %d \n", j, max[j],
position[j]);
}
void
swap (int r1, int r2)
{
int j, i;
float temp;
for (j = 0; j < (d + 1); j++)
{
temp = p[r1][j];
p[r1][j] = p[r2][j];
p[r2][j] = temp;
}
printf ("The matrix is now after swapping rows %d %d \n", r1, r2);
for (i = 0; i < n; i++)
{
for (j = 0; j < (d + 1); j++)
{
printf ("%f\t", p[i][j]);
}
printf ("\n");
}
}
void
main ()
{
int i, j;
printf ("Enter the number of equations");
scanf ("%d", &n);
printf ("Enter the no of variable in the equation");
scanf ("%d", &d);
p = (float **) malloc (n * sizeof (float));
for (i = 0; i < d; i++)
{
*(p + i) = (float *) malloc ((d + 1) * sizeof (float));
}
for (i = 0; i < n; i++)
{
for (j = 0; j < (d + 1); j++)
{
float g;
printf ("Enter the coefficient at position %d %d in the equation",
i, j);
scanf ("%f", &g);
if (j != d)
p[i][j] = g;
else
p[i][j] = (-g);
}
}
printf ("The matrix is \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < (d + 1); j++)
{
printf ("%f\t", p[i][j]);
}
printf ("\n");
}
pivot (0);
swap (0, position[0]);
for (i = 0; i < d; i++)
multiplyadd (i);
float var[100];
for(i=(n-1);i>=0;i--){
if(i==(n-1))
var[i]=p[i][d]/p[i][i];
else{
var[i]=p[i][d];
for(j=(d-1);j>i;j--){
int m;
var[i]-=var[j]*p[i][j];
}
var[i]/=p[i][i];
}
}
for (i = 0; i < n; i++)
printf ("answer of variable %d is %f \n", i, var[i]);
}