matrixAddition.js (***Revise Hard please***)
/* Matrix Addition
Input:-
3 - Rows
4 - Col
1,2,3,4
5,6,7,8
9,10,11,12
20,21,22,23
24,25,26,27
28,29,30,31
Output:
[ [ 21, 23, 25, 27 ], [ 29, 31, 33, 35 ], [ 37, 39, 41, 43 ] ]
*/
let fs = require("fs");
let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');
function readLine() {
idx++;
return data[idx - 1].trim();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
const noOfRows = parseInt(readLine()); // 3
const noOfColoumns = parseInt(readLine()); // 4
const A = []; // we will push the first matrix here afterwards
for (let i = 0; i < noOfRows; i++) // this will give us the array but not in integer form
{
let subArr = readLine().split(',');
//console.log(subArr);
for (let j = 0; j < noOfColoumns; j++) // this will convert each element into an integer
{
subArr[j] = parseInt(subArr[j]);
}
A.push(subArr);
}
console.log(A);
TERMINAL:
const B = []; // we will push the first matrix B here afterwards
for (let i = 0; i < noOfRows; i++) // this will give us the array but not in integer form
{
let subArr = readLine().split(',');
//console.log(subArr);
for (let j = 0; j < noOfColoumns; j++) // this will convert each element into an integer
{
subArr[j] = parseInt(subArr[j]);
}
//console.log(subArr);
B.push(subArr);
}
console.log(B);
TERMINAL:







Comments
Post a Comment