forked from Prakshalshah78/DSA_Relevel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
28_05-2D Array-1.js
200 lines (136 loc) · 3.09 KB
/
28_05-2D Array-1.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const studentNaina = {
firstName:'Naina',
age:32,
occupation:'Student'
};
const studentHariharan={
firstName:'Hariharan',
age:28,
occupation:'Software Engineer'
};
const studentDipanshu={
firstName:'Dipanshu',
age:25,
occupation:'Software Engineer'
};
const studentDatabase=[studentNaina,studentHariharan,studentDipanshu]
studentDatabase.push({
firstName:'Suroj',
age:22,
occupation:'Student'
})
for(let i=0;i<studentDatabase.length;i++){ //console.log(studentDatabase[i].firstName )
}
//console.log(studentDatabase )
//Array of Objects
const nainaInfo=['Naina',28,'Student'];
const dipanshuInfo=['Dipanshu',25,'Software Engineer'];
const surojInfo=['Suroj',21,'Software Engineer'];
const studentsInfo = [nainaInfo,dipanshuInfo,surojInfo]
for(let i=0;i<studentsInfo.length;i++){
//console.log(studentsInfo[i][0])
}
//console.log(studentsInfo)
//Array of Arrays => 2D array
//2D array
const students =
[
/*0th index of an outer array*/
['Naina',28,'Student'],
//0 1 2
/*1st index of an outer array*/
['Dipanshu',25,'Software Engineer'],
/*2nd index of an outer array*/
['Suroj',21,'Software Engineer']
]
//2D arrays travarsal
for(let i=0;i<students.length;i++){
for(let j=0;j<students[i].length;j++){
console.log(students[i][j])
}
}
//=============================Wave Matrix=============================
function waveMatrix(mat){
for(let i=0;i<mat[0].length;i++){
if(i%2==0){
//top to down
for (let j=0;j<mat.length;j++){
console.log(mat[j][i])
}
}
else{
//bottom up
for(let j=mat.length-1;j>=0;j--){
console.log(mat[j][i])
}
}
}
}
let mat=[[1,2,3],
[4,5,6],
[7,8,9]]
waveMatrix(mat)
//Transpose matrix
const arr=[[1,1,1,4],
[2,2,2,4],
[3,3,3,4]]
//3*4
//4*3
/*[[1,2,3],
[1,2,3],
[1,2,3]
[4,4,4]]*/
[
[],
[],
[],
[]
]
var output = [...Array(arr[0].length)].map(e=>Array(arr.length))
for(let i=0;i<arr.length;i++){
for(let j=0;j<arr[0].length;j++){
output[j][i]=arr[i][j]
}
}
console.log(output)
//Multiplication
let one = [
[1,2,3],
[4,5,6]
]
//2*3
//1*1+4*2
let two=[
[1,2],
[3,4],
[5,6]
]
//3*2
let r1= one.length;
let c1=one[0].length;
let r2= two.length;
let c2=two[0].length;
let res = [...Array(r1)].map(e=>Array(c2));
for(let i=0;i<res.length;i++){
for(let j=0;j<res[0].length;j++){
let sum =0;
for (let k=0;k<c1;k++){
let val =one[i][k]*two[k][j];
sum+=val
}
res[i][j]=sum
}
}
console.log(res)
let mat=[[1,2,3],
[4,5,6],
[7,8,9]]
let n=3;
function rotate180Mat(mat){
for(let i=n-1;i>=0;i--){
for(let j=n-1;j>=0;j--){
console.log(mat[i][j])
}
}
}
rotate180Mat(mat);