Matrix transpose (20 points) Question content: The user inputs the order of the matrix, then inputs all matrix elements (integers) by row, and transposes the matrix and outputs it. The order should be an integer between [1,5]. If it is not in this range, "matrix order error" will be displayed. Input format: Line 1, an integer representing the order n. After that, there are n rows of data, each row contains n integers, which are matrix elements input row by row. Output format: n lines of transposed data, separated by a space, and no space at the end of the line. Input and output sample 1 Input: 44 6 8 92 7 4 53 8 16 151 5 7 11 Output: 4 2 3 16 7 8 58 4 16 79 5 15 11 Input and output sample 2 Input: 0 Output: matrix order error Note
: Transpose should be the transposition operation implemented by the elements in the array storing the matrix, not just the display effect.
Time limit: 2000ms Memory limit: 32000kb C++ #include
using namespace std;
int main()
{ const int N=100,M=100; int a[N][M]; int i,j,n,temp; cin> >n; if(n>=1&&n<=5) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { cin>>a[i][j] ; } }
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j];
if(j!=(n-1))
cout<<" ";
}
cout<<endl;
}
}
else
cout<<"matrix order error";
return 0;
}