C language matrix multiplication

       When a matrix is ​​operated, it is multiplied row by column and then added. Therefore, for a two-dimensional matrix, two for loops are needed to solve the problem. The first for loop specifies the row, and the second for loop specifies the column. In addition, define a variable k to identify the elements in the row of A and the elements in the column of B. Traverse the elements in the rows of A and the elements in the columns of B by incrementing k.

The code for two-dimensional matrix multiplication using C language is as follows:

#include<stdio.h>

#define M 3
#define N 4
#define P 5


int main()
{
int i, j, k;
int a[M][N], b[N][P], c[M][P];
printf("a=\n");
for (i = 0; i < M; i++)

for (j = 0; j < N;j++)
scanf("%d", &a[i][j]);
}
printf("b=\n");
for (j = 0; j < N; j++)
{
for (k = 0; k < P; k++)
scanf("%d", &b[j][k]);
} for (i = 0; i < M; i++) { for (k = 0; k < P; k++) c[i][k] = 0; } for (i = 0; i < M; i++) { for (j = 0; j < N; j++) for (k = 0; k < P; k++) {














c[i][k] += a[i][j] * b[j][k];
} } printf("c=\n"); for (i = 0; i < M; i++) { for (k = 0; k < P; k++) printf("%d ", c[i][k]); printf("\n"); } return 0;












}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325792344&siteId=291194637