#include <stdio.h>
#define ROW_A 3
#define COL_A 4
#define ROW_B COL_A
#define COL_B 2
int main(void) {
// Declare the matrices
int A[ROW_A][COL_A] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int B[ROW_B][COL_B] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
int C[ROW_A][COL_B];
// Initialize the result matrix to 0
for (int i = 0; i < ROW_A; i++) {
for (int j = 0; j < COL_B; j++) {
C[i][j] = 0;
}
}
// Perform the matrix multiplication
for (int i = 0; i < ROW_A; i++) {
for (int j = 0; j < COL_B; j++) {
for (int k = 0; k < COL_A; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
// Print the result matrix
for (int i = 0; i < ROW_A; i++) {
for (int j = 0; j < COL_B; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
This program declares two matrices, A
and B
, with sizes 3x4 and 4x2, respectively, and a result matrix C
with size 3x2. It then initializes C
to all zeros and performs the matrix multiplication C = A * B
, storing the result in C
. Finally, it prints the resulting matrix to the console.
Here is an example of the output of this program:
30 70
66 158
102 250
0 Comments