Multiplication of Two Matrices

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn how to find the multiplication of two matrices (2D arrays) in the C++ programming language. We will break it down into simple steps, so even new learners can follow without any difficulty.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/IOandFileHandlingGroup -.-> cpp/user_input("`User Input`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/BasicsGroup -.-> cpp/arrays("`Arrays`") subgraph Lab Skills cpp/output -.-> lab-96168{{"`Multiplication of Two Matrices`"}} cpp/comments -.-> lab-96168{{"`Multiplication of Two Matrices`"}} cpp/variables -.-> lab-96168{{"`Multiplication of Two Matrices`"}} cpp/user_input -.-> lab-96168{{"`Multiplication of Two Matrices`"}} cpp/data_types -.-> lab-96168{{"`Multiplication of Two Matrices`"}} cpp/operators -.-> lab-96168{{"`Multiplication of Two Matrices`"}} cpp/conditions -.-> lab-96168{{"`Multiplication of Two Matrices`"}} cpp/for_loop -.-> lab-96168{{"`Multiplication of Two Matrices`"}} cpp/arrays -.-> lab-96168{{"`Multiplication of Two Matrices`"}} end

Include necessary header files.

We will create a new file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

In the first step, we need to include the necessary header files.

#include <iostream>

We will use iostream file to take input and display output.

Define main() function

In the second step, we will declare the main function.

int main() {

Define Variables

In the third step, we need to declare variables to store the input matrices and the multiplication result.

int row1, col1, row2, col2, i, j, k;
int m1[10][10], m2[10][10], pro[10][10];

Here, row1 and col1 represents the number of rows and columns for the first matrix, respectively, and row2 and col2 represents the number of rows and columns for the second matrix, respectively. The m1, m2, and pro variables represent the two input matrices and the product matrix.

Get input from the user

In the fourth step, we will get the matrices from the user.

std::cout << "\nEnter the number of Rows and Columns of first matrix : ";
std::cin >> row1 >> col1;

std::cout << "\nEnter the number of Rows and Columns of second matrix : ";
std::cin >> row2 >> col2;

// input of first matrix
std::cout << "\nEnter the " << row1 * col1 << " elements of first matrix : \n";
for (i = 0; i < row1; i++) {
    for (j = 0; j < col1; j++) {
        std::cin >> m1[i][j];
    }
}

// input of second matrix
std::cout << "\nEnter the " << row2 * col2 << " elements of second matrix : \n";
for (i = 0; i < row2; i++) {
    for (j = 0; j < col2; j++) {
        std::cin >> m2[i][j];
    }
}

Here, we take input from the user for rows and columns of the two matrices, and then take input for each element of the matrices.

Multiply Matrices

In the fifth step, we will multiply the matrices.

if (col1 == row2) {
    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++) {
            pro[i][j] = 0;
            for (k = 0; k < col1; k++)
                pro[i][j] = pro[i][j] + (m1[i][k] * m2[k][j]);
        }
    }
}

Here, we first check whether the matrices are valid for multiplication. If they are, we perform the multiplication as explained in the introduction.

Display matrices

In the sixth step, we will display the matrices.

std::cout << "\nThe first matrix is : \n";
for (i = 0; i < row1; i++) {
    for (j = 0; j < col1; j++) {
        std::cout << m1[i][j] << "  ";
    }
    std::cout << std::endl;
}

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

std::cout << "\nThe Product matrix is : \n";
for (i = 0; i < row1; i++) {
    for (j = 0; j < col2; j++) {
        std ::cout << pro[i][j] << "  ";
    }
    std::cout << std::endl;
}

We first display the two matrices and then display the resulting matrix.

Compile and run the code

To compile the code, open a terminal and navigate to the directory where the code file is saved. Now, run the following command to compile the code:

g++ main.cpp -o main

After the code is compiled successfully, run the following command to execute the program:

./main

Summary

Congratulations! You have learned how to find the multiplication of two matrices in the C++ programming language. We learned how to check whether matrices are valid for multiplication, how to multiply matrices, and how to display the input and resulting matrices. We have tested the code in a Linux environment and learned how to compile and run the code.

Other C++ Tutorials you may like