使用C语言,数据结构编写m*n矩阵和q*k矩阵的乘法
时间: 2024-01-17 18:47:48 浏览: 115
数据结构采用C语言编写
3星 · 编辑精心推荐
```
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int m, n, q, k;
int matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
printf("Enter the size of the first matrix (m x n): ");
scanf("%d %d", &m, &n);
printf("Enter the size of the second matrix (q x k): ");
scanf("%d %d", &q, &k);
if (n != q) {
printf("The matrices cannot be multiplied.\n");
return 0;
}
printf("Enter the elements of the first matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter the elements of the second matrix:\n");
for (int i = 0; i < q; i++) {
for (int j = 0; j < k; j++) {
scanf("%d", &matrix2[i][j]);
}
}
// Multiply the matrices
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
result[i][j] = 0;
for (int l = 0; l < n; l++) {
result[i][j] += matrix1[i][l] * matrix2[l][j];
}
}
}
// Print the result matrix
printf("The product of the matrices is:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
```
阅读全文