matrixAddition.js ( without dry run)

 /* 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 A here afterwards


for (let i = 0; i < noOfRows; i++) // this will give us the array but not in integer form
{

let subArr = readLine().split(',');


for (j = 0; j < noOfColoumns; j++) // this will convert each element into an integer
{
subArr[j] = parseInt(subArr[j]);

}

A.push(subArr);
}

//console.log(A);

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
{
const subArr = readLine().split(',');

for (let j = 0; j < noOfColoumns; j++) // this will convert each element into an integer
{
subArr[j] = parseInt(subArr[j]);

}

B.push(subArr);

}

//console.log(B);

const ans = []; // we will push the addition here

for (let i = 0; i < noOfRows; i++) {

let subArr = [];

for (let j = 0; j < noOfColoumns; j++) {

subArr.push(A[i][j] + B[i][j]);
}

ans.push(subArr);
}

console.log(ans);


Comments