write a C program to calculate the multiplication of two matrices using two_dimensional array. Requirement Getting input from users
时间: 2024-03-21 14:43:07 浏览: 71
用c语言编写两矩阵相乘程序
下面是一个使用二维数组计算两个矩阵乘积的C语言程序,可以从用户获取输入:
```c
#include <stdio.h>
int main() {
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix: ");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix:\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix: ");
scanf("%d%d", &p, &q);
if (n != p)
printf("The matrices cannot be multiplied with each other.\n");
else {
printf("Enter the elements of second matrix:\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
```
该程序通过提示用户输入每个矩阵的行和列数,并分别提示输入每个矩阵的元素。然后它检查两个矩阵是否可以相乘,如果可以,则计算两个矩阵的乘积并将其存储在一个名为multiply的二维数组中,最后打印输出结果。
阅读全文