Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Trouble with Transposing a 2D Array

transpose easy array 2d matrix

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1 Andex

Andex

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 16 February 2014 - 11:38 PM

Hi all, 

 

Im trying to transpose this last array,  A[][], and am having trouble with an arrayOutOfBoundsException. Ive tried fooling around with the array dimensions, and cannot figure out what is giving me this error. Any ideas? Thanks!

 

[attachment=11354:lab03.java]

public class lab03 {
	public static final int ARRAY_W = 4;
	public static final int ARRAY_H = 3;
	
	public static void main(String args[]) {
		int A[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
		int B[][] = {{2, 4, 6, 8}, {10, 12, 14, 16}, {18, 20, 22, 24}};
	 	System.out.println("A = ");
		print2D(A);
		System.out.println();
		System.out.println("B = ");
		print2D(B);
		System.out.println();
		System.out.println("A + B = ");
		print2D(add2Ds(A,B));
		System.out.println();
		System.out.println("3 X A = ");
		print2D(multiScalar2D(3,A));
		System.out.println();
		System.out.println("Transpose of A = ");
		print2D(transpose2D(A));
	}
	
	
	static void print2D(int A[][]) {
		for(int i = 0; i < ARRAY_H; i++) {
			for(int j = 0; j < ARRAY_W; j++) {
				System.out.printf("%4d", A[i][j]);
			}
			System.out.println();
		}
	}

	static int[][] add2Ds(int A[][], int B[][]) {
		int[][] C = new int[ARRAY_H][ARRAY_W];
		for(int i = 0; i < ARRAY_H; i++) {
			for(int j = 0; j < ARRAY_W; j++) {
				C[i][j] = A[i][j] + B[i][j];
			}
		}
		return C;
	}

	static int[][] multiScalar2D(int scalar, int A[][]) {
		int[][] B = new int[ARRAY_H][ARRAY_W];
		for(int i = 0; i < ARRAY_H; i++) {
			for(int j = 0; j < ARRAY_W; j++) {	
				B[i][j] = scalar * A[i][j];	
			}
		}
		return B;
	}
	
	static int[][] transpose2D(int A[][]) {
		int[][] D = new int[ARRAY_W][ARRAY_H];
		for(int i = 0; i < 4; i++) {
			for(int j = 0; j < 3; j++) {						D[i][j] = A[j][i];
			}	
		}
		return D;
	}
}

Here is my output/ error code:

A = 
   1   2   3   4
   5   6   7   8
   9  10  11  12

B = 
   2   4   6   8
  10  12  14  16
  18  20  22  24

A + B = 
   3   6   9  12
  15  18  21  24
  27  30  33  36

3 X A = 
   3   6   9  12
  15  18  21  24
  27  30  33  36

Transpose of A = 
   1   5   9Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
	at lab03.print2D(lab03.java:35)
	at lab03.main(lab03.java:28)


#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts

Posted 16 February 2014 - 11:56 PM

Well your print method is hardcoded to print arrays of width 4 and height 3.
After transposing you have an array of width 3 and height 4. And your print method fails.

for(int i = 0; i < ARRAY_H; i++) {
    for(int j = 0; j < ARRAY_W; j++) {
You can make your print method better by taking dynamic lenghts for the loop.
for(int i = 0; i < A.length; i++) {
    for(int j = 0; j < A[i].length; j++) {





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download