Results 1 to 14 of 14
Thread: Multidimensional array - swap
- 03-27-2011, 09:05 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
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.here's my code so far ....Java 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
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..
I'm thankful for every helpJava 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(); } }
Mark
- 03-27-2011, 10:03 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
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 #3
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
- 03-27-2011, 11:10 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Maybe the XOR swap algorithm?
- 03-28-2011, 09:40 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
- 03-28-2011, 09:44 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 03-28-2011, 10:26 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
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
Java 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
MarkJava 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?
Last edited by BeginnerNoob; 03-28-2011 at 10:44 AM.
- 03-28-2011, 10:53 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 03-28-2011, 11:16 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
hmm.. i exactly know what u mean, but it doesn't work, because I tryed it this way...
.. 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...Java 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(); }
- 03-28-2011, 11:24 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 03-28-2011, 12:01 PM #11
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
im getting closer... but still not working well :D
the right side of the diagonal looks good. but the left side doesn't..Java 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(); }
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 #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Don't overcomplicate things:
kind regards,Java 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] } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-28-2011, 06:43 PM #13
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
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 ...but it prints out 10 out of 16 numbers..Java 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(); }
.. thats what i get as an outputJava 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
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 !Last edited by BeginnerNoob; 03-28-2011 at 06:53 PM.
- 03-30-2011, 03:02 PM #14
Member
- Join Date
- Mar 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Filling a multidimensional array, and then change it
By MaceMan in forum New To JavaReplies: 2Last Post: 03-24-2011, 10:43 PM -
initialize multidimensional array
By Aidoru in forum New To JavaReplies: 2Last Post: 12-04-2010, 01:00 PM -
Retrieve Multidimensional Array ??
By oneofthelions in forum New To JavaReplies: 3Last Post: 12-12-2009, 07:24 AM -
Navigate through a multidimensional array
By VinTiger in forum New To JavaReplies: 4Last Post: 05-05-2009, 07:16 PM -
[SOLVED] Multidimensional array
By Torgero in forum New To JavaReplies: 20Last Post: 03-22-2009, 11:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks