# How to transpose a 2D array

• 08-27-2008, 06:34 AM
kian_hong2000
How to transpose a 2D array
From

1,2.3,4
5,6,7,8
9,10,11,12
13,14,15,16

To

1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

Is it more easy to use 1D array than 2D array to transpose?
I know that i use for loop as below it will display every row from 0 to 3,
but how to put in the expression in the for loop so that i can apply the logic inside?

for(int a = 0; a < 4; a++)
{
for(int b = 0; b < 4; b++)
{
group [a][b] = a + b +1;

}
}

I know that to dislay the result below i must

Logic
1,2.3,4 (a+b+1)
5,6,7,8 (a+b+4)
9,10,11,12 (a+b+7)
13,14,15,16 (a+b+10)

Do i need to use any class?
• 08-27-2008, 07:47 AM
Fubarable
Yes, this is the "2D" forum, but here 2D stands for 2-dimensional graphics, usually problems involving the Graphics2D class. Your question should have been posted in the beginner's forum.

That said, to solve your problem, why not create a second array, a pair of nested for loops, and copy items from one array at a time? You should try to work out the details on your own (by trying and failing and then ultimately succeeding, you'll learn more than by our just giving you the answer). Good luck!