Looking For Anything Specific?

How to Add and Subtract Two Matrices in C++, Python, and JavaScript

A matrix is a rectangular array of numbers, symbols, or expressions arranged in rows and columns. This rectangular grid of numbers is commonly used in mathematics, electrical engineering, and computer science. Matrices were originally created to describe the system of linear equations.

Now matrices are widely used in image processing, genetic analysis, big data, and programming. The addition and subtraction of matrices are the two most common matrix operations. In this article, you'll learn how to add and subtract two matrices.

Rules for Matrix Addition

Follow these rules to add two matrices:

  • Two matrices can only be added if they're of the same order.
  • If the two matrices are of the same order, add the corresponding elements of the two matrices i.e., add the elements which have the same positions.

In example 1, the matrices can be added because they have the same order. In example 2, the matrices can't be added because they don't have the same order.

C++ Program to Add Two Matrices

Below is the C++ program to add two matrices:

// C++ program for addition of two matrices
#include <bits/stdc++.h>
using namespace std;
// The order of the matrix is 3 x 3
#define size1 3
#define size2 3
// Function to add matrices mat1[][] & mat2[][],
// and store the result in matrix result[][]
void addMatrices(int mat1[][size2], int mat2[][size2], int result[][size2])
{
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
// Driver code
int main()
{
// 1st Matrix
int mat1[size1][size2] = { {9, 8, 7},
{6, 8, 0},
{5, 9, 2} };
// 2nd Matrix
int mat2[size1][size2] = { {4, 7, 6},
{8, 8, 2},
{7, 3, 5} };

// Matrix to store the result
int result[size1][size2];
// Calling the addMatrices() function
addMatrices(mat1, mat2, result);
cout << "mat1 + mat2 = " << endl;
// Printing the sum of the 2 matrices
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}

Output:

mat1 + mat2 = 
13 15 13
14 16 2
12 12 7

Python Program to Add Two Matrices

Below is the Python program to add two matrices:

# Python program for addition of two matrices
# The order of the matrix is 3 x 3
size1 = 3
size2 = 3
# Function to add matrices mat1[][] & mat2[][],
# and store the result in matrix result[][]
def addMatrices(mat1,mat2,result):
for i in range(size1):
for j in range(size2):
result[i][j] = mat1[i][j] + mat2[i][j]
# driver code
# 1st Matrix
mat1 = [ [9, 8, 7],
[6, 8, 0],
[5, 9, 2] ]
# 2nd Matrix
mat2 = [ [4, 7, 6],
[8, 8, 2],
[7, 3, 5] ]
# Matrix to store the result
result = mat1[:][:]
# Calling the addMatrices function
addMatrices(mat1, mat2, result)
# Printing the sum of the 2 matrices
print("mat1 + mat2 = ")
for i in range(size1):
for j in range(size2):
print(result[i][j], " ", end='')
print()

Output:

mat1 + mat2 =
13 15 13
14 16 2
12 12 7

C Program to Add Two Matrices

Below is the C program to add two matrices:

// C program for addition of two matrices
#include <stdio.h>
// The order of the matrix is 3 x 3
#define size1 3
#define size2 3
// Function to add matrices mat1[][] & mat2[][],
// and store the result in matrix result[][]
void addMatrices(int mat1[][size2], int mat2[][size2], int result[][size2])
{
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
// Driver code
int main()
{
// 1st Matrix
int mat1[size1][size2] = { {9, 8, 7},
{6, 8, 0},
{5, 9, 2} };
// 2nd Matrix
int mat2[size1][size2] = { {4, 7, 6},
{8, 8, 2},
{7, 3, 5} };

// Matrix to store the result
int result[size1][size2];
// Calling the addMatrices function
addMatrices(mat1, mat2, result);
printf("mat1 + mat2 = \⁠n");
// Printing the sum of the 2 matrices
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
printf("%d ", result[i][j]);
}
printf("\⁠n");
}
return 0;
}

Output:

mat1 + mat2 = 
13 15 13
14 16 2
12 12 7

JavaScript Program to Add Two Matrices

Below is the JavaScript program to add two matrices:

<script>
// JavaScript program for addition of two matrices
// The order of the matrix is 3 x 3
let size1 = 3;
let size2 = 3;
// Function to add matrices mat1[][] & mat2[][],
// and store the result in matrix result[][]
function addMatrices(mat1, mat2, result) {
for (let i = 0; i < size1; i++) {
for (let j = 0; j < size2; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
// Driver code
// 1st Matrix
mat1 = [ [9, 8, 7],
[6, 8, 0],
[5, 9, 2] ]
// 2nd Matrix
mat2 = [ [4, 7, 6],
[8, 8, 2],
[7, 3, 5] ]
// Matrix to store the result
let result = new Array(size1);
for (let k = 0; k < size1; k++) {
result[k] = new Array(size2);
}
// Calling the addMatrices function
addMatrices(mat1, mat2, result);
document.write("mat1 + mat2 = <br>")
// Printing the sum of the 2 matrices
for (let i = 0; i < size1; i++) {
for (let j = 0; j < size2; j++) {
document.write(result[i][j] + " ");
}
document.write("<br>");
}
</script>

Output:

mat1 + mat2 =
13 15 13
14 16 2
12 12 7

Rules for Matrix Subtraction

Follow these rules to subtract two matrices:

  • Two matrices can be subtracted only if they're of the same order.
  • If the two matrices are of the same order, subtract the corresponding elements of the two matrices i.e, subtract the elements which have the same positions.

In example 1, the matrices can be subtracted because they have the same order. In example 2, the matrices can't be subtracted because they don't have the same order.

C++ Program to Subtract Two Matrices

Below is the C++ program to subtract two matrices:

Related: These Sites Will Help You Learn C++ Programming

// C++ program for subtraction of two matrices
#include <bits/stdc++.h>
using namespace std;
// The order of the matrix is 3 x 3
#define size1 3
#define size2 3
// Function to subtract matrices mat1[][] & mat2[][],
// and store the result in matrix result[][]
void subtractMatrices(int mat1[][size2], int mat2[][size2], int result[][size2])
{
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}
// Driver code
int main()
{
// 1st Matrix
int mat1[size1][size2] = { {9, 8, 7},
{6, 8, 0},
{5, 9, 2} };
// 2nd Matrix
int mat2[size1][size2] = { {4, 7, 6},
{8, 8, 2},
{7, 3, 5} };

// Matrix to store the result
int result[size1][size2];
// Calling the subtractMatrices() function
subtractMatrices(mat1, mat2, result);
cout << "mat1 - mat2 = " << endl;
// Printing the difference of the 2 matrices (mat1 - mat2)
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}

Output:

mat1 - mat2 = 
5 1 1
-2 0 -2
-2 6 -3

Python Program to Subtract Two Matrices

Below is the Python program to subtract two matrices:

# Python program for subtraction of two matrices
# The order of the matrix is 3 x 3
size1 = 3
size2 = 3
# Function to subtract matrices mat1[][] & mat2[][],
# and store the result in matrix result[][]
def subtractMatrices(mat1,mat2,result):
for i in range(size1):
for j in range(size2):
result[i][j] = mat1[i][j] - mat2[i][j]
# driver code
# 1st Matrix
mat1 = [ [9, 8, 7],
[6, 8, 0],
[5, 9, 2] ]
# 2nd Matrix
mat2 = [ [4, 7, 6],
[8, 8, 2],
[7, 3, 5] ]
# Matrix to store the result
result = mat1[:][:]
# Calling the subtractMatrices function
subtractMatrices(mat1, mat2, result)
# Printing the difference of the 2 matrices (mat1 - mat2)
print("mat1 - mat2 = ")
for i in range(size1):
for j in range(size2):
print(result[i][j], " ", end='')
print()

Output:

mat1 - mat2 =
5 1 1
-2 0 -2
-2 6 -3

C Program to Subtract Two Matrices

Below is the C program to subtract two matrices:

Related: Characteristics of C Programming That Make It Unique (And Better)

// C program for subtraction of two matrices
#include <stdio.h>
// The order of the matrix is 3 x 3
#define size1 3
#define size2 3
// Function to subtract matrices mat1[][] & mat2[][],
// and store the result in matrix result[][]
void subtractMatrices(int mat1[][size2], int mat2[][size2], int result[][size2])
{
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}
// Driver code
int main()
{
// 1st Matrix
int mat1[size1][size2] = { {9, 8, 7},
{6, 8, 0},
{5, 9, 2} };
// 2nd Matrix
int mat2[size1][size2] = { {4, 7, 6},
{8, 8, 2},
{7, 3, 5} };

// Matrix to store the result
int result[size1][size2];
// Calling the subtractMatrices() function
subtractMatrices(mat1, mat2, result);
printf("mat1 - mat2 = \⁠n");
// Printing the difference of the 2 matrices (mat1 - mat2)
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
printf("%d ", result[i][j]);
}
printf("\⁠n");
}
return 0;
}

Output:

mat1 - mat2 = 
5 1 1
-2 0 -2
-2 6 -3

JavaScript Program to Subtract Two Matrices

Below is the JavaScript program to subtract two matrices:

<script>
// JavaScript program for subtraction of two matrices
// The order of the matrix is 3 x 3
let size1 = 3;
let size2 = 3;
// Function to subtract matrices mat1[][] & mat2[][],
// and store the result in matrix result[][]
function subtractMatrices(mat1, mat2, result) {
for (let i = 0; i < size1; i++) {
for (let j = 0; j < size2; j++) {
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}
// Driver code
// 1st Matrix
mat1 = [ [9, 8, 7],
[6, 8, 0],
[5, 9, 2] ]
// 2nd Matrix
mat2 = [ [4, 7, 6],
[8, 8, 2],
[7, 3, 5]]
// Matrix to store the result
let result = new Array(size1);
for (let k = 0; k < size1; k++) {
result[k] = new Array(size2);
}
// Calling the subtractMatrices function
subtractMatrices(mat1, mat2, result);
document.write("mat1 - mat2 = <br>")
// Printing the difference of the 2 matrices (mat1 - mat2)
for (let i = 0; i < size1; i++) {
for (let j = 0; j < size2; j++) {
document.write(result[i][j] + " ");
}
document.write("<br>");
}
</script>

Output:

mat1 - mat2 =
5 1 1
-2 0 -2
-2 6 -3

If you want to have a look at the complete source code used in this article, here's the GitHub repository.

Related: Coding Challenges and Competitions That May Lead to Money or Jobs

Increase Your Programming Capability

You can increase your programming capability by practicing a variety of programming problems. Solving these programming problems helps you develop basic programming principles. These are a must-know if you're to become an efficient programmer.


Post a Comment

0 Comments