problem no 59:(problem on the matrix): find the transpose of the matrix.

 problem:

 

Transpose of Matrix
Basic Accuracy: 56.14% Submissions: 9968 Points: 1

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

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.