Breaking

Monday, 5 June 2017

Rotate Matrix 90 Degree Anti-Clockwise

Rotate Matrix 90 Degree Anti-Clockwise

                                    
A MxN matrix is passed as the input. The program must rotate the matrix by 90 degrees in anti-clock wise direction and print the rotated matrix as the output.

Input Format: 
First line will contain the value of M.
Second line will contain the value of N.
Next M lines will contain the N values with each value separated by one or more space.

Output Format:N lines will contain the M values with each value separated by one or more space.

Boundary Conditions:
2 <= M <= 152 <= N <= 15

Example Input/Output 1:

Input:
2
3
4 5 9
1 3 5

Output:
9 5
5 3
4 1

Example Input/Output 2:

Input:
4
4
1   2  3    4
5   6  7    8
9 10  11  12
13 14 15 16

Output:
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13


Code:

#include <iostream> 
using namespace std;
int main(int argc, char** argv)
{
    cout<<"matrix"<<"\n";
int arr[15][15],i,j,m,n;
cin>>m>>n;
for(i=0;i<m;i++)
    for(j=0;j<n;j++)
        cin>>arr[i][j];
for(i=n-1;i>=0;i--)
    {
        for(j=0;j<m;j++)
        {
            cout<<arr[j][i]<<"\t";
        }
        cout<<"\n";
    }


}

No comments:

Post a Comment

Like