C中的矩阵乘法
2021-07-07 07:28:08
深夜i
--
--
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 number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter 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;
}
3 X 3 矩阵乘法 C 程序的输出:
下载矩阵乘法程序。
矩阵在计算机编程中有很多应用; 表示图形数据结构,用于求解线性方程组等。 关于如何使用最少的操作将它们相乘,正在进行大量研究。 您还可以使用指针来实现该程序。
上一篇:
idea打包java可执行jar包
下一篇:
打印字符串的C程序
评论区