Example 70: Addition of Matrices

Learn how to add two matrices.

Problem

Write a program to obtain the sum of two 3 x 3 matrices.

The function takes in three 2-D arrays as parameters, where the first two arrays represent the original matrices, and in the third array, you need to store the result of addition.

Example

Input (Matrix 1, Matrix 2) Output (Matrix 3)
1 1 1
1 1 1
1 1 1

2 2 2
2 2 2
2 2 2
3 3 3
3 3 3
3 3 3
1 2 3
1 2 3
1 2 3

2 3 4
2 3 4
2 3 4
3 5 7
3 5 7
3 5 7

Demonstration

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.