-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path2Darray.js
67 lines (58 loc) · 1.24 KB
/
2Darray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
how to find total row = martix.length
how to find total column = matrinx[0].length
how to find particular index value with linear array
(col * i) + j = linear array index value
like:
[
0 1 2
0 [1, 2, 3],
1 [4, 5, 6],
2 [7, 8, 9],
];
[1,2,3,4,5,6,7,8,9]
how to find 2D array index value with linear array
like:
[1,2,3,4,5,6,7,8,9]
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
row = mid / col;
col = mid % col;
*/
// Print Rom Sum
function PrintSum(arr, row, col) {
for (let i = 0; i < row; i++) {
var sum = 0;
for (let j = 0; j < col; j++) {
sum += arr[i][j];
}
console.log(sum);
}
}
// Print Col Sum
function PrintSum(arr, row, col) {
for (let i = 0; i < col; i++) {
if (i % 2 == 0) {
for (let j = 0; j < row; j++) {
console.log(arr[j][i]);
}
} else {
for (let j = row - 1; j >= 0; j--) {
console.log(arr[j][i]);
}
}
}
}
function array2D() {
var arr = [
[1, 2, 3],
[4, 5, 6],
[7, 7, 8],
];
// console.log(arr);
PrintSum(arr, 3, 3);
}
array2D();