sir,
I have tried rotating the image without reversing each line .
But by directly swapping ,
what is the error as it isn’t showing correct output???
Regarding the code
hello @gargshivam2001
didnt get ur logic.
try storing updated values in diffrrent array because i think u r updating values 2 times thats why u r getting output. same as input
the standard logic for this problem is as follows->
A simple trick that we can observe is that we can first transpose the whole matrix along the leading diagonal and then reverse each row of the matrix. The resulting matrix will be out the rotated matrix. Here is the code for the following algorithm.
Sir, would you please dry run the code for m=4
And tell me the mistake ???
pls explain ur logic.
i cant get ur logic by doing dry run on wrong code.
My logic !!
ok got ur logic , why r u using three loops .
use outer loop for number of sqaures and inner loop for swapping thats it.
why third loop?
check this->
void rotateMatrix(int mat[][N])
{
// Consider all squares one by one
for (int x = 0; x < N / 2; x++) {
// Consider elements in group
// of 4 in current square
for (int y = x; y < N - x - 1; y++) {
// Store current cell in
// temp variable
int temp = mat[x][y];
// Move values from right to top
mat[x][y] = mat[y][N - 1 - x];
// Move values from bottom to right
mat[y][N - 1 - x]
= mat[N - 1 - x][N - 1 - y];
// Move values from left to bottom
mat[N - 1 - x][N - 1 - y]
= mat[N - 1 - y][x];
// Assign temp to left
mat[N - 1 - y][x] = temp;
}
}
}
The idea is for each square cycle, swap the elements involved with the corresponding cell in the matrix in anti-clockwise direction i.e. from top to left, left to bottom, bottom to right and from right to top one at a time using nothing but a temporary variable to achieve this.
Thanks for the help sir !!!
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.