Sep 10, 2009

gauss elimination method

#include
#include
#include
void main()
{
int i=0,j=0,k=0,n=0;
float sum,a[100][100],temp,x[50];
clrscr();
printf("Enter the order of matrix : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
    for(j=1;j<=n+1;j++)
    {
    printf("\nEnter value of a[%d][%d] : ",i,j);
    scanf("%f",&a[i][j]);
    }
}
printf("\n\nEquations are : \n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("\t%f",a[i][j]);
}
printf("\n\n");
}
for(k=1;k<=n-1;k++)
{
for(j=k+1;j<=n;j++)
{
temp=a[j][k]/a[k][k];
for(i=k;i<=n+1;i++)
{
a[j][i]=a[j][i]-(temp)*a[k][i];
}
}
}
printf("\nNew Eq : \n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("\t%f",a[i][j]);
}
printf("\n");
}
x[n]=(a[n][n+1]/a[n][n]);
for(i=n-1;i>=0;i--)
{
sum=0;
for(j=i+1;j<=n;j++)
{
sum=sum+(a[i][j]*x[j]);
}
x[i]=(a[i][n+1]-sum)/a[i][i];
}
printf("\nRoots are : ");
for(i=1;i<=n;i++)
{
printf("\t%f",x[i]);
}
getch();
}

No comments:

Post a Comment