# Numerical Methods: Multiplication of two matrices using two dimensional array in C

Let we have two 2 x 2 matrices A and B

Loading...

Let we have two 2 x 2 matrices A and B

\[ A = \begin{bmatrix} a_{00} & a_{01}\\

a_{10} & a_{11}

\end{bmatrix} \text{ and } B = \begin{bmatrix} b_{00} & b_{01} \\

b_{10} & b_{11} \end{bmatrix}\]

The multiplication of $A$ and $B$ works as follows

- Multiply $a_{00}$ with $b_{00}$ and $a_{01}$ with $b_{10}$ and sum them together. So the first element $r_{00}$ becomes $a_{00}$ . $b_{00}$ + $a_{01}$ . $b_{10}$
- Multiply $a_{00}$ with $b_{01}$ and $a_{01}$ with $b_{11}$ and sum them together. So the first element $r_{01}$ becomes $a_{00}$ . $b_{01}$ + $a_{01}$ . $b_{11}$
- Multiply $a_{10}$ with $b_{00}$ and $a_{11}$ with $b_{10}$ and sum them together. So the first element $r_{10}$ becomes $a_{10}$ . $b_{00}$ + $a_{11}$ . $b_{10}$
- Multiply $a_{10}$ with $b_{01}$ and $a_{11}$ with $b_{11}$ and sum them together. So the first element $r_{11}$ becomes $a_{10}$ . $b_{01}$ + $a_{11}$ . $b_{11}$

So the resulting matrix $R$ becomes,

\[ R = \begin{bmatrix} a_{00}.b_{00}+a_{01}.b_{10} & a_{00}.b_{01} + a_{01}.b_{11} \\ a_{10}.b_{00} + a_{11}.b_{10} & a_{10}.b_{01} + a_{11}.b_{11}\end{bmatrix}\]

Note: In order to multiply two matrices, $A$ and $B$, the number of columns in $A$ must equal the number of rows in $B$. Thus, if $A$ is an $m * n$ matrix and $B$ is an $r * s$ matrix, $n = r$.

Note: In order to multiply two matrices, $A$ and $B$, the number of columns in $A$ must equal the number of rows in $B$. Thus, if $A$ is an $m * n$ matrix and $B$ is an $r * s$ matrix, $n = r$.

**Source Code**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include<stdio.h> int main() { int r1, c1, r2, c2, matrix1[10][10], matrix2[10][10], result[10][10]; int i, j, k; printf("Enter the row and column of the first matrix: "); scanf("%d%d",&r1,&c1); printf("Enter the row and column of the second matrix: "); scanf("%d%d",&r2,&c2); if(c1 != r2){ printf("Matrix multiplication impossible"); } printf("Enter the first matrix: \n"); for(i = 0; i <r1; i++) for(j = 0; j < c1; j++) scanf("%d", &matrix1[i][j]); printf("Enter the second matrix: \n"); for(i = 0; i <r2; i++) for(j = 0; j < c2; j++) scanf("%d", &matrix2[i][j]); for(i = 0; i < r1; i++ ){ for(j = 0; j < c2; j++){ result[i][j] = 0; for(k = 0; k < c1; k++){ result[i][j] += matrix1[i][k] * matrix2[k][j]; } } } printf("The multiplication of the matrix is: \n"); for(i = 0; i < r1; i++){ for(j = 0; j < c2; j++){ printf("%d", result[i][j]); printf(" "); } printf("\n"); } return 0; } |

Loading...

this is wrong…. the multiplication logic is not right….

Shayan thanks for reply … but i don't think the logic is wrong. Can you prove it?

Bibek, your code for Marixmultiplication is korekt and gut, may be mohr in analytical Geometry source code in c/c++

I think your source code in c/c++ for two Matrix multiplication is correct and not wrong, which Mr Shayan mean, thank you for very nice and in simply way to write your programm about numerical Mathematic, i hope more in analytical Geometrie like point,victor,line,plan in c/c++, my compiler is dev c++ from blood sheet

its not c++ its c..

Its wrong.