Python and C++ program to implement multiplication of 2d array (Matrix multiplication)

Here, in this program we are going to implement matrix multiplication ,
suppose matrix 1 has dimensions:m*n
and matrix 2 has dimensions :p*q
for these two matrix to be multiplied we need to have the number of columns in matrix 1(n) equal to the number of rows in matrix 2(p),
if the condition is satisfied then the result of multiplication will be a matrix of order m*q



in multiplication the elements of the resultant matrix will be the sum of product of corresponding elements of row(of M1) and column(of M2)
//first element in 1st row in given example:
res[0][0]+=mat1[0][i] * mat2[i][0] where 0<i<n or p
//second element in 1st row:
res[0][1]+=mat1[0][i] * mat2[i][1] where 0<i<n or p
and so on…………

Example :
Input :
1 2 3   1
4 5 6   2
7 8 9   3 
Output :
14
32
50


Input :
4 3 4   2
4 1 1   1
Output :
multiplication not possible
n!=p


Here goes the main execution part where the calculation is been done:

for(int i=0;i<r1;i++)
   for(int j=0;j<c2;j++)
      for(int l=0;l<r2;l++)
           *(res+i*c2+j)+=*(arr1+i*c1+l) * *(arr2+l*c2+j);

the last statement can also be written as:
res[i][j] + = arr1[i][l] + arr2[l][j];

we are using dynamic memory allocation in this program, using calloc() , in the above code each element of res[] array is calculated by continuously adding the product of columns of arr1  (increasing ) and rows of arr2 ( increasing ).
The source code is given below in Python and C++ :

Python Source Code:


# 2D array multiplication
if __name__ == '__main__':
    r1, c1, r2, c2 = map(int, input("rows and column for matrix 1 and 2:").rstrip().split())
    if c1 != r2:
        print("Multiplication not possible:")
        exit(0)

    print("Enter elements:")
    arr1 = []
    arr2 = []
    for i in range(r1):
        arr1.append(list(map(int, input().rstrip().split())))
    for i in range(r2):
        arr2.append(list(map(int, input().strip().split())))
    res = [[0 for c in range(c2)] for r in range(r1)]
    for i in range(r1):
        for j in range(c2):
            for l in range(r2):
                res[i][j] = res[i][j] + arr1[i][l] * arr2[l][j]
    print(*res, sep='\n')


C++ Source Code:

#include <iostream>
#include<conio.h>
using namespace std;

int main(int argc, char *argv[]) {
 int r1,c1,r2,c2;
 printf("Enter row and column of matrix 1 and 2\n");
 cin>>r1>>c1>>r2>>c2;
 int *arr1=(int*)calloc(r1*c1,sizeof(int));
 int *arr2=(int*)calloc(r2*c2,sizeof(int));
 int *res=(int*)calloc(r1*c2,sizeof(int));
 
 for(int i=0;i<r1;i++)
  for(int j=0;j<c1;j++)
   cin>>*(arr1+c1*i+j);
 for(int i=0;i<r2;i++)
  for(int j=0;j<c2;j++)
   cin>>*(arr2+c2*i+j);
 for(int i=0;i<r1;i++)
  for(int j=0;j<c2;j++)
   for(int l=0;l<r2;l++)
    *(res+i*c2+j)+=*(arr1+i*c1+l) * *(arr2+l*c2+j);
 for(int i=0;i<r1;i++)
 {
  for(int j=0;j<c2;j++)
   cout<<*(res+i*c2+j);
  cout<<endl;
 }
 getch();
 return 0;
}


For downloading executable Code:  click here


if any doubts or queries please comment 🙂

Comments

Post a Comment

Popular posts from this blog

What is AI (Artificial Intelligence )? and its characteristics

How to find and replace a node in Linked List