Sep 10, 2009

Gauss jordan elimination to find the inverse of the matrix

#include
#include
#include
#include
#include
void main()
{
float ans[50],a[50][50],temp;
int no,i,j,k;
clrscr();
printf("How many equations you want to enter ???\n");
cin>>no;
for(i=1;i<=no;i++)
{
for(j=1;j<=no+1;j++)
{
printf("\nenter value of a[%d][%d]\n",i,j);
scanf("%f",&a[i][j]);
}
}
printf("your matrix is\n");
for(i=1;i<=no;i++)
{
for(j=1;j<=no+1;j++)
printf("%4.2f ",a[i][j]);
printf("\n");
}
//logic
for(k=1;k
{
for(i=k+1;i<=no;i++)//loop for become zero elements
 {
temp=a[i][k]/a[k][k];
for(j=k;j<=no+1;j++)
a[i][j]=a[i][j]-temp*a[k][j];
 }
}
ans[no]=a[no][no+1]/a[no][no];//answer from last
for(i=no-1;i>=1;i--)
{
temp=0.0;
for(j=i+1;j<=no;j++)
temp+=a[i][j]*ans[j];
ans[i]=(a[i][no+1]-temp)/a[i][i];
}
for(i=1;i<=no;i++)
printf("x%d is %3.2f\n",i,ans[i]);
getch();
}

No comments:

Post a Comment