matrix multiplication in c

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

showing results for - "matrix multiplication in c"
Shaima
31 Nov 2019
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
Émilie
21 Mar 2018
1#include <stdio.h>
2
3void main ()
4{
5    int i, j, k, m1, n1, m2, n2;
6    int matrix1[10][10],matrix2[10][10],mult[10][10];
7
8    printf("Enter number of rows of matrix 1 : ");
9    scanf("%d", &m1);
10    printf("Enter number of columns of matrix 1 : ");
11    scanf("%d", &n1);
12
13    printf("\n");
14
15    for (i = 0; i < m1; i++)
16    {
17        for (j = 0; j < n1; j++)
18        {
19            printf("Enter element of matrix 1[%d][%d]: ", i, j);
20            scanf("%d", &matrix1[i][j]);
21        }
22    }
23
24    printf("\n");
25
26    printf("Enter number of rows of matrix 2 : ");
27    scanf("%d", &m2);
28    printf("Enter number of columns of matrix 2 : ");
29    scanf("%d", &n2);
30
31    printf("\n");
32
33    if(m1==n2)
34    {
35        for (i = 0; i < m2; i++)
36        {
37            for (j = 0; j < n2; j++)
38            {
39                printf("Enter elements of matrix 2[%d][%d]: ", i, j);
40                scanf("%d", &matrix2[i][j]);
41            }
42        }
43
44        printf("\n");
45
46        printf("\n....Your resultant matrix is....\n\n");
47
48       for(i=0;i<m1;i++)
49        {
50            for(j=0;j<n2;j++)
51            {
52                mult[i][j]=0;
53                for(k=0;k<m2;k++)
54                {
55                    mult[i][j]+=matrix1[i][k]*matrix2[k][j];
56                }
57            }
58        }
59
60        for (i = 0; i < m1; i++)
61        {
62            for (j = 0; j < n2; j++)
63            {
64                printf("%d\t", mult[i][j]);
65            }
66            printf("\n");
67        }
68
69    }
70
71    else
72        printf("Matrix multiplication not possible");
73}
74
75
76
Gena
04 Jan 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}
Delbert
05 Nov 2017
1#include <stdio.h>
2
3int main()
4{   int a_rows,a_cols,b_rows,b_cols,i,j,k,sum=0;
5
6    printf("Enter the rows and columns for first matrix (row)(col)  :\n");
7    scanf("%d %d",&a_rows,&a_cols);
8
9    printf("Enter the rows and columns for second matrix (row)(col) :\n");
10    scanf("%d %d",&b_rows,&b_cols);
11    int a[a_rows][a_cols], b[b_rows][b_cols],matrix[10][10];
12
13    if(a_cols != b_rows){
14        printf("Sorry! We can't multiply the matrix because the column number of matrix 1 and the row number of matrix 2 aren't same !!\n");
15        
16    }else{
17        printf("Enter elements for first matrix  :\n");
18       for(i = 0; i < a_rows; i++){
19           for(j = 0; j< a_cols; j++){
20               scanf("%d",&a[i][j]);
21           }
22       }
23
24    printf("Enter elements for second matrix  :\n");
25
26       for(i = 0; i < b_rows; i++){
27           for(j = 0; j < b_cols; j++){
28               scanf("%d",&b[i][j]);
29           }
30       }
31
32       printf("multiplying matrix....\n");
33       //multiplying matrix
34       for(i=0; i < a_rows; i++){
35           for(j = 0; j < b_cols; j++){
36               for(k = 0; k < a_cols; k++){
37                   sum+=a[i][k] * b[k][j];
38               }
39               matrix[i][j] = sum;
40               sum=0;
41               
42           }
43           printf("\n");
44       }
45
46       printf("first matrix  :\n");
47       for(i = 0; i < a_rows; i++){
48           for(j = 0; j< a_cols; j++){
49               printf("%d ",a[i][j]);
50           }
51           printf("\n");
52       }
53
54
55        printf("\n\n");
56
57       printf("second matrix  :\n");
58       for(i = 0; i < b_rows; i++){
59           for(j = 0; j< b_cols; j++){
60               printf("%d ",b[i][j]);
61           }
62           printf("\n");
63       }
64
65
66       printf("Multiplied matrix  :\n");
67       for(i = 0; i < a_rows; i++){
68           for(j = 0; j< b_cols; j++){
69               printf("%d ",matrix[i][j]);
70           }
71           printf("\n");
72       }
73    }
74
75     
76
77    return 0;
78}
Santino
02 Oct 2020
1#include <stdio.h>
2void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2);
3void multiplyMatrices(int first[][10], int second[][10], int multResult[][10], int r1, int c1, int r2, int c2);
4void display(int mult[][10], int r1, int c2);
5
6int main() {
7    int first[10][10], second[10][10], mult[10][10], r1, c1, r2, c2;
8    printf("Enter rows and column for the first matrix: ");
9    scanf("%d %d", &r1, &c1);
10    printf("Enter rows and column for the second matrix: ");
11    scanf("%d %d", &r2, &c2);
12
13    // Taking input until columns of the first matrix is equal to the rows of the second matrix
14    while (c1 != r2) {
15        printf("Error! Enter rows and columns again.\n");
16        printf("Enter rows and columns for the first matrix: ");
17        scanf("%d%d", &r1, &c1);
18        printf("Enter rows and columns for the second matrix: ");
19        scanf("%d%d", &r2, &c2);
20    }
21
22    // Function to take matrices data
23    enterData(first, second, r1, c1, r2, c2);
24
25    // Function to multiply two matrices.
26    multiplyMatrices(first, second, mult, r1, c1, r2, c2);
27
28    // Function to display resultant matrix after multiplication.
29    display(mult, r1, c2);
30
31    return 0;
32}
33
34void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2) {
35    printf("\nEnter elements of matrix 1:\n");
36
37    for (int i = 0; i < r1; ++i) {
38        for (int j = 0; j < c1; ++j) {
39            printf("Enter a%d%d: ", i + 1, j + 1);
40            scanf("%d", &first[i][j]);
41        }
42    }
43    printf("\nEnter elements of matrix 2:\n");
44
45    for (int i = 0; i < r2; ++i) {
46        for (int j = 0; j < c2; ++j) {
47            printf("Enter b%d%d: ", i + 1, j + 1);
48            scanf("%d", &second[i][j]);
49        }
50    }
51}
52
53void multiplyMatrices(int first[][10], int second[][10], int mult[][10], int r1, int c1, int r2, int c2) {
54
55    // Initializing elements of matrix mult to 0.
56    for (int i = 0; i < r1; ++i) {
57        for (int j = 0; j < c2; ++j) {
58            mult[i][j] = 0;
59        }
60    }
61
62    // Multiplying first and second matrices and storing in mult.
63    for (int i = 0; i < r1; ++i) {
64        for (int j = 0; j < c2; ++j) {
65            for (int k = 0; k < c1; ++k) {
66                mult[i][j] += first[i][k] * second[k][j];
67            }
68        }
69    }
70}
71
72void display(int mult[][10], int r1, int c2) {
73
74    printf("\nOutput Matrix:\n");
75    for (int i = 0; i < r1; ++i) {
76        for (int j = 0; j < c2; ++j) {
77            printf("%d  ", mult[i][j]);
78            if (j == c2 - 1)
79                printf("\n");
80        }
81    }
82}
83
queries leading to this page
multiplication array in cprogram for matrix multiplication using array in cmatrix multiplication i n c2d array multiplication in cmatrix multiply in carray of integers multiplication in cmatricx multiplication in cwrite a c program for multiplication of two matrices multiplication of m 2an matrices in cc program for the matrix multiplicationmatrix right multiplication in cmatrix multiplication in c logicmatrix multiplication c programmatrix multiplication programc program to find matrix multiplicationwrite a c program to multiply two matrices c multiply matrixaddition of two matrix in c programmultiplication function in cuse in matrix computations in c programmingmatrix multiplication code in c gfgmatrix transpore in cmatrix program in cmath h array multiplicationc matrix multiplication for loopblock multiplication of matrices program in cc code of matrix multiplyalgorithm for matrix multiplication in cwrite a c program multiplication of two matrixprogram to multiply two matrices in c2d array multiplication cwrite a c program to perform matrix multiplicationc matrix multiplication functionmatrix multiplication code in cmultiply 2 matrice codec metric multiplacationmatriz multiplication c matrix multiplication in cmatrix multiplication c codec programming multiplying matriceswrite a program for matrix multiplication in cm 2an matrix multiplication in cc program for matrix multiplication2x2 matrix multiplication in csquare matrix multiplication 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 arrayc matrix multiplicationmatrix multiplication prgrammatrix multiplication in c using arrayc add two matrixmatrix multiplication i c programizmatrix multiplication cprogram for product of two matriceshow to take multiple matrix based on user in cmultiply array elements in cwrite a program to implement matrix multiplication c program for multiplying two matricesprogram to multiply two matrix in cmultiply of matrix in cproduct of matrix in c programmultiplication of matrix program in cwrite a c program for matrix multiplicationmultiplication of 2d arrayc multiply matricesmatrix multiplicaytion cmatrix operations in cmatrix multiplication using function in cproduct c of matricegiven two matrices multiply them and output the matrixhow to program matrix multiplicationc program to find multipilication of 2 matricesmultiplication of two matrix in cmultiply two matrix cmultiply matrix in cmultiply matric in cproduct of 2 matrices in cmultiply matrix function c 3a write a c program for multiplication of the two matrixmatrix multiplication in c with different dimensionsmatrix multiplication in c 2b 2bmatrix multiplication cmultiplication of 2 matrices in cmultiplication of matrices in cwrite a program to multiply two matrices in cprogram for matrix multiplicationmultiplication arrays codeprogram for matrix multiplication in cmatrix multiplication data structurearray multiplication csimple matrix programs in cimplement matrix multiplication in cc code for multiplication of matrix in placemultiplication of a matrix in c matrix multiplication codematrix in c programmingc program to multiply 5 matricescode in c for matrix multiplicationproduct of 2 marix in cmatrix multiplciation in cwap to multiply two matrices in cmatrix multiplication with different dimensions in cmultiplication of matrix 2 2a2 code in cmatrix multiplication in array cmatri multiplicarion c explainlogic of matrix multiplication in cmultiply two rectangular matrixes using cmatrix multiplication write a program to perform matrix multiplication assume only square matrices of the same dimension write a program to perform matrix multiplication c program for matrix multiplication using functionscode for matrix multiplicationwap to multiply 2 matrixwrite a c program for matrix multiplication using operator overloadingsproduct of matrix in cmultiplication of 2 matrices codemultiplication of 22three 22 matrices in cprogram to multiply 2 matrices in cmulti matrix cmultiplication of matrix in cmultiply matrices c programc matrix multiplywrite a program to multiply 2 matrices in chow to code matrix multiplication in c with a matrixc program to multiply two matricestwo dimensional array in c multiplicationc program to multiply n xn matrixmatrices in cwrite a c program to implement three matrices multiplicationcode of matrix multiplication in cmultiplying matrix in cmultiplication of any two matrix in cmatrices multiplication in c2 2a2 and 2 2a1 matrix multiplication in cprogram of multiply two matrices in cmultiplication of matrix in c using 2d arraymatrix multiplication in c 2d arraymultiplication of 2d array in cmatrix multiplication logicc program multiply matrixc code for multiplication of two matricesmultiply the two matrices ccode for matrix multiplicationmultiplication of two matrices code2d matrix multiplicationmultiplying any 2 matrices in cc multiply two matricesmultiply two matrix in cc matrix multiplication o 28n 29program to print out multiplcation array in cresult for matrix multiplication in carray multiplication in c2d array matrix multiplication in c language program c program for multiplication of two 3x3 matricesmatrix multiplication in c programalgorithm to multiply matrices in cc program multiplication of two matrixc program to find matrix multiplication using arrayswrite a program to print the multiplication of two n 2an 28square 29 matrix write a program to print multiplication of two matriceswrite a c program to find the matrix multiplication of two matrices results for matrix multiplication in cmultiplication of 2d matrix in cmatrix multiplication program in cmatrix matrix multiplication cwrite a program to calculate matrix multiplicationmatrix multiplication using cmultiplication of 2 2 matrix c programmatrix addition in chow to multiply two arrays in cmultiply matrices in c matrix code in c 2 2a 40c code for matrix multiplicationmultiplication matrix table c if statementwrite a program to implement multiplication of matrices in cmatrix multiplication in cmatrix programming in cmultiply two matrices in cwap to 3a a multiply two matrixes b find the transpose of a given matrix in cwrite a programme to multiply two matrices in cmultiplication of matrix cmatrix multiplication using c programmultiply 2 matrices in cwrite c programs to read two matrices and find the addition and multiplication of two matricesalgorithm to multiply two matrices in cmultiplying any 2 matrices in c codehow to do matrix multiplication in cmultiplication of two matrices in cmatrix multiplication algorithm in chow to multiply matrix in cmultiply array in cmultiplication of square matrix in cmultiplication matrix in cmatrix multiplicationin carray of multiplication cc program for matrix multiplicationmultiplication two matrices by using 2 dimensional array in c3 2a3 matrix multiplication in csimple matrix program in chow to multiply 2 matrices in cproduct of 2d array in cc matrix mathmultiplying matrices in chow to do matrix multiplication in 2d array 27multiplication of three matrices in cmatrix multiplication code in cc program of matrix multiplicationmultiply arrays in cmultiplying two atrix programmatrix operations in c programmatrix multiplication codingmultiply matrix cperform matrix multiplication using carrys in multiplication two matrices in c programwrite a c program to implement matrix multiplication of three matrikshow to multiply matrices in cmultiplying two matrices in cc program to multiply two matrix using multi dimensional arraysc program to implement matrix multiplicationcode for matrx multiplication 2dmatrices multiplication program in cc matrix multiplication librarymatrix multiplication function c language2 2a2 and 2 2a3 matrix multiplication in c3 3 matrix multiplication in cprogram to find multiplication of two matricesrule for matrix multiplication in cmatrix multplication of 2 matrix in chow to have product of two matix in cmultiple matrix arraymultiplication of matrix 2 2a2 codematrix multiplication from file in carray multiply programmatrix multiplication in c using functionhow to code matrix multiplication in cmultiplying two matrices c programmatrix multiplication programmingcalculate product of 2 matrix in cright multiply matrix cmatrix multiplication cmultiplication of arrays in cmatrix multiplication in c