Rotate 2D-Array by 90-Degree in C++ Language
#include<iostream>
using namespace std;
void swap(int a[][4] ,int r, int c)
{
int i,j,n=4;
for(int i=0;i<r;i++)
{
for(int j=0;j<c/2;j++)
{
int tmp = a[i][j];
a[i][j] = a[i][n-j-1];
a[i][n-j-1] = tmp;
}
}
}
void transpose(int a[][4],int r, int c)
{
int i, j, tmp;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i>=j)
{
tmp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=tmp;
}
}
}
swap(a,r,c);
}
int main()
{
int a[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}},i,j;
transpose(a,4,4);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
using namespace std;
void swap(int a[][4] ,int r, int c)
{
int i,j,n=4;
for(int i=0;i<r;i++)
{
for(int j=0;j<c/2;j++)
{
int tmp = a[i][j];
a[i][j] = a[i][n-j-1];
a[i][n-j-1] = tmp;
}
}
}
void transpose(int a[][4],int r, int c)
{
int i, j, tmp;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i>=j)
{
tmp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=tmp;
}
}
}
swap(a,r,c);
}
int main()
{
int a[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}},i,j;
transpose(a,4,4);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
}
No comments
Post your comments