Multiplying Matrices With User-Defined Functions in C++

Exercise:

Write a C++ Program that multiplies two matrices and prints it on the screen with a user-defined function.

Click Here to View the Solution:
#include <iostream>
using namespace std;
void Mult(int mx1[][10], int mx2[][10], int ans[][10], int row1, int col1, int row2, int col2);
void Data(int mx1[][10], int mx2[][10], int row1, int col1, int row2, int col2);
void View(int mult[][10], int row1, int col2);
int main()
{
    int mx1[10][10], mx2[10][10], mult[10][10], row1, col1, row2, col2, i, j, k;
    cout << "Insert row and column of matrix 1:\n";
    cin >> row1 >> col1;
    cout << "Insert row and column of matrix 2:\n";
    cin >> row2 >> col2;
    while (col1 != row2)         //CHECK! column of matrix 1 must be equal to row of matrix 2
    {
        cout << "ERROR! column of matrix 1 not equal to row of matrix 2" << endl;
        cout << "Insert row and column of matrix 1 again:\n";
        cin >> row1 >> col1;
        cout << "Insert row and column of matrix 2 again:\n";
        cin >> row2 >> col2;
    }
    // Function to take data
    Data(mx1, mx2, row1, col1, row2, col2);
    // Function to multiply.
    Mult(mx1, mx2, mult, row1, col1, row2, col2);
    // Function to View
    View(mult, row1, col2);
    return 0;
}
void Data(int mx1[][10], int mx2[][10], int row1, int col1, int row2, int col2)
{
    cout << endl << "Insert values of matrix 1:" << endl;
    for(int x = 0; x < row1; ++x)
    {
        for(int y = 0; y < col1; ++y)
        {
            cout << "position "<< x + 1 << y + 1 << ": ";
            cin >> mx1[x][y];
        }
    }
    cout << endl << "Insert values of matrix 2:" << endl;
    for(int x = 0; x < row2; ++x)
    {
        for(int y = 0; y < col2; ++y)
        {
            cout << "position " << x + 1 << y + 1 << ": ";
            cin >> mx2[x][y];
        }
    }
}
void Mult(int mx1[][10], int mx2[][10], int mult[][10], int row1, int col1, int row2, int col2)
{
    int x, y, z;
    // initializing mult[][] to 0.
    for(x = 0; x < row1; ++x)
    {
        for(y = 0; y < col2; ++y)
        {
            mult[x][y] = 0;
        }
    }
    // Multiplying and storing in mult[][].
    for(x = 0; x < row1; ++x)
    {
        for(y = 0; y < col2; ++y)
        {
            for(z=0; z<col1; ++z)
            {
                mult[x][y] += mx1[x][z] * mx2[z][y];
            }
        }
    }
}
void View(int mult[][10], int row1, int col2)
{
    cout << "\nMultiplication of Matrix:" << endl;
    for(int x = 0; x < row1; ++x)
    {
        for(int y = 0; y < col2; ++y)
        {
            cout << mult[x][y] << " ";
            if(y == col2 - 1)
            cout << endl;
        }
    }
}
Click Here to View the Output:
Insert row and column of matrix 1:
 2
 2
 Insert row and column of matrix 2:
 2
 2

 Insert values of matrix 1:
 position 11: 2
 position 12: 4
 position 21: 3
 position 22: 5

 Insert values of matrix 2:
 position 11: 6
 position 12: 1
 position 21: 8
 position 22: 7

 Multiplication of Matrix:
 44 30
 58 38
Click Here to View the Explanation:
  • To begin the program, it must be ensured that both the matrices can be multiplied. To pass two matrices for multiplication, the column number of first matrix should be equal to the number of rows of the second matrix.
  • The user is requested to enter the sizes of the two matrices by the entering the number of columns and the number of rows for both matrices.
  • A function is created to multiply two matrices.
  • The function is used and the product of both matrices is saved and displayed on the screen.
%d bloggers like this: