-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion31.js
61 lines (46 loc) · 1.37 KB
/
question31.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
// Write a program to add and subtract of given (NXN) Matrices
var readlineSync = require('readline-sync');
var N1;
var N2;
function MatrixSize() {
N1 = readlineSync.question("Please enter 1st Matrix size: ");
N2 = readlineSync.question("Please enter 2nd Matrix size: ");
if (N1 != N2) {
console.log("These values do not produce a square matrix. please try again.");
MatrixSize();
return;
} else {
MatrixValues();
}
}
var array1 = [];
var array2 = [];
function MatrixValues() {
for (var i = 0; i < N1; i++) {
array1.push(readlineSync.question("First matrix value " + (i+1) + ": "));
}
for (var i = 0; i < N2; i++) {
array2.push(readlineSync.question("Second matrix value " + (i+1) + ": "));
}
console.log(array1);
console.log(array2);
AddMatrix();
}
addedArray = [];
function AddMatrix() {
for (var i = 0; i < array1.length; i++) {
addedArray.push(Number(array1[i]) + Number(array2[i]));
}
console.log("The addition of both Matrix is:");
console.log(addedArray);
SubtractMatrix();
}
subtractedArray = [];
function SubtractMatrix() {
for (var i = 0; i < array1.length; i++) {
subtractedArray.push(Number(array1[i]) - Number(array2[i]));
}
console.log("The subtraction of both Matrix is:");
console.log(subtractedArray);
}
MatrixSize()