Home > Blockchain >  how to print two dimensional array correctly?
how to print two dimensional array correctly?

Time:06-21

my output is

4 4 8 7 

3 3 3 5 6 3 3 4 5 

when it should be

4 4 5 
8 7 7 

3 3 3 4 
5 6 3 1 
3 4 5 6 

i need to use same method for all of arrays, summing up arrays of two matrices. how can i do that when arrays are different length and how can i print output correctly?

public static void main(String[] args) {
    int[][] m1 = { { 1, 2, 0 }, { 2, 3, 4 } };
    int[][] m2 = { { 3, 2, 5 }, { 6, 4, 3 } };
    int[][] m3 = { { 1, 1, 1, 1 }, { 3, 3, 2, 1 }, { 2, 2, 2, 2 } };
    int[][] m4 = { { 2, 2, 2, 3 }, { 2, 3, 1, 0 }, { 1, 2, 3, 4 } };
    printMatrixSum(m1, m2);
    System.out.println();
    printMatrixSum(m3, m4);
}

private static void printMatrixSum(int[][] x, int[][] y) {
    int c[][] = new int[4][4];
    for (int i=0; i < 2;   i) {
        for(int j=0;j<3;j  ) {
            c[i][j]=x[i][j] y[i][j];
            System.out.print(c[i][j] " ");
        }
    }System.out.println();
    
}

}

CodePudding user response:

Your code is close to working. I made some changes below, and it now produces this output:

4 4 5 
8 7 7 

3 3 3 4 
5 6 3 1 
3 4 5 6

Here's the code:

1.  private static void printMatrixSum(int[][] x, int[][] y) {
2.      int[][] c = new int[x.length][x[0].length];
3.      for (int i = 0; i < x.length; i  ) {
4.         for (int j = 0; j < x[0].length; j  ) {
5.             c[i][j] = x[i][j]   y[i][j];
6.             System.out.print(c[i][j]   " ");
7.         }
8.         System.out.println();
9.      }
10.  }

Here are the edits I made:

  • line 2: change int c[][] to int[][] c – this is minor, but follows Java style
  • line 2: change new int[4][4] to use actual values for array length. The first dimension is just x.length, and the second is x[0].length. This will work correctly with different array inputs vs. hard-coding to "4".
  • line 3: change i < 2 condition to use the length of one of the input arrays; I chose x but it could be y (and of course this code assumes that x and y are matching dimensions)
  • line 3: change i to i – not sure why you were using pre-increment, but in my opinion that makes it harder to reason about loop behavior, and also the second loop (line 4) was already doing post-increment (j ) so I edited this one to make it both clearer as well as consistent with the rest of your code.
  • line 4: change j < 3 to be dynamic, similar to edit on line 3
  • line 8: move the println() here, so that a newline is printed after each row of the array is printed

CodePudding user response:

Instead of printing it with fixed sizes ( like i < 2 and j <3 ), use the dimension of the array to make your function more reusable.

ii) Secondly, the println() should be outside the inner loop but inside the outer loop .

private static void printMatrixSum(int[][] x, int[][] y) {
        int c[][] = new int[4][4];
        for (int i = 0; i < x.length;   i) {
            for (int j = 0; j < y[0].length; j  ) {
                c[i][j] = x[i][j]   y[i][j];
                System.out.print(c[i][j]   " ");
            }
            System.out.println();
        }

    }

and this is the answer :

4 4 5 
8 7 7

3 3 3 4
5 6 3 1
3 4 5 6

CodePudding user response:

A couple suggestions.

  • Consider not writing a printSum method. Write a print method. You may not always want to print a matrix after you compute the sum. Of course that means iterating over the matrix twice so it's ultimately going to be based on your requirements.
  • specify a width value to help nicely format the output.
  • And you don't need to index the matrix. Multi-dimensional arrays are simply arrays of other arrays. So you can use the enhanced forloop to first iterate the rows, and then the values in each row.

Here is an example.

int [][] data = {{203,2,3334,-22022, 23},{2,1,233, 42,33}};
matPrint(data, 7);

prints

    203      2   3334 -22022     23
      2      1    233     42     33
  • use the width to specify the largest value(including sign)
  • create a format for System.out.printf
  • then simply iterate the rows. and within each row, iterate the values.
public static void matPrint(int[][] m, int width) {
    String fmt = "%"   width   "s";
    for (int[] row : m) {
        for (int col : row) {
            System.out.printf(fmt, col);
        }
        System.out.println();
    }
}
  • If you have a specific width that you want to use often you can overload the print method as follows. Then you can use the one argument version which invokes the two argument with the default width.
private  static int DEFAULT_WIDTH = 4;
public static void matPrint(int[][] m) {
    matPrint(m, DEFAULT_WIDTH);
}
  • And if you specify the field width as a negative value, it will left justify the output within each field.
  •  Tags:  
  • java
  • Related