Table of Contents
Problem Statement
In the “Addition of Two Matrices” problem, we have given two matrices a and b. We have to find the final matrix after adding matrix b in matrix a. If the order is the same for both the matrices then only we can add them otherwise we can’t. If we can’t add them then print “We can’t add these matrices”.
Input Format
The first line containing four integer values r1, c1, r2, c2. Where r1 and c1 denote the number of rows and columns of the first matrix. And r2, c2 denotes the number of rows, columns of the second matrix.
Next r1 lines containing c1 integer values.
And next r2 lines containing c2 integer values.
Output Format
Print the final matrix after adding in such a way that every row starts from the new line and every element separated by space in each row.
Constraints
- 1<=r1, c1, r2, c2<=5000.
- 1<= |m[i][j]| <=10^9 where m is the matrix and the position of element at ith row and jth column.
Example
2 2 2 2 4 8 3 7 1 0 5 2
5 8 8 9
Explanation: We can find the addition of matrices by adding the corresponding entries in matrix A and B.
Algorithm for Addition of Two Matrices
1. For each row in the two matrices.
1.1 Add the respective elements in matrix B with the elements in matrix A.
1.2 Store the result in the same position in some auxiliary matrix.
2. Move to the next row and follow steps 1.1 and 1.2 till the end of the matrices.
3. Print the auxiliary matrix.
Implementation
C++ Program for Addition of Two Matrices
#include <bits/stdc++.h> using namespace std; int main() { int r1,c1,r2,c2; cin>>r1>>c1>>r2>>c2; int a[r1][c1]; int b[r2][c2]; for(int i=0;i<r1;i++) { for(int j=0;j<c1;j++) { cin>>a[i][j]; } } for(int i=0;i<r2;i++) { for(int j=0;j<c2;j++) { cin>>b[i][j]; } } if(c1!=c2 || r1!=r2) { cout<<"We can’t add these matrices"; } else { int c[r1][c2]; for(int i=0;i<r1;i++) { for(int j=0;j<c2;j++) { c[i][j]=a[i][j]+b[i][j]; cout<<c[i][j]<<" "; } cout<<endl; } } return 0; }
Java Program for Addition of Two Matrices
import java.io.*; import java.util.Scanner; class TutorialCup { // Driver code public static void main(String[] args) { int r1,c1,r2,c2; Scanner inp = new Scanner(System.in); r1 = inp.nextInt(); c1 = inp.nextInt(); r2 = inp.nextInt(); c2 = inp.nextInt(); int a[][] = new int[r1][c1]; for(int i=0;i<r1;i++) { for(int j=0;j<c1;j++) { a[i][j]=inp.nextInt(); } } int b[][] = new int[r2][c2]; for(int i=0;i<r2;i++) { for(int j=0;j<c2;j++) { b[i][j]=inp.nextInt(); } } if(r1!=r2 && c1!=c2) { System.out.println("\nWe can’t add these matrices."); } else { int c[][] = new int[r1][c1]; for(int i=0;i<r1;i++) { for(int j=0;j<c1;j++) { c[i][j]=a[i][j]+b[i][j]; System.out.print(c[i][j] + " "); } System.out.println(); } } } }
2 2 2 2 2 8 0 9 5 6 11 3
7 14 11 12
Complexity Analysis for Addition of Two Matrices
Time Complexity
O(n^2) where n is the maximum of r1 and c1. Here we simply run two loops first loop run r1 times and the second loop runs c1 times.
Space Complexity
O(m^2) where m is the maximum of r1 and c1. Here we create extra space for storing the result of the addition of given matrices. Here we also declared r1*c1 size for taking input the first matrix and r2*c2 size for taking input the second matrix.