Say I have multidimensional array like this:
int arr[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
I'd like to rotate the array clockwise so it looks like this:
{{7, 4, 1},
{8, 5, 2},
{9, 6, 3}};
I tried swapping each of the values in turn with their previous value:
swap(&arr[0][0],&arr[0][1]);
swap(&arr[0][1],&arr[0][2]);
swap(&arr[0][2],&arr[1][2]);
swap(&arr[1][2],&arr[2][2]);
swap(&arr[2][2],&arr[2][1]);
swap(&arr[2][1],&arr[2][0]);
swap(&arr[2][0],&arr[1][0]);
swap(&arr[1][0],&arr[0][0]);
This didn't rotate correctly. It left a few values where they were and put the others in the wrong places.
What am I doing wrong, and how can I achieve this?