Software & Finance





C++ - Matrix Multiplication With out using Classes





This would be the simple version of the matrix multiplication written in one function. It does not have any classes or any other operator overloading mechanism. For detailed version, Click here

 


Source Code


 

#include <stdio.h>
#include <iostream> 

int main()

{   

    int row1, col1, row2, col2;

    double matrixA[24][24];

    double matrixB[24][24];

    double matrixC[24][24];

 

    // Init Array

    for(int i = 0; i < 24; i++)

    {

        for(int j = 0; j < 24; j++)

        {

            matrixA[i][j] = 0;

            matrixB[i][j] = 0;

            matrixC[i][j] = 0;

        }

    }

 

 

    // Input the rows and cols of the matrix A and B

    std::cout << "Enter rows and columns of the Matrix A: ";

    std::cin >> row1;

    std::cin >> col1;

    std::cout << "Enter rows and columns of the Matrix B: ";

    std::cin >> row2;

    std::cin >> col2;

   

    if( col1 != row2)

    {

        std::cout << "Multiplication could not take place because number of columns of 1st Matrix and number of rows in 2nd Matrix are different";

        return -1;

    }

 

    // Input for Matrix A

    {

        for(int i = 0; i < row1; i++)

        {

            for(int j = 0; j < col1; j++)

            {

                std::cout << "Input For Matrix A Row: " << i + 1 << " Col: " << j + 1 << " = ";

                std::cin >> matrixA[i][j];

            }

            std::cout << "\n";

        }

        std::cout << "\n";

    }

 

    // Input for Matrix B

    {

        for(int i = 0; i < row2; i++)

        {

            for(int j = 0; j < col2; j++)

            {

                std::cout << "Input For Matrix B Row: " << i + 1 << " Col: " << j + 1 << " = ";

                std::cin >> matrixB[i][j];

            }

            std::cout << "\n";

        }

        std::cout << "\n";

    }

 

    // Matrix C = A * B

    {

        for(int i = 0; i < row1; i++)

            for(int j = 0; j < col2; j++)

                for(int k = 0; k < col1; k++)

                    matrixC[i][j] += matrixA[i][k] * matrixB[k][j];

    }

 

 

    // Display Matrix C = A * B

    {

        std::cout << "\n\nMatrix C = A * B : " << " Rows: " << row1 << " Cols: " << col2 << "\n\n";

        for(int i = 0; i < row1; i++)

        {

            std::cout << " | ";

            for(int j = 0; j < col2; j++)

                std::cout << matrixC[i][j] << " ";

            std::cout <<  "|\n";

        }

        std::cout << "\n\n";

    }

    return 0;

}

Output


Enter rows and columns of the Matrix A: 2

3

Enter rows and columns of the Matrix B: 3

4

Input For Matrix A Row: 1 Col: 1 = 1

Input For Matrix A Row: 1 Col: 2 = 2

Input For Matrix A Row: 1 Col: 3 = 3

 

Input For Matrix A Row: 2 Col: 1 = 1

Input For Matrix A Row: 2 Col: 2 = 2

Input For Matrix A Row: 2 Col: 3 = 3

 

 

Input For Matrix B Row: 1 Col: 1 = 1

Input For Matrix B Row: 1 Col: 2 = 2

Input For Matrix B Row: 1 Col: 3 = 3

Input For Matrix B Row: 1 Col: 4 = 4

 

Input For Matrix B Row: 2 Col: 1 = 1

Input For Matrix B Row: 2 Col: 2 = 2

Input For Matrix B Row: 2 Col: 3 = 3

Input For Matrix B Row: 2 Col: 4 = 4

 

Input For Matrix B Row: 3 Col: 1 = 1

Input For Matrix B Row: 3 Col: 2 = 2

Input For Matrix B Row: 3 Col: 3 = 3

Input For Matrix B Row: 3 Col: 4 = 4

 

 

 

 

Matrix C = A * B :  Rows: 2 Cols: 4

 

 | 6 12 18 24 |

 | 6 12 18 24 |

 

 

Press any key to continue . . .