0

how can I formulate two-dimensional array-oriented program TwoDimMatrix that will produce the given output TY :)

Sample output:

1 10 11 20 21  
2 9  12 19 22  
3 8  13 18 23  
4 7  14 17 24  
5 6  15 16 25
3
  • Why do you need arrays for this? Commented Oct 23, 2017 at 12:11
  • I always have 12345 instead of vertical Commented Oct 23, 2017 at 12:12
  • @JustinSanGabriel "I always have 12345 instead of vertical" <- This is a matter of presentation aka how you output your two dimensional array and has nothing to do with how you create it. Commented Oct 23, 2017 at 12:34

1 Answer 1

1

I think out there must be a lot of better solutions than this, but you can try it:

int[][] array = new int[5][5];

int value = 1,  flag = 0;

for (int i = 0; i < 5; i++) {
    if (flag == 0) {
        for (int j = 0; j < 5; j++) {
            array[j][i] = value++;
        }
        flag = 1;
    } else {
        for (int j = 4; j >= 0; j--) {
            array[j][i] = value++;
        }
        flag = 0;
    }
}

for (int i = 0; i < 5; i++) {
    System.out.println(Arrays.toString(array[i]));
}

This snippet will print your desired output.

Sign up to request clarification or add additional context in comments.

1 Comment

Ty very much :) <3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.