6 kyu

Matrix Addition

4,278 of 13,951xDranik

Description:

Write a function that accepts two square matrices (N x N two dimensional arrays), and return the sum of the two. Both matrices being passed into the function will be of size N x N (square), containing only integers.

How to sum two matrices:

Take each cell [n][m] from the first matrix, and add it with the same [n][m] cell from the second matrix. This will be cell [n][m] of the solution matrix. (Except for C where solution matrix will be a 1d pseudo-multidimensional array).

Visualization:

|1 2 3|     |2 2 1|     |1+2 2+2 3+1|     |3 4 4|
|3 2 1|  +  |3 2 3|  =  |3+3 2+2 1+3|  =  |6 4 4|
|1 1 1|     |1 1 3|     |1+1 1+1 1+3|     |2 2 4|

Example

matrixAddition(
  [ [1, 2, 3],
    [3, 2, 1],
    [1, 1, 1] ],
//      +
  [ [2, 2, 1],
    [3, 2, 3],
    [1, 1, 3] ] )

// returns:
  [ [3, 4, 4],
    [6, 4, 4],
    [2, 2, 4] ]
Matrix
Arrays
Algorithms

Stats:

CreatedOct 19, 2013
PublishedOct 19, 2013
Warriors Trained21407
Total Skips2322
Total Code Submissions32677
Total Times Completed13951
JavaScript Completions4278
CoffeeScript Completions134
Ruby Completions930
C# Completions658
PHP Completions462
Python Completions3445
C++ Completions1297
Java Completions2224
C Completions517
Elixir Completions106
Lua Completions128
NASM Completions10
Rust Completions93
Scala Completions16
TypeScript Completions174
Total Stars255
% of votes with a positive feedback rating92% of 1519
Total "Very Satisfied" Votes1303
Total "Somewhat Satisfied" Votes185
Total "Not Satisfied" Votes31
Ad
Contributors
  • xDranik Avatar
  • jhoffner Avatar
  • SagePtr Avatar
  • anter69 Avatar
  • stevehopkinson Avatar
  • donaldsebleung Avatar
  • docgunthrop Avatar
  • Voile Avatar
  • Souzooka Avatar
  • rowcased Avatar
  • FArekkusu Avatar
  • sazzadshopno Avatar
  • hobovsky Avatar
  • uniapi Avatar
  • xavierguihot Avatar
  • trashy_incel Avatar
  • username0 Avatar
  • ejini战神 Avatar
  • MobulaKuhlii Avatar
  • akar-0 Avatar
  • skaarj1989 Avatar
  • Mednoob Avatar
  • mouthbreather Avatar
  • KayleighWasTaken Avatar
Ad