Reputation: 21
Hi I am trying to create a matrix on the console using 2D array. The idea is that the output should look like this one :
1|8|9 |16
2|7|10|15
3|6|11|14
4|5|12|13
Is there any one who has an idea how it can be done?
Upvotes: 0
Views: 4617
Reputation: 1
const matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
];
let i = 1;
for (let col = 0; col < matrix[0].length; col++) {
if (col % 2 === 1) {
for (let row = matrix.length - 1; row >= 0; row--) {
matrix[row][col] = i++;
}
} else {
for (let row = 0; row < matrix.length; row++) {
matrix[row][col] = i++;
}
}
}
console.table(matrix);
Upvotes: 0
Reputation: 21
finally with your help and after looking carefully at how multidimensional arrays work I solved the problem which now is looking quite simple to me.
int a = 4;
int b = 4;
int c = 1;
boolean direction = true;
int[][] arrey = new int[a][b];
for (int y = 0; y <= b - 1; y++) {
if (direction) {
for (int x = 0; x <= a - 1; x++) {
arrey[x][y] = c;
c++;
}
direction = false;
} else {
for (int x = a - 1; x >= 0; x--) {
arrey[x][y] = c;
c++;
}
direction = true;
}
}
for (int x = 0; x <= a - 1; x++) {
for (int y = 0; y <= b - 1; y++) {
System.out.print("["+arrey[x][y]+"]");
}
System.out.println("");
}
Upvotes: 0
Reputation: 213281
Few things you can guess from the matrix: -
First, you have to traverse all rows of a columns first before moving to the next column
Second, you need to alternate between downwards
and upwards
direction on each iteration
So, you would need two nested for loop, for iterating through rows for a particular column. One will go from row 0 to max - 1
, and the next will go from row = max - 1 to 0
.
Now, to alternate the iteration direction, you can use a boolean variable, and toggle it after each iteration of inner loop finishes.
Each loop needs to be enclosed inside an if-else
. Both of them will be executed on a certain condition. If boolean downwards = false;
, then loop moving upwards will be executed and vice-versa.
On each iteration, fill the current cell with an integer counter, that you would have to initialize with 1
, and increment it after each fill.
Pseudo code : -
// Initialize variables row, col, and count = 1
boolean goDown = true;
int[][] matrix = new int[row][col]; // declare matrix
for i = 0 to col:
if (goDown)
for j = 0 to row: // Move in downwards direction
assign count++ to matrix[j][i]
// assign to `[j][i]` because, we have to assign to rows first
goDown = false; // Toggle goDown
else
for j = row - 1 to 0: // Move in upwards direction
assign count++ to matrix[j][i]
goDown = true; // toggle goDown
}
Upvotes: 2
Reputation: 12562
Just some psuedo-code, hope it helps and gives you something to start with.
boolean goUp = false;
boolean goDown = true;
size = 4;
matrix[size][size];
k = 0;
l =0;
loop i->0 i < size*size i++
matrix[l][k] = i;
if(l==size and goDown)
goDown = false;
goUp = true;
k++;
else if(l==0 and goUp)
goDown = true;
goUp = false;
k++;
else
l = l+ (1*goDown?1:-1);
end loop;
Upvotes: 0