Alaa A
Alaa A

Reputation: 21

I am trying to print out the lower right triangle of a 2D array

I am trying to print out the lower right triangle of a 2D array, but I get the element triangle reversed:

int[][] a3 = {
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,16},
    };
    System.out.println("Lower right triangle");
    for (int row = 0 ; row < a3.length ; row++){
        for (int col = 3 ; col >= a3[0].length-row-1 ; col--) {

            System.out.print("\t" + a3[row][col]);
        }
        System.out.println();
    }

The output looks like this:

4
8   7
12  11  10
16  15  14  13

Instead of:

             4
          7  8
      10 11 12
   13 14 15 16

Upvotes: 2

Views: 1032

Answers (4)

Jagrut Sharma
Jagrut Sharma

Reputation: 4754

Here is another way to print. You keep a flag to check if you have indented the very first number printed for a row. This indentation will depend upon the column you start printing from. Then, subsequently, all numbers printed will append a single indent after them.

The number of times the initial indentation happens is based on col, which is being calculated as a3[0].length - row - 1.

for (int row = 0; row < a3.length; row++) {
            boolean formatted = false;
            for (int col = a3[0].length - row - 1; col <= 3; col++) {

                if (!formatted) {
                    for (int i = 0; i <= col; i++) {
                        System.out.print("\t");
                        formatted = true;
                    }
                }
                System.out.print(a3[row][col] + (col < 3?"\t":""));
            }
            System.out.println();
        }
    }

Output:

            4
        7   8
    10  11  12
13  14  15  16

To visually see how the indents are applied, use a X in place of \t. This is the output. Hope this helps in understanding how initial indentation is being applied, and then the subsequent ones.

XXXX4
XXX7X8
XX10X11X12
X13X14X15X16

Upvotes: 0

Jacob
Jacob

Reputation: 168

Without giving you the complete answer, consider the following... In your solution you're working right to left. In order to best attack this problem you should print left to right (which is the order of traversal). In order to do that, you need to likewise leverage your row and column index to determine the print order:

            if(((a3[0].length - 1) - row) <= col) {
                System.out.print(a3[row][col] + "\t");
            } else {
                System.out.print("\t");
            }

Upvotes: 0

thaveethu gce
thaveethu gce

Reputation: 625

Here is the solution for you.. Try to understand the logic and ask if any doubt. hope it helps you

for (int row = 0 ; row < a3.length ; row++){
    for (int col = 0 ; col < a3[0].length ; col++) {
          if(col>=a3[0].length-1-row){
              System.out.print(a3[row][col]+"\t");

          } else {
              System.out.print("\t");
          }
    }
    System.out.println();
}

Upvotes: 1

C.S.Reddy Gadipally
C.S.Reddy Gadipally

Reputation: 1758

int[][] a3 = {
        {1,2,3,4},
        {5,6,7,8},
        {9,10,11,12},
        {13,14,15,16},
};
System.out.println("Lower right triangle");
for (int row = 0 ; row < a3.length ; row++){
    for (int col = 0 ; col <= a3[row].length-1 ; col++) {
        if(col < a3[row].length-row-1){
            System.out.print("\t");
        }else{
            System.out.print("\t" + a3[row][col]);
        }
    }
    System.out.println();
}

output:

Lower right triangle
          4
       7  8
   10 11 12
13 14 15 16

Upvotes: 0

Related Questions