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