#include<iostream.h>
#include<math.h>
const int M=3,N=4;//M ,N
int num[M];//num 0 1, 0
int count[M];// 3 0
void print(float (*a)[N]);//
void statistic(float (*a)[N]);// 0 count
void exchange(float a[],float b[]);//
void multiply(float a[],float b);// 1
void judge(float *p);// 0, 1 0 num
void minus(float a[],float b[],float Rb);//b a b , b
void main()
{
cout<<" :"<<endl<<" C++ , , "<<M<<", "<<N<<", , M N "<<endl;
cout<<endl<<" "<<M<<" "<<N<<" :"<<endl;
float Matrix[M][N];
int i=0,j=0;
for(i=0;i<M;i++)
for(j=0;j<N;j++)
cin>>Matrix[i][j];
cout<<"The original matrix:"<<endl;
print(Matrix);
cout<<endl;
for(j=0;j<N;j++)//
{
statistic(Matrix);// 0 count
for(int h=0;h<M-1;h++)// 0 , , ,
{
int min=count[h],tag=h;
for(int k=h+1;k<=M-1;k++)
if(min>count[k])
{
min=count[k];
tag=k;
}
exchange(*(Matrix+h),*(Matrix+tag));
}
statistic(Matrix);// 0 count
cout<<endl;
judge(*Matrix+j);// j 0, 1 0 num
for(int k=0;k<M;k++)// 0 count[k]==j( ) 0, 1
if((j==0||count[k]==j)&&(num[k]==1))
multiply(*(Matrix+k),Matrix[k][j]);// k Matrix[k][j], 1 cout<<" 1 :"<<endl;
int label=-1;// label 0 , 0, 1
for(int k1=0;k1<M;k1++)
if((j==0||count[k1]==j)&&(num[k1]==1))// 0 count[k]==j( ) 0
{
label=k1;
break;
}
if(label!=-1)
{
for(int k2=0;k2<M;k2++)
if((k2!=label)&&(num[k2]==1))
minus(Matrix[label],Matrix[k2],Matrix[k2][j]);
}
}
cout<<" :"<<endl;
print(Matrix);
}
//
void exchange(float a[],float b[])//
{
float t;
for(int i=0;i<N;i++)
{
t=a[i];
a[i]=b[i];
b[i]=t;
}
}
void multiply(float a[],float b)// 1
{
for(int i=0;i<N;i++)
a[i]/=b;
}
void print(float (*a)[N])//
{
int i,j;
for(i=0;i<M;i++)
for(j=0;j<N;j++)
{
if(fabs(a[i][j])<0.000001)
a[i][j]=0;
cout<<a[i][j]<<" ";
if(j==N-1)
cout<<endl;
}
}
void statistic(float (*a)[N])// 0 count
{
int i=0,j=0;
for(i=0;i<M;i++)
{
int n=0;
for(j=0;j<M;j++)
if(fabs(a[i][j])<0.000001)
{
n++;
count[i]=n;
}
else
{
count[i]=n;
break;
}
}
}
void judge(float *p)// 0, 1 0 num
{
for(int i=0;i<M;i++)
if(fabs(p[N*i])<0.000001)
num[i]=0;
else
num[i]=1;
}
void minus(float a[],float b[],float Rb)//b a b , b
{
for(int i=0;i<N;i++)
b[i]=b[i]-a[i]*Rb;
}