# Matrix 2

• 09-15-2002
Achillles
Matrix 2
OK I got it to rotate but there is one problem for example it does this:
123
456
789
this is not the 2 dimensional array i'm working with keep that in mind... I'm just using it for an example... this is supposed to work with any matrix no matter the size. I'm using random number gen to produce the numbers this is just an example... but this is what it does it rotates all the numbers but when it gets to the left corner it does this...
412
953
876
well here is the code
Code:

``` void Array2::rotate()  {   int temp1, temp2;   int a=n-1;   for (int r=0; r<n/2; r++)  // for rows   {   temp1=A[r][r];     for(int c=r; c<n-1; c++) // across the top       {         temp2=temp1; // first number = temp2         temp1=A[r][c]; // temp1 = second         A[r][c]= temp2;  // A[r][c] becomes becomes whats in temp2       }       for(int h=r; h<n-1; h++)  // down right side       {       temp2=temp1; // first number = temp2         temp1=A[h][a]; // temp1 = second         A[h][a]= temp2;  // A[r][c] becomes becomes whats in temp2       }     for(int i=a; i>=0; i--)       {         temp2=temp1; // first number = temp2         temp1=A[a][i]; // temp1 = second         A[a][i]= temp2;  // A[r][c] becomes becomes whats in temp2       }   for(int j=n-1; j>=0; j--)       {         temp2=temp1; // first number = temp2         temp1=A[j][r]; // temp1 = second         A[j][r]= temp2;  // A[r][c] becomes becomes whats in temp2       }   a++;   }  }```
• 09-15-2002
Nick
This way should work. For a matrix like this

12345
45666
78977
12345
45676

Call a rotate_square(left, right, up, down) on the squares
Code:

```12345 4  6 7  7 1  5 45676 566 8 7 234 9```
Inside the actual rotate function you would have code
like

Code:

```while(left <= right && up <= down) {       rotate_square(left, right, up, down);       left++;       right--;       up++;       down--; }```