-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRA_7.c
59 lines (53 loc) · 981 Bytes
/
PRA_7.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
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int i,j,k,n;
float m[20][20],x[20],sum,temp;
float err,maxiter,eps;
printf("\n How many equations do you want? : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=(n+1);j++)
{
printf("\n Enter M[%d][%d]: ",i,j);
scanf("%f",&m[i][j]);
}
}
printf("\n Enter the number of iterations: ");
scanf("%f",&maxiter);
printf("\n Enter the epsilon: ");
scanf("%f",&eps);
for(i=1; i<=n; i++)
{
x[i] = 0.0;
}
for(k=1; k<=maxiter; k++)
{
for(i=1; i<=n ;i++)
{
sum = 0.0;
for(j=1; j<=n; j++)
{
if(i!=j)
{
sum = sum + (m[i][j] * x[j]);
}
}
temp = (m[i][n+1] - sum) / m[i][i];
err = fabs((temp - x[i]) / temp);
x[i] = temp;
}
if(err <= eps)
{
printf("\n Solution converges in %d iterations",k);
for(i=1; i<=n; i++)
{
printf("\n X[%d] = %f",i,x[i]);
}
}
}
return 0;
}