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.
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.
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.
In the second step, we will declare the main function.
int main() {
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.
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.
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.
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.
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
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.