Q:

C++ Program to Multiply Two Matrices using array

belongs to collection: C++ Arrays Solved Programs

0

Write a C++ Program to Multiply Two Matrices using array. Here’s simple Program to Multiply Two Matrices using array in C++ Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What is Matrix ?


Matrix representation is a method used by a computer language to store matrices of more than one dimension in memory. C uses “Row Major”, which stores all the elements for a given row contiguously in memory.

 
 

Two-dimensional Arrays : :

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. An m × n (read as m by n) order matrix is a set of numbers arranged in m rows and n columns.

To declare a two-dimensional integer array of size [x][y], you would write something as follows −

 
  • type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier.


To multiply two matrices in C++ programming, you have to ask to the user to enter the first and second matrix elements.Now start multiplying the two matrices and store the multiplication result inside any variable say sumand finally store the value of sum in the third matrix say mat3[ ][ ]

Below is the source code for C++ Program to Multiply Two Matrices using array which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C++ Program to Multiply Two Matrices using array  */

#include<iostream>
using namespace std;

int main()
{

        int arr1[5][5], arr2[5][5], arr3[5][5], sum=0, i, j, k,m,n;

        cout<<"Enter size of matrix ( Max:5 ) :: ";
    cin>>m;
    cout<<"\nEnter Elements to Matrix A Below :: \n";

    for(i=0;i<m;i++)
    {
        for(j=0;j<m;++j)
        {
            cout<<"\nEnter arr1["<<i<<"]["<<j<<"] Element :: ";
            cin>>arr1[i][j];
        }

    }

    cout<<"\nEnter Elements to Matrix B Below :: \n";

    for(i=0;i<m;i++)
    {
        for(j=0;j<m;++j)
        {
            cout<<"\nEnter arr2["<<i<<"]["<<j<<"] Element :: ";
            cin>>arr2[i][j];
        }

    }

        cout<<"\nMultiplying two matrices.....\n";
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        sum=0;
                        for(k=0; k<3; k++)
                        {
                                sum = sum + arr1[i][k] * arr2[k][j];
                        }
                        arr3[i][j] = sum;
                }
        }
        cout<<"\nMultiplication of two Matrices : \n\n";
        for (i = 0; i < m; ++i)
        {
            for (j = 0; j < m; ++j)
            {
                cout<<"\t"<<arr3[i][j];
            }
            printf("\n\n");
        }

        return 0;
}

OUTPUT : :


/*  C++ Program to Multiply Two Matrices using array  */

Enter size of matrix ( Max:5 ) :: 3

Enter Elements to Matrix A Below ::

Enter arr1[0][0] Element :: 1

Enter arr1[0][1] Element :: 2

Enter arr1[0][2] Element :: 3

Enter arr1[1][0] Element :: 4

Enter arr1[1][1] Element :: 5

Enter arr1[1][2] Element :: 6

Enter arr1[2][0] Element :: 7

Enter arr1[2][1] Element :: 8

Enter arr1[2][2] Element :: 9

Enter Elements to Matrix B Below ::

Enter arr2[0][0] Element :: 1

Enter arr2[0][1] Element :: 2

Enter arr2[0][2] Element :: 3

Enter arr2[1][0] Element :: 4

Enter arr2[1][1] Element :: 5

Enter arr2[1][2] Element :: 6

Enter arr2[2][0] Element :: 7

Enter arr2[2][1] Element :: 8

Enter arr2[2][2] Element :: 9

Multiplying two matrices.....

Multiplication of two Matrices :

        30      36      42

        66      81      96

        102     126     150


Process returned 0

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Arrays Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Program to Find Transpose of a Matrix using ar... >>
<< C++ Program to Accessing Elements of an Array Usin...