Matrix multiplication in C
0 247
Matrix multiplication in C is a process of multiplying two matrices to produce a third matrix.
Each entry in the resulting matrix is determined by multiplying and summing the elements of the respective row from the first matrix with the corresponding column from the second
Example:
Consider two matrices:
// Program Matrix multiplication in C #include<stdio.h>int main() { int A[2][2] = { {2, 3}, {4, 1} }; int B[2][2] = { {1, 2}, {3, 4} }; int C[2][2]; int i, j, k; // Matrix multiplication for(i = 0; i < 2; i++) { for(j = 0; j < 2; j++) { C[i][j] = 0; for(m = 0; m < 2; m++) { C[i][j] += A[i][m] * B[m][j]; } } } // Displaying the result printf("Resultant Matrix:\n"); for(i = 0; i < 2; i++) { for(j = 0; j < 2; j++) { printf("%d ", C[i][j]); } printf("\n"); } return 0; }
Output:
Resultant Matrix: 11 16 7 12
We initialize matrices A and B with the given values.
We then compute the matrix multiplication using nested loops.
The result is stored in matrix C.
Finally, we display the resulting matrix C using printf.
Share:
Comments
Waiting for your comments