Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C++ Program to Perform Matrix Addition

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

Photo Credit to CodeToFun

πŸ™‹ Introduction

Matrix addition is a fundamental operation in linear algebra and computer science. It involves adding corresponding elements of two matrices to create a new matrix.

This operation is essential in various fields, including computer graphics, scientific computing, and data analysis.

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

Understanding matrix operations and implementing them in C can provide a solid foundation for more complex computations.

πŸ“„ Example

Let's delve into the C++ code that achieves matrix addition.

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

// Function to perform matrix addition
void addMatrices(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] << " ";
    }
    std::cout << std::endl;
  }
}

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

  // Call the function to perform matrix addition
  addMatrices(matrix1, matrix2, resultMatrix);

  // Displaying matrices and result
  std::cout << "Matrix 1:\n";
  displayMatrix(matrix1);

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

  std::cout << "\nResultant Matrix:\n";
  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:
1 2 3 
4 5 6 
7 8 9 

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

Resultant Matrix:
10 10 10 
10 10 10 
10 10 10 

Compile and run the program to see the result of the matrix addition

🧠 How the Program Works

  1. The program defines a function addMatrices to add two matrices and a function displayMatrix to display a matrix.
  2. In the main function, sample matrices matrix1 and matrix2 are provided for testing.
  3. The addMatrices function is called to perform the addition, and the result is stored in resultMatrix.
  4. The matrices are displayed using the displayMatrix function.

🧐 Understanding the Concept of Matrix Addition

Matrix addition involves adding corresponding elements of two matrices to create a new matrix.

Given two matrices A and B, the sum C is calculated as follows:

Cij = Aij + Bij.

where Cij is the element at the i-th row and j-th column of matrix C, and Aij and Bij are the corresponding elements of matrices A and B.

Matrix addition is only defined for matrices of the same size; that is, they must have the same number of rows and columns.

🎒 Optimizing the Program

While the provided program is effective, there are optimizations that can be applied for larger matrices. Consider exploring techniques such as parallelization for improved performance.

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 Addition), 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