-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaussThomas method.cpp
More file actions
47 lines (47 loc) · 953 Bytes
/
GaussThomas method.cpp
File metadata and controls
47 lines (47 loc) · 953 Bytes
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
#include<stdio.h>
#include<conio.h>
main()
{
float a[10][10],b[10],x[10],sum,z;
int n,i,j,k;
printf("Enter the order of the matrix: ");
scanf("%d",&n);
printf("Enter %d*%d elements of co-efficient matrix: \n",n,n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%f",&a[i][j]);
}
printf("\nEnter 3 elements of right hand side vector: \n");
for(i=1;i<=n;i++)
scanf("%f",&b[i]);
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
if(i!=k)
z=a[i][k]/a[k][k];
for(j=1;j<=n;j++)
{
a[i][j]=a[i][j]-z*a[k][j];
}
b[i]=b[i]-z*b[k];
}
}
x[n]=b[n]/a[n][n];
for(k=n-1;k>=1;k--)
{
sum=b[k];
for(j=n;j>=k+1;j--)
{
sum=sum-a[k][j]*x[j];
x[k]=sum/a[k][k];
}
}
printf("\nThe Solution of the System of linear equations is:\n");
printf("--------------------------------------------------\n");
for(i=1;i<=n;i++)
printf("X[%d]=%f\t\t",i,x[i]);
getch();
return 0;
}