Hàm hoán vị 2 dòng, hoán vị 2 cột trong ma trận

Với n là số dòng ,m là số cột ma trận .Mình code theo ngôn ngữ C++

void xuat(int a[][100],int n,int m)
{
      for(int i=0;i<n;i++)
{   
for(int j=0;j<m;j++)
cout<<a[i][j]<<" ";
                        cout<<endl;
           }
}


void hoanvi(int &a,int &b)
{
    int t=a;
    a=b;
    b=t;
}
void hoanvicot(int a[][100],int n,int m,int c1,int c2)
{
    if((c1>=0 && c1<m)&&(c2>=0 && c2<m))
        {
             for(int i=0;i<n;i++)
            hoanvi(a[i][c1],a[i][c2]);
        }

xuat(a,n,m);
}
void hoanvidong(int a[][100],int n,int m,int d1,int d2)
{
    if((d1>=0 &&d1<n) &&(d2>=0 && d2<n))
        {
        for(int j=0;j<m;j++)
            hoanvi(a[d1][j],a[d2][j]);
       }
     xuat(a,n,m);
}


Share this

Related Posts

Previous
Next Post »