Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ Program to Perform Matrix Subtraction

Posted in C++ Tutorial
Updated on Oct 06, 2024
By Mari Selvan
πŸ‘οΈ 47 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
C++ Program to Perform Matrix Subtraction

Photo Credit to CodeToFun

πŸ™‹ Introduction

Matrix subtraction is a fundamental operation in linear algebra and computer science.

Matrix subtraction involves subtracting each element of one matrix from the corresponding element of another matrix of the same order.

In this tutorial, we'll explore a C++ program that performs matrix subtraction.

In the provided C++ program, we'll perform matrix subtraction for 3x3 matrices as an example. You can easily modify the program to work with matrices of different sizes.

πŸ“„ Example

Let's dive into the C++ code that performs matrix subtraction.

displayMatrix.cpp
Copied
Copy To Clipboard
#include <iostream>

// Function to perform matrix subtraction
void subtractMatrices(int mat1[3][3], int mat2[3][3], int result[3][3]) {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      result[i][j] = mat1[i][j] - mat2[i][j];
    }
  }
}

// Function to display a matrix
void displayMatrix(int matrix[3][3]) {
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      std::cout << matrix[i][j] << "\t";
    }
    std::cout << std::endl;
  }
}

// Driver program
int main() {
  // Sample matrices for subtraction
  int matrix1[3][3] = {
    {5, 8, 2},
    {7, 4, 9},
    {3, 6, 1}
  };
  int matrix2[3][3] = {
    {3, 1, 7},
    {6, 9, 2},
    {8, 5, 4}
  };
  int resultMatrix[3][3];

  // Perform matrix subtraction
  subtractMatrices(matrix1, matrix2, resultMatrix);

  // Display matrices and result
  std::cout << "Matrix 1:" << std::endl;
  displayMatrix(matrix1);

  std::cout << "\nMatrix 2:" << std::endl;
  displayMatrix(matrix2);

  std::cout << "\nResultant Matrix (Matrix1 - Matrix2):" << std::endl;
  displayMatrix(resultMatrix);

  return 0;
}

πŸ’» Testing the Program

Feel free to replace the sample matrices with your own 3x3 matrices in the main function to test the program with different input.

Output
Matrix 1:
5  8  2
7  4  9
3  6  1

Matrix 2:
3   1   7
6   9   2
8   5   4

Resultant Matrix (Matrix1 - Matrix2):
2   7  -5
1  -5   7
-5  1  -3

Compile and run the program to see the result of the matrix subtraction.

🧠 How the Program Works

  1. The program defines a function subtractMatrices that takes two 3x3 matrices as input and calculates their subtraction, storing the result in another matrix.
  2. It also defines a function displayMatrix to print a matrix.
  3. In the main function, sample 3x3 matrices matrix1 and matrix2 are defined, and matrix subtraction is performed using the subtractMatrices function.
  4. The result is displayed using the displayMatrix function.

🧐 Understanding the Concept of Matrix Subtraction

Matrix subtraction is performed element-wise, subtracting the corresponding elements of one matrix from the elements of another matrix. The result is a new matrix of the same order.

In the provided example, the element in the first row and first column of the result matrix is obtained by subtracting the element in the first row and first column of Matrix 2 from the element in the first row and first column of Matrix 1, and so on.

πŸŽ‰ Conclusion

Matrix subtraction is a fundamental operation in linear algebra and computer science. This C program provides a practical implementation of matrix subtraction and serves as a foundation for more advanced matrix operations.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (C++ Program to Perform Matrix Subtraction), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy