function printAlignedMatrix(matrix) {
const columnWidths = matrix[0].map((_, colIndex) =>
Math.max(...matrix.map(row => String(row[colIndex]).length)
));
matrix.forEach(row => {
let rowStr = '';
row.forEach((cell, colIndex) => {
const formattedCell = String(cell).padStart(columnWidths[colIndex]);
rowStr += formattedCell + ' ';
});
console.log(rowStr);
});
}
function generateRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
function generateRandomMatrix(size) {
let matrix = Array(size).fill(0).map(()=>new Array(size).fill(0));
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
matrix[i][j] = generateRandomInteger(1, 100);
}
}
return matrix;
}
const matrix = generateRandomMatrix(20);
printAlignedMatrix(matrix);
/*
run:
18 92 92 16 62 65 57 60 80 92 9 95 86 15 9 63 35 42 51 25
53 18 94 79 47 5 48 78 5 4 98 86 10 29 79 34 32 35 18 38
78 89 43 27 60 42 5 69 23 5 17 96 37 47 82 14 32 24 58 44
38 97 71 59 39 14 30 24 84 72 60 88 4 4 23 22 58 35 18 15
27 88 100 77 19 51 58 52 21 20 41 23 18 96 20 85 50 88 42 62
83 89 5 77 33 70 25 90 63 66 46 83 100 88 11 99 49 32 37 4
50 84 71 58 86 53 30 79 77 14 76 65 27 76 67 37 91 97 43 35
12 66 1 24 80 20 94 40 49 74 21 71 21 31 39 96 53 5 67 71
59 15 64 99 1 46 24 76 93 22 55 83 83 68 17 23 33 98 86 92
98 25 97 37 15 68 97 40 83 46 6 5 49 81 69 43 57 60 48 28
67 72 84 52 92 17 28 86 32 62 9 34 48 24 1 55 76 92 53 9
77 23 77 73 96 12 63 41 100 5 67 14 2 82 26 88 68 88 2 91
73 5 17 17 36 29 100 52 38 81 1 41 8 54 69 21 29 33 36 63
35 96 86 7 1 10 1 61 47 79 70 83 67 61 83 36 26 20 23 89
89 18 67 32 12 93 80 88 61 28 45 47 36 21 45 15 86 16 99 38
41 85 90 83 74 54 83 9 95 77 97 8 65 36 11 22 39 24 93 16
64 68 39 74 71 80 51 47 58 8 42 17 18 84 13 33 63 10 10 10
78 60 15 27 49 60 12 37 57 73 65 21 60 79 49 45 50 42 5 61
5 58 40 15 17 48 26 50 58 73 20 62 63 85 47 70 15 71 67 8
*/