# Multidimensional array - swap

• 03-27-2011, 09:05 PM
BeginnerNoob
Multidimensional array - swap
hey everybody!
My name is Mark and I'm new to Java, but (haha what a wonder) i already got my first problem ;)
The task is to write an method, which swaps a multidimensional array by 90° - eg.
Code:

```1  2  3  4    will be oder better say should be  13  9    5  1 5  6  7  8                                                  14 10  6  2 9  10 11 12                                              15 11  7  3 13 14 15 16                                              16 12  8  4```
here's my code so far ....
oh yea first task was to invert a regular array... which was kind a easy..
oh yea ² .. i am NOT allowed to use a second array to swap! i need to swap within the array..
Code:

```import java.util.Random; import java.util.Random; public class SwapIt {         private static int length = 4;                 public static int[] changeArray(int[] array) {                 int temp = 0;                                 Random number = new Random();                 System.out.println("Initial Array:");                 for (int i = 0; i < length; i++) {                         array[i] = number.nextInt() % 20;                         System.out.print("[" + array[i] + "]");                 }                 System.out.println("");                 System.out.println("Array after swapping:");                 for (int i = array.length-1; i >= 0; i--) {                         temp = array[i];                         array[i] = temp;                         System.out.print("[" + array[i] + "]");                 }                 return array;         }         public static int[][] changeArray(int[][] array) {                 Random number = new Random();                 System.out.println("randomized array:");                 for (int i = 0; i <array.length; i++ ) {                         for (int j =0; j<array.length;j++) {                                 array[i][j] = number.nextInt() % 20;                                 System.out.print(array[i][j] + "\t");                         }                         System.out.println();                 }                 System.out.println();                 System.out.println("new array:");                 int temp1 = 0;                 int temp2 = 0;                 int row = array.length;                 int column = 0;                 for (int i = 0; i <array.length; i++ ) {                         for (int j =0; j<array.length;j++) { //                for (int i = array.length-1; i >=0; i-- ) { //                        for (int j =array.length-1; j>=0;j--) {                                 temp1 = array[row][column];                                 array[i][j]=temp1;                                 row--;                                 System.out.print(""+array[i][j]);                                                                                 }                                 } //                                array[i][j]=temp1;                                 // HERE I THOUGHT SOMETHING ABOUT LIKE PUTTING ROW AND COLLUMN //TO 0 AGAIN .. BUT IT DIDN'T WORK THE WAY I WANTED :)                         //                        System.out.print(temp1 + "\t");                                                 return array;         }         public static void main(String[] args) {                                 int[] array = new int[length];                 changeArray(array);                 System.out.println();                 System.out.println(); // creating [][] array                 int arrayd[][] = new int[length][length];                 changeArray(arrayd);                 System.out.println();                         } }```
I'm thankful for every help
Mark
• 03-27-2011, 10:03 PM
BOJANM
e=0;
for(int c=array.length-1;c>=0;c--){
f=0;
for(int d=array.length-1;d>=0;d--){
b[e][f]=a[c][d];
f++}
e++}
mey help
• 03-27-2011, 10:12 PM
BeginnerNoob
Quote:

Originally Posted by BOJANM
e=0;
for(int c=array.length-1;c>=0;c--){
f=0;
for(int d=array.length-1;d>=0;d--){
b[e][f]=a[c][d];
f++}
e++}
mey help

hmmm .. thanks gotto try it out tomorrow.
tell u then
thx for the reply

Mark
• 03-27-2011, 11:10 PM
Iron Lion
Maybe the XOR swap algorithm?
• 03-28-2011, 09:40 AM
BeginnerNoob
Quote:

Originally Posted by BOJANM
e=0;
for(int c=array.length-1;c>=0;c--){
f=0;
for(int d=array.length-1;d>=0;d--){
b[e][f]=a[c][d];
f++}
e++}
mey help

is a good idea, but I'm not allowed to use a second array wo swap it

@IronLion

puh - looks kinda complicated. but i got to give it a try.

does anyone think it could be possible by using two temp int values?

Mark
• 03-28-2011, 09:44 AM
JosAH
Quote:

Originally Posted by BeginnerNoob
hey everybody!
My name is Mark and I'm new to Java, but (haha what a wonder) i already got my first problem ;)
The task is to write an method, which swaps a multidimensional array by 90° - eg.
Code:

```1  2  3  4    will be oder better say should be  13  9    5  1 5  6  7  8                                                  14 10  6  2 9  10 11 12                                              15 11  7  3 13 14 15 16                                              16 12  8  4```

First transpose the matrix (it can be done in place), next reverse each row of the matrix (it can also be done in place).

kind regards,

Jos
• 03-28-2011, 10:26 AM
BeginnerNoob
Quote:

Originally Posted by JosAH
First transpose the matrix (it can be done in place), next reverse each row of the matrix (it can also be done in place).

kind regards,

Jos

ahm .. very interesting approach.
i know how to reverse reach row of the matrix, but atm i just can't figuere out how to transpose it, so that the last step would be to reverse it.
can u give me a hint?

i retyped the matrix - for optical reason
Code:

``` 1  2  3  4    will be oder better say should be          13  9    5  1 5  6  7  8                                                14 10  6  2 9  10 11 12                                              15 11  7  3 13 14 15 16                                              16 12  8  4```
Code:

```EDIT: u mean like? 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 .. but how is this possible without a second array?```
Mark
• 03-28-2011, 10:53 AM
JosAH
Quote:

Originally Posted by BeginnerNoob
ahm .. very interesting approach.
i know how to reverse reach row of the matrix, but atm i just can't figuere out how to transpose it, so that the last step would be to reverse it.
can u give me a hint?

i retyped the matrix - for optical reason
Code:

``` 1  2  3  4    will be oder better say should be          13  9    5  1 5  6  7  8                                                14 10  6  2 9  10 11 12                                              15 11  7  3 13 14 15 16                                              16 12  8  4```
Code:

```EDIT: u mean like? 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 .. but how is this possible without a second array?```
Mark

Transposing a square matrix in place is easy, just swap the elements M[ i ][ j ] and M[ j ][ i ] for all i < j.

kind regards,

Jos
• 03-28-2011, 11:16 AM
BeginnerNoob
hmm.. i exactly know what u mean, but it doesn't work, because I tryed it this way...
Code:

```for (int i = 0; i <array.length; i++ ) {                         for (int j = 0; j<array.length;j++) {                                 array[i][j]        = array[j][i];                                 System.out.print(" "+array[i][j]);                                                                                 }System.out.println();                                 }```
.. this way, the first row is correctly changed, but there are already the "new" values stored at the second row (position 0) .. so get lost of the original values...
• 03-28-2011, 11:24 AM
JosAH
Quote:

Originally Posted by BeginnerNoob
hmm.. i exactly know what u mean, but it doesn't work, because I tryed it this way...
Code:

```for (int i = 0; i <array.length; i++ ) {                         for (int j = 0; j<array.length;j++) {                                 array[i][j]        = array[j][i];                                 System.out.print(" "+array[i][j]);                                                                                 }System.out.println();                                 }```
.. this way, the first row is correctly changed, but there are already the "new" values stored at the second row (position 0) .. so get lost of the original values...

Carefully reread what I wrote: " swap the elements M[ i ][ j ] and M[ j ][ i ] for all i < j." Pay special attention to the last part of my remark.

kind regards,

Jos
• 03-28-2011, 12:01 PM
BeginnerNoob
im getting closer... but still not working well :D

Code:

```for (int i = 0; i <array.length; i++ ) {                         for (int j =0; j<array.length;j++) {                                 if(i==j) {                                         array[i][j]=array[i][j];                                 }                                 if(i<j){                                 array[i][j]        = array[j][i];                                 } //                                if(i>j) { //                                        array[i][j]=array[i][j]; //                                }                                                                 System.out.print(" "+array[i][j]);                                                                                 }System.out.println();                                 }```
the right side of the diagonal looks good. but the left side doesn't..
how can i merge the i<j and the j>i that it works the way i want i to work :)
Thanks a lot for your help so far!

Mark
• 03-28-2011, 12:43 PM
JosAH
Don't overcomplicate things:

Code:

```for (int j= 0; j < matrix.length; j++)   for (int i= 0; i < j; i++) {  // <--- pay attention to the upperbound       // swap matrix[j][i] and matrix[i][j]   } }```
kind regards,

Jos
• 03-28-2011, 06:43 PM
BeginnerNoob
puh im sorry.. it doesn't wok at all. maybe i'm to stupid
I debugged it, and saw, that the first index which will be printed is j = 1; .. so i changed the code to i<=j and i changed ...
Code:

```for (int j = 0; j <array.length; j++ ) {                         for (int i = 0; i<=j;i++) {                                 array[j][i]=array[i][j];                                                        System.out.print(" "+array[i][j]);                                         }System.out.println();                 }```
but it prints out 10 out of 16 numbers..
Code:

```1        2        3        4        5        6        7        8        9        10        11        12        13        14        15        16        new array:  1  2 6  3 7 11  4 8 12 16```
.. thats what i get as an output

u have any more ideas for this bunch of cr** ;)
it would be so damn easy when I'm allowed to use a second array.. but hey.. that's the way it is :D .... was just to motivate myself a bit :D

.. now the left side of the diagonal is working .. but the right one isn't anymore !
• 03-30-2011, 03:02 PM
BeginnerNoob
Solved:
problem solved

Code:

```if(i<j){         int tempJ = array[j][i];         array[j][i] = array[i][j];         array[i][j] = tempJ;         }```
thx

Mark