Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Bookmark I need to rotate a matrix n*m 90º on C++ where n!=m or n=m I need to be

ID: 3802960 • Letter: B

Question

Bookmark

I need to rotate a matrix n*m 90º on C++ where n!=m or n=m I need to be able to do both. I was given these instructions:

•Takes a signed integer as the argument corresponding to an angle in degrees and rotates the image in-place by that angle.

•You can assume the angle in degrees will always be a multiple of 90.

•Positive value of degrees means an anti-clockwise rotation while negative means a clockwise rotation.

•You should allow for cascading calls so that A.Rotate(90).Rotate(90) finally rotates the image A by 180 degrees.

Explanation / Answer

include <bits/stdc++.h>

#define N 4

using namespace std;

void display(int m[N][N]); //function declaration for displaying the matrix

void rotate(int mat[][N])   //function declaration and defination for rotation

{for (int i=0; i<N/2; i++)

    {for (int k=i; k<N-i-1; k++)

        { int temp=m[i][k];               // storing current value in temporary variable

     m[i][k]=mat[k][N-1-i];              // shifting values from right cell to the cell on top

     m[k][N-1-i]=m[N-1-i][N-1-k];      // shifting values from bottom cell to right

     m[N-1-i][N-1-k]=m[N-1-k][i];          // shifting values from left cell to bottom cell

      m[N-1-k][i] = temp;          // value assigned from temp to left cell

        }

    }

}

void display(int m[N][N])   //function defination for displaying the matrix

{ for (int a=0; a<N; a++)

    { for (int b=0; b<N; b++)

printf("%2d ", m[a][b]);

printf(" ");

}

printf(" ");

}

int main()

{

int m[N][N]={ {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} };

display(m); // Prints the current matrix

rotate(m);    // rotates the current matrix

display(m);   //Prints the rotated matrix

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote