#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], transpose [20] [20], second[10][10], multiply[10][10];
// m row n col of first matrix
// transpose would be m col n row of first matrix
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]);
//make first matrix transpose
for (int i = 0; i < c; ++i)
for (int j = 0; j < d; ++j) {
transpose[d][c] = first[c][d]; //c becomes column d is row
}
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (m != 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 + transpose[d][k]*second[k][d];
}
multiply[d][c] = 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[d][c]);
printf("\n");
}
}
return 0;
}
我的目标是对第一个矩阵进行转置,然后将其与第二个矩阵相乘。我找到了示例代码,并根据需要对其进行了编辑,在乘法部分之前,它似乎一直有效。它给了我无关的数字,如下图所示。另外我还需要在此代码的某些部分添加其他内容,以便如果列值不等于行值,则程序应停止执行。