Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Qassas/1336b9c6d27fcd933d4bea8f76e40827 to your computer and use it in GitHub Desktop.
Save Qassas/1336b9c6d27fcd933d4bea8f76e40827 to your computer and use it in GitHub Desktop.
// rotate 90 degrees
// Written By: Wael Qassas ,23-8-2016
// Original post: https://blog.svpino.com/2015/05/10/programming-challenge-rotating-a-matrix-90-degrees-in-place
#include <iostream.h>
int main()
{int i,j,temp;
const int N=5;
int A[][N]={{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}};
for( i=0;i<5;i++)
for( j=0;j<i;j++)
{temp=A[i][j];
A[i][j]=A[j][i];
A[j][i]=temp;
}
for(i=0;i<N;i++) // 5/2 is integer division
for(j=0;j<N/2;j++)
{ temp=A[i][j];
A[i][j]=A[i][(5-1)-j]; // just swap columns
A[i][(5-1)-j]=temp;
}
for(i=0; i<5; i++)
{ for(j=0; j<5; j++)
cout<<A[i][j]<<"\t";
cout<<endl;
}
return 0;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment