PROBLEM : The problem is to rotate a matrix by 90. Fairly easy , just focus on double pointers if you are using C/C++ and make it for Clock Wise and anti clockwise.The code is as follows:
#include<iostream>
#include<cstdlib>
#define CL 0
#define ACL 1
using namespace std;
void printMatrix(int **matrix,int width,int height)
{
for(int i=0;i<width;i++)
{
for(int j=0;j<height;j++)
cout<<*(*(matrix+i)+j)<<" ";
cout<<endl;
}
}
void matrixRot90(int **matrix,int width,int height,int dir)
{
//dir 0 for CL and 1 for ACL
if(dir==CL)
{
for(int j=0;j<height;j++)
{
for(int i=width-1;i>=0;i--)
cout<<*(*(matrix+i)+j)<<" ";
cout<<endl;
}
}
else
{
for(int j=height-1;j>=0;j--)
{
for(int i=0;i<width;i++)
cout<<*(*(matrix+i)+j)<<" ";
cout<<endl;
}
}
}
int main(void)
{
int width,height;
cin>>width>>height;
int **matrix=(int**)malloc(sizeof(int**)*width);
for(int i=0;i<width;i++)
*(matrix+i)=(int*)malloc(sizeof(int)*height);
for(int i=0;i<width;i++)
for(int j=0;j<height;j++)
cin>>*(*(matrix+i)+j);
cout<<"Original Matrix::";
printMatrix(matrix,width,height);
cout<<"After clockwise rotation ::\n\n";
matrixRot90(matrix,width,height,CL);
cout<<"After anticlockwise rotation ::\n\n";
matrixRot90(matrix,width,height,ACL);
return 0;
}