Matrix Multiplication in C++

Muhammad Ahmer Siddique
3 min readOct 25, 2020

Library:

Library used for Input and Output stream

First of all, we include a library which is used for input and output, and this library is called “iostream”. Iostream means input and output stream.

Function:

Function

After using the library we use the main function in which we write the whole program.

Initialization:

Initialize the Variables

We initialize three arrays in which two arrays take as standard in the program or these arrays are also taken by the user. In the given program we take those arrays from the user. These are the arrays that are multiplied with each other then we need another array that is used to store the answer of those arrays which is multiplied. Then we use the third array initialize earlier. Then we initialize two variables that are used for rows and columns. Again they are also taken as standard in the program, but in this program, we take from the user. We take both matrices as standard or taken from the user. We take from the user in this program.

Store Values in the Variables:

Store values in the variables

After we enter the values of rows and columns from the user and store in the variables initialize earlier and then we take the values of two arrays from the user and store in the arrays initialize earlier.

Multiplication of Given Arrays:

Loop used to multiply two arrays and store in third array

After storing the values we use another nested loop that is responsible for matrix multiplication. In the first loop `for (i = 0; i < x; i++)` which holds matrix rows and the second loop for `(j = 0; j < y; j++)` which is responsible for matrix columns. In this loop, we assign the value zero to a third array which is used to store the answer given after the multiplication of the first two arrays. In the nested loop, we use the third loop `for (k = 0; k < x; k++)` which adds the multiplication of two numbers of arrays in the third array and also store in the third array until the loop end.

Print the Result:

Print the final result

In the end, we use another loop that is used to print the result and then terminates the program.

Conclusion:

First of all, we include a library which is responsible for inputs and outputs then we initialize variables and arrays which is used for matrix multiplication which include three arrays in which two arrays are multiplied and then stored in a third array then we use two variables which holds the values of rows and columns then we use nested loops that are responsible for matrix multiplication and then use the loop to print the result. This is the whole procedure to find multiplication of two matrices.

Output:

The output shown on the console after compiling the program is as follow,

Output of the above program

--

--