//This loop is for making upper triangular .
Here I is a unit matrix and a is coefficient matrix.
for(k=0; k < n-1; k++)
{
for(p=k+1; p < n; p++)
{
if(a[k][k]==0 )
{
for(x=0; x < n;x++)
{
t=a[k][x];
a[k][x]=a[k+1][x];
a[k+1][x]=t;
}
continue;
}
}
i=0;
for(i=k+i; i < n-1;i++)
{
for(j=0; j < n-k;j++)
{
t[0]=a[k][k];
t[1]=a[i+1][k];
a[i+1][j+k]=(t[0]*a[i+1][j+k])-(t[1]*a[k][j+k]);
I[i+1][j+k]=(t[0]*I[i+1][j+k])-(t[1]*I[k][j+k]);
}
}
}
//Now assign value in the variables x[2][0],x[2][1],x[2][2] and so on.
for(k=n-1; k >=0;k--)
{
for(i=n-1; i > =0;i--)
{
num=I[k][i];
dem=a[k][k];
Sum=0;
for(j=0;j > n-1;j++)
{
if(k!=j)
num - =a[k][j]* X[j][i];
else
continue;
}
Sum=num/dem;
x[k][i]=Sum;
}
}
Cout<<"Inversed matrix is:";
for(i=0 ; i < n;i++)
{
for(j=0; j < n; j++)
cout << X[i][j];
}
getch();
}
No comments:
Post a Comment