Given two N x M matrices. Find a N x M matrix as the sum of given matrices each value at the sum of values of corresponding elements of the given two matrices.

Approach: Below is the idea to solve the problem.
Iterate over every cell of matrix (i, j), add the corresponding values of the two matrices and store in a single matrix i.e. the resultant matrix.
Try It Yourself
Follow the below steps to Implement the idea:
- Initialize a resultant matrix res[N][M].
- Run a for loop for counter i as each row and in each iteration:
- Run a for loop for counter j as each column and in each iteration:
- Add values of the two matrices for index i, j and store in res[i][j].
- Run a for loop for counter j as each column and in each iteration:
- Return res.
Below is the Implementation of above approach.
#include <iostream>
#include <vector>
using namespace std;
void add(vector<vector<int>>& A, vector<vector<int>>& B,
vector<vector<int>>& C) {
int n = A.size();
int m = A[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
}
int main() {
vector<vector<int>> A = { {1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4} };
vector<vector<int>> B = { {1, 1, 1},
{2, 2, 2},
{3, 3, 3},
{4, 4, 4} };
int n = A.size();
int m = A[0].size();
vector<vector<int>> C(n, vector<int>(m));
add(A, B, C);
cout << "Result matrix is:" << endl;
for (const auto& row : C) {
for (int val : row) {
cout << val << " ";
}
cout << endl;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define N 4 // Number of rows
#define M 3 // Number of columns
void add(int A[][M], int B[][M], int C[][M]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
}
int main() {
int A[N][M] = { {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4} };
int B[N][M] = { {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4} };
int C[N][M];
add(A, B, C);
printf("Result matrix is:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
public class GfG {
public static void add(int[][] A, int[][] B, int[][] C) {
int n = A.length;
int m = A[0].length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
}
public static void main(String[] args) {
int[][] A = { {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4} };
int[][] B = { {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4} };
int[][] C = new int[4][3];
add(A, B, C);
System.out.println("Result matrix is:");
for (int[] row : C) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
}
def add(A, B):
n = len(A)
m = len(A[0])
C = [[0] * m for _ in range(n)]
for i in range(n):
for j in range(m):
C[i][j] = A[i][j] + B[i][j]
return C
A = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4] ]
B = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4] ]
C = add(A, B)
print("Result matrix is:")
for row in C:
print(' '.join(map(str, row)))
function add(A, B) {
let n = A.length;
let m = A[0].length;
let C = Array.from({ length: n }, () => Array(m).fill(0));
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
return C;
}
const A = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4] ];
const B = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4] ];
const C = add(A, B);
console.log("Result matrix is:");
C.forEach(row => {
console.log(row.join(' '));
});
<?php
function add($A, $B) {
$n = count($A);
$m = count($A[0]);
$C = array_fill(0, $n, array_fill(0, $m, 0));
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $m; $j++) {
$C[$i][$j] = $A[$i][$j] + $B[$i][$j];
}
}
return $C;
}
$A = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4] ];
$B = [ [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4] ];
$C = add($A, $B);
echo "Result matrix is:\n";
foreach ($C as $row) {
echo implode(' ', $row) . "\n";
}
?>
Output
Result matrix is 2 2 2 2 4 4 4 4 6 6 6 6 8 8 8 8
Time complexity: O(n x m).
Auxiliary space: O(n x m). since n2 extra space has been taken for storing results
The program can be extended for rectangular matrices. The following post can be useful for extending this program.