Results 1 to 14 of 14
  1. #1
    BeginnerNoob is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default 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.
    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
    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..
    Java 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

  2. #2
    BOJANM is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default

    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

  3. #3
    BeginnerNoob is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by BOJANM View Post
    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

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Maybe the XOR swap algorithm?

  5. #5
    BeginnerNoob is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by BOJANM View Post
    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

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by BeginnerNoob View Post
    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.
    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
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    BeginnerNoob is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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
    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
    Java 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
    Last edited by BeginnerNoob; 03-28-2011 at 11:44 AM.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by BeginnerNoob View Post
    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
    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    BeginnerNoob is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    hmm.. i exactly know what u mean, but it doesn't work, because I tryed it this way...
    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();
    				}
    .. 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...

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by BeginnerNoob View Post
    hmm.. i exactly know what u mean, but it doesn't work, because I tryed it this way...
    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();
    				}
    .. 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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    BeginnerNoob is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    im getting closer... but still not working well :D

    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();
    				}
    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

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Don't overcomplicate things:

    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]
       }
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    BeginnerNoob is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    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 ...
    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();
    		}
    but it prints out 10 out of 16 numbers..
    Java 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 !
    Last edited by BeginnerNoob; 03-28-2011 at 07:53 PM.

  14. #14
    BeginnerNoob is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Solved:
    problem solved

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

    Mark

Similar Threads

  1. Replies: 2
    Last Post: 03-24-2011, 11:43 PM
  2. initialize multidimensional array
    By Aidoru in forum New To Java
    Replies: 2
    Last Post: 12-04-2010, 02:00 PM
  3. Retrieve Multidimensional Array ??
    By oneofthelions in forum New To Java
    Replies: 3
    Last Post: 12-12-2009, 08:24 AM
  4. Navigate through a multidimensional array
    By VinTiger in forum New To Java
    Replies: 4
    Last Post: 05-05-2009, 08:16 PM
  5. [SOLVED] Multidimensional array
    By Torgero in forum New To Java
    Replies: 20
    Last Post: 03-23-2009, 12:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •