MATRIX MULTIPLICATION Program

MATRIX MULTIPLICATION Program

21 April 2023

21 April 2023

MATRIX MULTIPLICATION



If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Sample input 1:

3 3

1 2 3

4 5 6

7 8 9

3 2

1 0

0 1

0 0

Sample output 1:

1 2

4 5

7 8

Sample input 2:

3 3

1 2 3

4 5 6

7 8 9

2 3

1 0 1

1 1 0

Sample Output:

Multiplication not possible

C Program

#include <stdio.h>

 

int main()

{

            int r1,c1,r2,c2;

            scanf("%d%d",&r1,&c1);

            int arr1[r1][c1];

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

            {

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

                        {

                                    scanf("%d",&arr1[i][j]) ;

                        }

            }

            scanf("%d%d",&r2,&c2);

            int arr2[r2][c2];

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

            {

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

                        {

                                    scanf("%d",&arr2[i][j]) ;

                        }

            }

            // Matrix multiplication is possible only if Row of 1st matrix equals to colum of 2ed matrix

            // Product sum matrix will be of diamention Row1 x Col2

           

            if(c1 == r2)

            {

                        int arr3[r1][c2];

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

                        {

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

                                    {

                                                arr3[i][j] = 0;

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

                                                {

                                                            arr3[i][j] = arr3[i][j] + (arr1[i][k] * arr2[k][j]);

                                                }

                                    }

                        }

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

                        {

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

                                    {

                                                printf("%d ",arr3[i][j]);

                                    }

                                    printf("\n");

                        }

            }

            else

            {

                        printf("Multiplication not possible");

            }

            return 0;

}

 

C++ program

#include <bits/stdc++.h>

using namespace std;

int main()

{

            int r1,c1,r2,c2;

            cin>>r1>>c1;

            int arr1[r1][c1];

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

            {

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

                        {

                                    cin>>arr1[i][j];

                        }

            }

            cin>>r2>>c2;

            int arr2[r2][c2];

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

            {

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

                        {

                                    cin>>arr2[i][j];

                        }

            }

            // Matrix multiplication is possible only if Row of 1st matrix equals to colum of 2ed matrix

            // Product sum matrix will be of diamention Row1 x Col2

           

            if(c1 == r2)

            {

                        int arr3[r1][c2];

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

                        {

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

                                    {

                                                arr3[i][j] = 0;

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

                                                {

                                                            arr3[i][j] = arr3[i][j] + (arr1[i][k] * arr2[k][j]);

                                                }

                                    }

                        }

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

                        {

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

                                    {

                                                cout<<arr3[i][j]<<" ";

                                    }

                                    cout<<"\n";

                        }

            }

            else

            {

                        cout<<"Multiplication not possible";

            }

            return 0;

}

 

JAVA Program

import java.util.*;

import java.lang.*;

import java.io.*;

 

class Main

{

            public static void Multiplication(int arr1[][],int arr2[][],int r1,int c1,int r2,int c2)

            {

                // Matrix multiplication is possible only if Row of 1st matrix equals to colum of 2ed matrix

            // Product sum matrix will be of diamention Row1 x Col2

                        if(c1 == r2)

                        {

                                    int arr3[][] = new int[r1][c2];

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

                                    {

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

                                                {

                                                            arr3[i][j] = 0;

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

                                                            {

                                                                        arr3[i][j] = arr3[i][j] + (arr1[i][k] * arr2[k][j]);

                                                            }

                                                }

                                    }

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

                                    {

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

                                                {

                                                            System.out.print(arr3[i][j] + " ");

                                                }

                                                System.out.println();

                                    }

                        }

                        else

                        {

                                    System.out.print("Multiplication not possible");

                        }

            }

 

            public static void main (String[] args) throws java.lang.Exception

            {

                        Scanner sc=new Scanner(System.in);

                        int r1 = sc.nextInt();

                        int c1 = sc.nextInt();

                        int arr1[][] = new int[r1][c1];

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

                        {

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

                                    {

                                                arr1[i][j] = sc.nextInt();

                                    }

                        }

                        int r2 = sc.nextInt();

                        int c2 = sc.nextInt();

                        int arr2[][] = new int[r2][c2];

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

                        {

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

                                    {

                                                arr2[i][j] = sc.nextInt();

                                    }

                        }

                        Multiplication(arr1,arr2,r1,c1,r2,c2);

            }

}

 

PYTHON Program

a1=[[1, 2, 3],

    [4, 5, 6]]

 

a2=[[1, 1],

    [2, 2],

    [3, 3]]

 

resultant = []

 

for i in range(len(a1)):

    row = []

    for j in range(len(a2[0])):

        row.append(0)

    resultant.append(row)

 

for i in range(len(a1)):

    for j in range(len(a2[0])):

        for k in range(len(a2)) :

            resultant[i][j] += a1[i][k] * a2[k][j]

 

for row in resultant:

    print(row)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023


Related Articles

Ask Us Anything !