# Thread: How Do you rotate a multidimensional array?

1. ## How Do you rotate a multidimensional array?

lets say i have an array like this.....

array[4][4]

and this is the data in the array

array[0] { 1,1,1,1}
array[1] { 2,2,2,2}
array[2] { 3,3,3,3}
array[3] { 4,4,4,4}

how do i rotate the array to the left so it is converted to...

array[0] { 1,2,3,4}
array[1] { 1,2,3,4}
array[2] { 1,2,3,4}
array[3] { 1,2,3,4}

I am sure it must be a basic problem but i cannnot think of it!!!

2. Can we assume you have a square matrix(array) to work with or do the columns and rows vary?

Mr. C.

3. The array is square.

4. >how do i rotate the array to the left
Simple enough, just think differently when you traverse the array:
Code:
```#include <iostream>

using namespace std;

int main()
{
int i, j;
int a[4][4] =
{
{ 1,1,1,1 },
{ 2,2,2,2 },
{ 3,3,3,3 },
{ 4,4,4,4 },
};

cout<<"Original array: "<<endl;
for ( i = 0; i < 4; ++i ) {
for ( j = 0; j < 4; ++j )
cout<< a[i][j] <<' ';
cout.put ( '\n' );
}

cout<<"\nRotated array: "<<endl;
for ( i = 0; i < 4; ++i ) {
for ( j = 0; j < 4; ++j )
cout<< a[j][i] <<' ';
cout.put ( '\n' );
}
return 0;
}```
-Prelude