Nov 18, 2008

Gauss Seidal Method

#include
#include

int main()
{
int i,j,flag,n;
float a[10][10],y[10];
float sum,temp;

printf("Enter the no.s of equations.\n");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter the coefficent & value of equations %d.",i+1);
for(j=0;j<=n;j++)
{
scanf("%f",&a[i][j]);
}
}

for(i=0;i<=n-1;i++)
{
x[i]=0;
y[i]=9999;
}

do
{
for(i=0;i<=n-1;i++)
{
sum=0;
for(j=0;j<=n-1;j++)
{
if(j!=i)
{
sum=sum+a[i][j]*x[j];
}
}
temp=(a[i][n]-sum)/a[i][i];
x[i]=temp;
}

for(i=0;i<=n-1;i++)
{
if(y[i]==x[i]) flag=1;
else flag =0;
}

for(i=0;i<=n-1;i++)
{
y[i]=x[i];
printf("x[%d]=%f\t",i,x[i]);
}
printf("\n");
}while(flag==0);

return 0;
}

0 comments: