forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix_addition.js
140 lines (130 loc) · 3.53 KB
/
Matrix_addition.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
/*
In mathematics, matrix addition is the operation of adding two matrices by adding
the corresponding entries together.This program takes two matrices of order n*m and
stores it in two-dimensional array. Then, the program adds these two matrices and
displays it on the screen.
*/
function matrix_addition(a_1, n1, m1, a_2) {
//res is the result array
let res = new Array(n1);
for (let i = 0; i < n1; i++) {
res[i] = new Array(m1);
}
for (let i = 0; i < n1; i++) {
for (let j = 0; j < m1; j++) {
res[i][j] = a_1[i][j] + a_2[i][j];
}
}
return res;
}
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const getLine = (function () {
const getLineGen = (async function* () {
for await (const line of rl) {
yield line;
}
})();
return async () => ((await getLineGen.next()).value);
})();
const main = async () => {
console.log("Enter the number of rows in the first matrix");
let n1 = Number(await getLine());
console.log("Enter the number of columns in the first matrix");
let m1 = Number(await getLine());
console.log("Enter the elements of the first matrix");
//initializing array
let a_1 = new Array(n1);
for (let i = 0; i < n1; i++) {
a_1[i] = new Array(m1);
}
for (let i = 0; i < n1; i++) {
for (let j = 0; j < m1; j++) {
console.log("Enter the element at position " + i + " , " + j);
a_1[i][j] = Number(await getLine());
}
}
console.log("Enter the number of rows in the second matrix");
let n2 = Number(await getLine());
console.log("Enter the number of columns in the second matrix");
let m2 = Number(await getLine());
console.log("Enter the elements of the second matrix");
//initializing array
let a_2 = new Array(n2);
for (let i = 0; i < n2; i++) {
a_2[i] = new Array(m2);
}
for (let i = 0; i < n2; i++) {
for (let j = 0; j < m2; j++) {
console.log("Enter the element at position " + i + " , " + j);
a_2[i][j] = Number(await getLine());
}
}
let res = matrix_addition(a_1, n1, m1, a_2);
let s = '';
for (let i = 0; i < n1; i++) {
for (let j = 0; j < m1; j++) {
s += res[i][j] + " ";
}
s += '\n';
}
console.log("Result of matrix addition is");
console.log(s);
process.exit(0);
};
main();
/*
Sample I/O:
Enter the number of rows in the first matrix
3
Enter the number of columns in the first matrix
3
Enter the elements of the first matrix
Enter the element at position 0 , 0
1
Enter the element at position 0 , 1
2
Enter the element at position 0 , 2
3
Enter the element at position 1 , 0
4
Enter the element at position 1 , 1
5
Enter the element at position 1 , 2
6
Enter the element at position 2 , 0
7
Enter the element at position 2 , 1
8
Enter the element at position 2 , 2
9
Enter the number of rows in the second matrix
3
Enter the number of columns in the second matrix
3
Enter the elements of the second matrix
Enter the element at position 0 , 0
9
Enter the element at position 0 , 1
8
Enter the element at position 0 , 2
7
Enter the element at position 1 , 0
6
Enter the element at position 1 , 1
5
Enter the element at position 1 , 2
4
Enter the element at position 2 , 0
3
Enter the element at position 2 , 1
2
Enter the element at position 2 , 2
1
Result of matrix addition is
10 10 10
10 10 10
10 10 10
Time complexity : O(n^2)
Space complexity : O(n^2)
*/