Add Two Matrix In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Add Two Matrix In C++

Add Two Matrix In C++

BLUF: Mastering Add Two Matrix In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Add Two Matrix In C++

C++ is renowned for its efficiency. Learn how Add Two Matrix In C++ enables low-level control and high-performance computing in the tutorial below.

In C++, a matrix represents a 2D array containing elements organized in rows and columns. Different techniques can be employed to instantiate a matrix, like employing nested for loops or dynamically assigning memory. One approach to forming a matrix in C++ involves defining a 2D array using the subsequent syntax:

Example

dataType matrixName[rowSize][columnSize];

Here, dataType represents the data type of the elements within the matrix, while matrixName serves as the identifier for the matrix. rowSize indicates the total number of rows in the matrix, and columnSize specifies the total number of columns in the matrix. For instance, to generate a matrix containing integer elements with 3 rows and 4 columns, you can utilize the code snippet below:

Example

int matrix[3][4];

This establishes a matrix labeled matrix with 3 rows and 4 columns, setting each element to 0 as the default value. An alternative approach to constructing a matrix in C++ involves dynamically assigning memory using the new operator. This technique proves beneficial when the matrix size cannot be predetermined during compilation and must be ascertained during program execution. To dynamically generate a matrix, the initial step involves declaring a pointer pointing to a two-dimensional array with the subsequent syntax:

Example

dataType** matrixName;

In this context, dataType represents the data type of the elements within the matrix, while matrixName is the identifier assigned to the matrix pointer. Following this, memory allocation for the matrix rows is performed utilizing the new operator:

Example

matrixName = new dataType*[rowSize];

This establishes an array consisting of pointers to dataType arrays with a size equivalent to the number of rows, where each dataType array symbolizes a specific row within the matrix. Subsequently, we reserve memory for the columns of each row by employing an additional new operator:

Example

for(int i = 0; i < rowSize; i++) {

    matrixName[i] = new dataType[columnSize];

}

This generates an array of dataType with columnSize elements for every row within the matrix, essentially forming the complete matrix structure. For instance, to construct a matrix containing 3 rows and 4 columns of double elements through dynamic memory allocation, the code below can be employed:

Example

double** matrix;

int rowSize = 3;

int columnSize = 4;

matrix = new double*[rowSize];

for(int i = 0; i < rowSize; i++) {

    matrix[i] = new double[columnSize];

}

This generates a matrix called matrix consisting of 3 rows and 4 columns, with each value set to 0.0 as the default initialization. To summarize, in C++, matrices can be constructed using either two-dimensional arrays or by dynamically assigning memory. The selection between these approaches hinges on the matrix's dimensions and whether they are fixed during compilation or need to be calculated during program execution. Both techniques facilitate the effective organization and handling of substantial datasets, rendering them indispensable for various programming scenarios.

C++ Code

Example

#include <iostream>

using namespace std;

const int ROWS = 2;

const int COLS = 3;

void addMatrices(int mat1[][COLS], int mat2[][COLS], int result[][COLS]) {

    for(int i = 0; i < ROWS; i++) {

        for(int j = 0; j < COLS; j++) {

            result[i][j] = mat1[i][j] + mat2[i][j];

        }

    }

}

int main() {

    int matrix1[ROWS][COLS] = {{1, 2, 3}, {4, 5, 6}};

    int matrix2[ROWS][COLS] = {{7, 8, 9}, {10, 11, 12}};

    int result[ROWS][COLS];

    addMatrices(matrix1, matrix2, result);

    cout << "Matrix 1: " << endl;

    for(int i = 0; i < ROWS; i++) {

        for(int j = 0; j < COLS; j++) {

            cout << matrix1[i][j] << " ";

        }

        cout << endl;

    }

    cout << "Matrix 2: " << endl;

    for(int i = 0; i < ROWS; i++) {

        for(int j = 0; j < COLS; j++) {

            cout << matrix2[i][j] << " ";

        }

        cout << endl;

    }

    cout << "Result: " << endl;

    for(int i = 0; i < ROWS; i++) {

        for(int j = 0; j < COLS; j++) {

            cout << result[i][j] << " ";

        }

        cout << endl;

    }

    return 0;

}

Output

Output

Matrix 1:

1 2 3

4 5 6

Matrix 2:

7 8 9

10 11 12

Result:

8 10 12

14 16 18

Explanation:

In this script, the addMatrices procedure accepts a pair of matrices (mat1 and mat2) as arguments and computes their total on a per-element basis, saving the outcome in the result matrix. The matrices are provided to the procedure as two-dimensional arrays, with their sizes defined utilizing the const declaration.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience