Mirroring 2D array across diagnol

I am trying to mirror an array. This is the array I am given
1.0 2.0 3.0 4.0
5.0 6.0 7.0 8.0
9.0 10.0 11.0 12.0
13.0 14.0 15.0 16.0
and I need to transform it to this
1.0 2.0 3.0 13.0
5.0 6.0 10.0 14.0
9.0 7.0 11.0 15.0
4.0 8.0 12.0 16.0

This is the code I wrote but I am not getting the same thing. Where am I going wrong?

for(i = 0; i < y; i++)
for(j = 0; j <= i; j++)
data[i][j] = data[i][j] + data[j][i] - (data[j][i]= data[i][j]);

why didnt the 5 and 2 swap, or the 9 and 3?? What exactly is the algorithm here?

I would expect a mirror to do this:

1 5 9 13

Last edited on

The numbers starting on the bottom left of the array to the top right, get mirrored. The rest of the numbers remain the same.
I'm with @jonnin here: OP please explain why you are reflecting only part of your matrix.
ok, so you want to mirror arcoss the top left to bottom right diagonal only the elements in the bottom half including the diagonal of the diagonal from bottom left to top right. That is a little odd, but it should be doable.

what I would do is write a standard mirror function to start, one that flips everything across the top left to bottom right diagonal.

once that works, apply logic to filter it (ideally, by doing less work) so it only operates on the elements you want.

Topic archived. No new replies allowed.