2d matrix multiplication

Solutions on MaxInterview for 2d matrix multiplication by the best coders in the world

showing results for - "2d matrix multiplication"
Constance
23 Apr 2016
1double[][] c = new double[N][N];
2for (int i = 0; i < N; i++)
3{
4 for (int j = 0; j < N; j++)
5 	{
6 for (int k = 0; k < N; k++)
7 		{
8		 c[i][j] += a[i][k] * b[k][j];
9 		}
10 	}
11}
12
Anaelle
02 Nov 2017
1#include <stdio.h>
2
3// function to get matrix elements entered by the user
4void getMatrixElements(int matrix[][10], int row, int column) {
5
6   printf("\nEnter elements: \n");
7
8   for (int i = 0; i < row; ++i) {
9      for (int j = 0; j < column; ++j) {
10         printf("Enter a%d%d: ", i + 1, j + 1);
11         scanf("%d", &matrix[i][j]);
12      }
13   }
14}
15
16// function to multiply two matrices
17void multiplyMatrices(int first[][10],
18                      int second[][10],
19                      int result[][10],
20                      int r1, int c1, int r2, int c2) {
21
22   // Initializing elements of matrix mult to 0.
23   for (int i = 0; i < r1; ++i) {
24      for (int j = 0; j < c2; ++j) {
25         result[i][j] = 0;
26      }
27   }
28
29   // Multiplying first and second matrices and storing it in result
30   for (int i = 0; i < r1; ++i) {
31      for (int j = 0; j < c2; ++j) {
32         for (int k = 0; k < c1; ++k) {
33            result[i][j] += first[i][k] * second[k][j];
34         }
35      }
36   }
37}
38
39// function to display the matrix
40void display(int result[][10], int row, int column) {
41
42   printf("\nOutput Matrix:\n");
43   for (int i = 0; i < row; ++i) {
44      for (int j = 0; j < column; ++j) {
45         printf("%d  ", result[i][j]);
46         if (j == column - 1)
47            printf("\n");
48      }
49   }
50}
51
52int main() {
53   int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
54   printf("Enter rows and column for the first matrix: ");
55   scanf("%d %d", &r1, &c1);
56   printf("Enter rows and column for the second matrix: ");
57   scanf("%d %d", &r2, &c2);
58
59   // Taking input until
60   // 1st matrix columns is not equal to 2nd matrix row
61   while (c1 != r2) {
62      printf("Error! Enter rows and columns again.\n");
63      printf("Enter rows and columns for the first matrix: ");
64      scanf("%d%d", &r1, &c1);
65      printf("Enter rows and columns for the second matrix: ");
66      scanf("%d%d", &r2, &c2);
67   }
68
69   // get elements of the first matrix
70   getMatrixElements(first, r1, c1);
71
72   // get elements of the second matrix
73   getMatrixElements(second, r2, c2);
74
75   // multiply two matrices.
76   multiplyMatrices(first, second, result, r1, c1, r2, c2);
77
78   // display the result
79   display(result, r1, c2);
80
81   return 0;
82}
Ivanna
21 Aug 2020
1import java.util.Arrays;
2import java.util.Scanner;
3public class Matrix {
4
5    private static int counter =0;
6    private static Scanner scanner = new Scanner(System.in);
7    private int rawForArr1 =0 , columnForArr1=0, rawForArr2=0, ColumnForArr2=0;
8
9    public Matrix(){
10    }
11
12    public Matrix(int rawForArr1, int columnForArr1, int rawForArr2, int columnForArr2) {
13        this.rawForArr1 = rawForArr1;
14        this.columnForArr1 = columnForArr1;
15        this.rawForArr2 = rawForArr2;
16        ColumnForArr2 = columnForArr2;
17    }
18
19    public void sizeOfRawsAndColumns(int r1, int c1, int r2, int c2){
20        System.out.println("Enter numbers of raws for array#1");
21        r1=scanner.nextInt();
22        this.rawForArr1 =r1;
23        System.out.println("Enter numbers of columns for array#1");
24        c1=scanner.nextInt();
25        this.columnForArr1=c1;
26        System.out.println("Enter numbers of raws for array#2");
27        r2=scanner.nextInt();
28        this.rawForArr2 = r2;
29        System.out.println("Enter numbers of columns for array#2");
30        c2=scanner.nextInt();
31        this.ColumnForArr2 = c2;
32        checkIfMatrixIsValid(c1,r2,r1,c2);
33    }
34    public static void printValue(int[][] arr)
35    {
36                System.out.print(Arrays.deepToString(arr));
37    }
38
39    public static void checkIfMatrixIsValid(int c1,int r2,int r1, int c2)
40    {
41        while(c1 != r2)
42        {
43            System.out.println("The columns in the left array is not equal to the raw's in the right"+"\n"+
44                    "array enter other values"+"\n"+
45                    "enter a value and it will be assigned for valid Matrix");
46            Scanner scanner = new Scanner(System.in);
47            c1 = scanner.nextInt();
48            r2 = c1;
49        }
50        int [][] firstArr = initilizeMatrix(r1,c1);
51        int [][] secondArr = initilizeMatrix(r2,c2);
52        EnterElements(firstArr);
53        EnterElements(secondArr);
54        int[][] multplied =multiplieMatrix(firstArr,secondArr);
55        printValue(multplied);
56
57
58
59    }
60
61    public static int [][] initilizeMatrix(int n , int m)
62    {
63
64        int [][] newMatrix = new int[m][n];
65        return  newMatrix;
66
67    }
68
69    public static int [][] multiplieMatrix(int [][] leftArr,int[][] rightArr){
70        int m = leftArr[0].length;
71        int n = rightArr.length;
72        int [][] sumOfMultiplie = new int[m][n];
73        int i=0,j=0,k=0;
74        int sum = 0 ;
75        for( i = 0 ; i < leftArr.length ; ++i)
76        {
77            for(j = 0 ; j < rightArr.length ; ++j)
78            {
79                for(k = 0 ; k < rightArr[0].length ; ++k)
80                {
81                    sum+= leftArr[i][k]*rightArr[k][j];
82                }
83                sumOfMultiplie[i][j]=sum ;
84                sum=0;
85            }
86        }
87        return sumOfMultiplie ;
88    }
89
90    public static int[][] EnterElements(int[][] arr)
91    {
92
93        ++counter;
94        if(counter==1){
95            System.out.println("Enter the elements for the left array"+'\n');
96        }else{
97            System.out.println("Enter the elements for the right array");
98        }
99
100        for(int i = 0 ; i < arr.length ; ++i)
101        {
102            for(int j = 0 ; j<arr.length ; ++j)
103            {
104                System.out.println("Enter element in raw #"+(i+1)+" column #"+(j+1));
105                arr[i][j] = scanner.nextInt();
106            }
107        }
108        return arr ;
109
110    }
111
112
113
114}
queries leading to this page
multiplication of 2d matrix in cmultiplication of two matrices in cmatrix multiplication using function in cmultiply matrices c programsproduct of matrix in cwrite a c program for matrix multiplication using operator overloadingmultiplication of 2d arraymultiplication of two matrices codeperform matrix multiplication using carray multiplication in cc metric multiplacationmultiplication of 22three 22 matrices in cmultiplication of 2 matrices codemultiply two matrices in clogic of matrix multiplication in chow to do matrix multiplication in cprogram of multiply two matrices in cmultiplying two atrix programmultiplication of square matrix in cc program multiplication of two matrixwrite a c program to find the matrix multiplication of two matrices matrix multiplication programmatriz multiplication cmatrix multiplication algorithm in cmatrix multiply in cmatrix multiplication logicblock multiplication of matrices program in cmatrix multiplication codematrix multiplication code in cmatrix multiplication in c using arrayc program for multiplying two matricesmatrix multiplication data structurec program to find matrix multiplication using arrays2d array multiplication in cmatrix multiplication 2d array c 2b 2bc program to multiply 5 matricesmultiply matrix cwrite a program to multiply 2 matrices in cmultiplying 2d arraymatrix multiplication program in cwrite a c program multiplication of two matrixc program for matrix multiplicationproduct of 2d array in cmultiplication of matrix cmatrix multiplication gives 2d arraymultiply two matrix in cc code of matrix multiplymultiplying matrices in cmultiplication of m 2an matrices in cmatrix multiplication using carray multiplication ccode for matrx multiplication 2dhow to code matrix multiplication in c with a matrixcode for matrix multiplicationmultiplying any 2 matrices in c codematrix multiplication c codematrix multiplicationin cmatrix multiplication in c logicmultiplying matrix in cc program to multiply two matricesmatrix multiplication c programc program multiply matrixaddition of two matrix in c program2d matrixprogram for product of two matricesmatrix multiplication in c using functionmultiplication of any two matrix in cmatrix multiplication in array cwrite a c program for multiplication of two matrices c matrix multiplymultiply two matrix cmatrix multiplication cmatrix multiplication in c 2d arraywrite a c program to implement three matrices multiplicationmultiply matrix in cmultiplication of matrix 2 2a2 code in c3 dimensional matrix multiplicationc matrix mathhow to multiply matrix in cmatrix in c programmingmultiplying two matrices in ccalculate product of 2 matrix in cprogram for matrix multiplicationwrite a program to implement matrix multiplication 2d array multiplication cmatrix multiplication in c with different dimensionsmultiplication of a matrix in c how to code matrix multiplication in cmultiplication of 2 matrices in csquare matrix multiplication in cmatrix multiplication code in c gfgmatrix multiplication in cmatrix multiplication cc code for multiplication of matrix in placec program for the matrix multiplicationmultiplication of matrix in c using 2d arraywrite a program to implement multiplication of matrices in calgorithm for matrix multiplication in carray multiply programtwo dimensional array in c multiplicationmultiplication of three matrices in cmultiplication matrix table c if statementimplement matrix multiplication in cmultiply 2 matrice codematrix multiplication codingwrite a program to print the multiplication of two n 2an 28square 29 matrix c matrix multiplication librarymatrix multiplication with different dimensions in c2 2a2 and 2 2a3 matrix multiplication in cc code for matrix multiplicationmultiple matrix arraycode in c for matrix multiplicationmultiplication two matrices by using 2 dimensional array in c4 dimensional matrix multiplicationwrite a c program for matrix multiplicationprogram to find multiplication of two matriceswrite a program to perform matrix multiplication how to take multiple matrix based on user in cprogram for matrix multiplication using array in carrys in multiplication two matrices in c programmatrix matrix multiplication c2 2a2 and 2 2a1 matrix multiplication in carray of integers multiplication in cmath h array multiplicationmatrix transpore in cprogram for matrix multiplication in cmatrix multiplication programmingc program of matrix multiplicationgiven two matrices multiply them and output the matrixmatrices multiplication program in cmatrix multiplication code in cmultiply matrices in c 3 2a3 matrix multiplication in cmatrix programming in cwrite a program to print multiplication of two matricesmatrix multiplication using c programc matrix multiplicationmultiplication of matrix 2 2a2 codewrite c programs to read two matrices and find the addition and multiplication of two matricesmatrix multiplication i n c2x2 matrix multiplication in chow to multiply 2 matrices in cuse in matrix computations in c programmingc multiply matricesprogram to multiply two matrices in cc multiply two matricesalgorithm to multiply matrices in cc program for matrix multiplication using functionsmatrix operations in chow to multiply matrices in cc program for multiplication of two 3x3 matriceshow to do matrix multiplication in 2d array 27multiply arrays in cmultiply two rectangular matrixes using cwrite a c program to perform matrix multiplicationmultiplying two matrices c programmatrix program in cc program for matrix multiplicationwrite a program to calculate matrix multiplicationmatrix multiplication prgram 3a write a c program for multiplication of the two matrixwrite a c program to multiply two matrices c code for multiplication of two matriceswap to multiply 2 matrixmultiplication of arrays in cwrite a shell script for multiplying 2 matrices and printing the resultantmatrix here youneed to represent a 2 dimensional matrix using a 1 dimensional array matrix multiplication in cresult for matrix multiplication in cc add two matrixprogram to multiply 2 matrices in cmultiplication of 2d array in cmatrix code in c 2 2a 40multiply 2 matrices in cmatrix multiplication i c programizmultiplication matrix in cmatrix right multiplication in cmultiplication array in cc program to find matrix multiplicationwap to multiply two matrices in chow to program matrix multiplicationwap to 3a a multiply two matrixes b find the transpose of a given matrix in cmultiplication arrays codec matrix multiplication functioncode for matrix multiplicationwrite a program for matrix multiplication in cprogram to multiply two matrix in csimple matrix programs in cmultiplication function in cmultiply matrix function cproduct of 2 matrices in cwrite a programme to multiply two matrices in chow to have product of two matix in cwrite a c program to implement matrix multiplication of three matriksmatrix multiplication write a program to perform matrix multiplication assume only square matrices of the same dimension array of multiplication cmultiplication of matrix in cmatrix multiplication in c programmultiply the two matrices chow to multiply two arrays in cmatrices in c3 3 matrix multiplication in cc matrix multiplication for loopn dimensional multiplication matricesproduct c of matriceproduct of 2 marix in cmultiply matric in cmatrix multiplication ccode of matrix multiplication in cmultiplication of matrices in cmatrix operations in c program2d matrix multiplicationwrite a program to multiply two matrices in cc program to find multipilication of 2 matricesmultiplying any 2 matrices in cmultiplication of 2 2 matrix c programc programming multiplying matrices2d array matrix multiplication in c language program matrix multiplication from file in cmultiply of matrix in cmatrices multiplication in cmatricx multiplication in cmultiplication of matrix program in cproduct of matrix in c programmatri multiplicarion c explainprogram to print out multiplcation array in cc matrix multiplication o 28n 29m 2an matrix multiplication in csimple matrix program in cc multiply matrixmatrix multiplication of two 2d arraysc program to implement matrix multiplicationmultiplication of two matrix in cresults for matrix multiplication in crule for matrix multiplication in cmatrix multiplication function c language2d matrix multiplication