C and C++ matrix transpose of sample code

  • 2020-04-02 01:57:08
  • OfStack

Without further ado, go straight to the code


#include <iostream>
using namespace std;
const int N = 5;
int matrix[5][5] =
{
    1,2,3,4,5,
    1,2,3,4,5,
    1,2,3,4,5,
    1,2,3,4,5,
    1,2,3,4,5
};
void swap(int &a,int &b)
{
    a=a^b;
    b=a^b;
    a=a^b;
}
void matrix_transpose(int m[N][N])
{
    int i,j;
    for(i=1;i<N;i++)
    {
        for(j=0;j<i;j++)
            swap(m[i][j],m[j][i]);
    }
}
void print(int m[N][N])
{
    int i,j;
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
            cout<<m[i][j]<<"  ";
        cout<<endl;
    }
}
int main()
{
    cout << "Hello world!" << endl;
    int a=10;
    int b=13;
    swap(a,b);
    cout<<" After the exchange: "<<"ta :"<<a<<"tb:"<<b<<endl;
    print(matrix);
    matrix_transpose(matrix);
    cout<<" After exchanging the order: "<<endl;
    print(matrix);
    return 0;
}


Related articles: