C++ Program to Perform Matrix Addition

C++ Program to Perform Matrix Addition

Matrix operations are a fundamental part of mathematics and computer science. One of the simplest yet essential operations is matrix addition, where two matrices of the same size are added element by element to produce a new matrix. Learning how to perform matrix addition in C++ is an excellent way for beginners to practice nested loops, array handling, and basic input/output. This concept is widely used in areas such as graphics, engineering computations, and scientific programming, making it a practical and foundational skill.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Matrix Addition Using User Input

This program allows the user to enter the elements of two matrices and then calculates their sum. It is ideal for beginners to understand how nested loops and arrays work together in C++.

#include <iostream>

using namespace std;

int main() {

    int rows, cols;

    cout << "Enter the number of rows: " << endl;
    cin >> rows;

    cout << "Enter the number of columns: " << endl;
    cin >> cols;

    int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];

    cout << "Enter elements of first matrix:" << endl;

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

        for(int j = 0; j < cols; j++) {
            cin >> matrix1[i][j];
        }

    }

    cout << "Enter elements of second matrix:" << endl;

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

        for(int j = 0; j < cols; j++) {
            cin >> matrix2[i][j];
        }

    }

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

        for(int j = 0; j < cols; j++) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }

    }

    cout << "Sum of the matrices:" << endl;

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

        for(int j = 0; j < cols; j++) {
            cout << sum[i][j] << " ";
        }

        cout << endl;

    }

    return 0;

}

In this program, the user provides the size and elements of two matrices. Nested for loops iterate through rows and columns to add corresponding elements. The resulting sum matrix is displayed, demonstrating how arrays and loops work together to handle structured data. Beginners gain hands-on experience with array indexing, nested loops, and element-wise operations.

Program 2: Matrix Addition with Predefined Matrices

Sometimes, you may want to work with predefined matrices instead of taking user input. This program demonstrates matrix addition using predefined values.

#include <iostream>

using namespace std;

int main() {

    int matrix1[2][2] {{1, 2}, {3, 4}};
    int matrix2[2][2] {{5, 6}, {7, 8}};
    int sum[2][2];

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

        for(int j = 0; j < 2; j++) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }

    }

    cout << "Sum of the predefined matrices:" << endl;

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

        for(int j = 0; j < 2; j++) {
            cout << sum[i][j] << " ";
        }

        cout << endl;

    }

    return 0;

}

In this program, the matrices are already defined in the code. The nested loops add each element and store it in a new matrix. Beginners can quickly verify the result without entering values manually. It’s useful for testing logic and learning how array indexing works without the extra step of input handling.

Program 3: Matrix Addition for Larger Predefined Matrices

For more advanced practice, you can work with larger matrices. This program demonstrates addition of two 3×3 matrices using predefined values.

#include <iostream>

using namespace std;

int main() {

    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 sum[3][3];

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

        for(int j = 0; j < 3; j++) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }

    }

    cout << "Sum of the 3x3 matrices:" << endl;

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

        for(int j = 0; j < 3; j++) {
            cout << sum[i][j] << " ";
        }

        cout << endl;

    }

    return 0;

}

This program shows how matrix addition scales to larger matrices. Beginners can observe the pattern in the loops and understand how nested iterations handle multiple rows and columns. It also demonstrates how C++ arrays can manage structured data efficiently.

Program 4: Colorful Matrix Display

This program demonstrates how to add two matrices and display the matrices and their sum in different colors using ANSI escape codes. It’s a fun way for beginners to learn loops, arrays, and output formatting.

#include <iostream>

using namespace std;

int main() {

    int matrix1[2][2] {{1, 2}, {3, 4}};
    int matrix2[2][2] {{5, 6}, {7, 8}};
    int sum[2][2];

    // Calculate sum of matrices
    for(int i = 0; i < 2; i++) {

        for(int j = 0; j < 2; j++) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }

    }

    // Display first matrix in blue
    cout << "\033[1;34mMatrix 1:\033[0m" << endl;

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

        for(int j = 0; j < 2; j++) {
            cout << "\033[1;34m" << matrix1[i][j] << " \033[0m";
        }

        cout << endl;

    }

    // Display second matrix in green
    cout << "\033[1;32mMatrix 2:\033[0m" << endl;

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

        for(int j = 0; j < 2; j++) {
            cout << "\033[1;32m" << matrix2[i][j] << " \033[0m";
        }

        cout << endl;

    }

    // Display sum matrix in yellow
    cout << "\033[1;33mSum of Matrices:\033[0m" << endl;

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

        for(int j = 0; j < 2; j++) {
            cout << "\033[1;33m" << sum[i][j] << " \033[0m";
        }

        cout << endl;

    }

    return 0;

}

In this program, the two predefined 2×2 matrices are added element by element using nested loops. The ANSI escape codes like \033[1;34m and \033[1;32m change the color of the text displayed on the console. The first matrix appears in blue, the second in green, and the sum in yellow. This visual distinction makes it easier for beginners to track which values belong to which matrix, helping them understand the addition process more clearly.

Frequently Asked Questions (FAQ)

Understanding matrix addition in C++ often raises some common questions. Here are the answers in simple terms.

Q1: What is matrix addition?
A1: Matrix addition is the process of adding two matrices element by element. Both matrices must have the same number of rows and columns.

Q2: Can matrices of different sizes be added?
A2: No. For addition, both matrices must be of the same size. Otherwise, the operation is undefined.

Q3: How are matrices stored in C++?
A3: Matrices are stored as two-dimensional arrays, where each row contains a one-dimensional array of elements.

Q4: Why are nested loops used for matrix addition?
A4: Nested loops allow us to traverse rows and columns. The outer loop goes through each row, while the inner loop goes through each column.

Q5: Can predefined matrices be useful for learning?
A5: Yes. Predefined matrices allow beginners to focus on logic without worrying about user input. They are also helpful for testing and debugging code.

Conclusion

Matrix addition in C++ is a fundamental skill that helps beginners understand arrays, nested loops, and element-wise operations. By practicing with both user input and predefined matrices, you can strengthen your understanding of structured data and loop logic. Keep experimenting with larger matrices, different sizes, and variations in your programs to enhance your coding skills and problem-solving ability.

Additional & References

Beginners are encouraged to explore other matrix operations, such as subtraction, multiplication, and transpose, to deepen their understanding of arrays and nested loops. Practicing these operations will prepare you for more advanced programming challenges.

Scroll to Top