Add Two Matrices

Beginner

Introduction

In this lab, we will write a C++ program to add two matrices using 2D arrays.

Create the C++ file

Create a C++ file named add_matrices.cpp inside the ~/project directory. This will be our main file where we write the code.

cd ~/project
touch add_matrices.cpp

Write the code

Copy the below code and paste it in the add_matrices.cpp file.

#include <iostream>

using namespace std;

int main() {
    cout << "\n\nWelcome to the Add Matrices Program\n\n\n";

    // Initialize variables
    int row, col, i, j;

    // Declare the three matrices (2D arrays)
    int m1[10][10], m2[10][10], sum[10][10];

    // Get the number of rows and columns of matrix from user
    cout << "\nEnter the number of Rows and Columns of matrix : ";
    cin >> row >> col;

    // Get the elements of the first matrix from user
    cout << "\nEnter the " << row * col << " elements of first matrix : \n";
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            cin >> m1[i][j];
        }
    }

    // Get the elements of the second matrix from user
    cout << "\nEnter the " << row * col << " elements of second matrix : \n";
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            cin >> m2[i][j];
        }
    }

    // Calculate the sum matrix
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            sum[i][j] = m1[i][j] + m2[i][j];
        }
    }

    // Display the matrices
    cout << "\n\nThe first matrix is : \n";
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            cout << m1[i][j] << "  ";
        }
        cout << endl;
    }

    cout << "\n\nThe second matrix is : \n";
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            cout << m2[i][j] << "  ";
        }
        cout << endl;
    }

    cout << "\n\nThe Sum matrix is : \n";
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            cout << sum[i][j] << "  ";
        }
        cout << endl;
    }

    return 0;
}

Compile and Run the code

To compile the code, open the terminal in the ~/project directory, and run the following command:

g++ add_matrices.cpp -o add_matrices

To run the program, type the following command in the terminal:

./add_matrices

This will run the program and output the result of adding two matrices.

Verify the output

After running the program, you should see the output like below:

Welcome to the Add Matrices Program

Enter the number of Rows and Columns of matrix : 2 2

Enter the 4 elements of first matrix :
1 2 3 4

Enter the 4 elements of second matrix :
5 6 7 8


The first matrix is :
1  2
3  4


The second matrix is :
5  6
7  8


The Sum matrix is :
6  8
10  12

Summary

In this lab, we have learned how to add two matrices using 2D arrays in C++ programming language. We have created a program that takes two matrices as input, adds the corresponding elements of the two matrices, and outputs the sum matrix.

Other Tutorials you may like