problem no 59:(problem on the matrix): find the transpose of the matrix.
problem:
Write a program
to find the transpose of a square matrix of size N*N. Transpose of a
matrix is obtained by changing rows to columns and columns to rows.
Example 1:
Input:
N = 4
mat[][] = {{1, 1, 1, 1},
{2, 2, 2, 2}
{3, 3, 3, 3}
{4, 4, 4, 4}}
Output:
{{1, 2, 3, 4},
{1, 2, 3, 4}
{1, 2, 3, 4}
{1, 2, 3, 4}}
Example 2:
Input:
N = 2
mat[][] = {{1, 2},
{-9, -2}}
Output:
{{1, -9},
{2, -2}}
code:
void transpose(vector<vector<int> >& matrix, int n)
{
int arr[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[j][i]=matrix[i][j];
}
}
for(int k=0;k<n;k++)
{
for(int l=0;l<n;l++)
{
matrix[k][l]=arr[k][l];
}
}
}
Comments
Post a Comment