Here's an example:
1 2 3
4 5 6
7 8 9
After rotating:
4 1 2
7 5 3
8 9 6
4x4 example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12 5x5 is similar
I can rotate 90° but that's not true in this exercise A element of the matrix just move once I have tried for 2 hours to find the algorithm but my code doesn't work. Please help me solve this problem
#include <iostream>
using namespace std;
void input(int **a,int row,int column)
{
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
cin>>a[i][j];
}
}
void output(int **a,int row,int column)
{
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
cout<<a[i][j]<< " ";
cout<<endl;
}
}
void rotate(int **a,int **b,int m,int n)
{
for(int i=0; i<n; i++)
{
int k=m-1;
for(int j=0; j<m; j++)
{
b[i][j]=a[k][i];
k--;
}
}
}
int main()
{
int m,n;
cin>>m>>n;
int **a=new int*[m];
for(int i=0; i<m; i++)
a[i]=new int[n];
int **b=new int*[n];
for(int i=0; i<n; i++)
b[i]=new int[m];
input(a,m,n);
rotate(a,b,m,n);
output(b,n,m);
return 0;
}