我努力编写了一个用于矩形矩阵乘法的C程序,但是当编译并运行该程序时,结果矩阵没有随乘积一起打印。作为用户输入,我尝试为矩阵 A 输入 2 行和 3 列,为矩阵 B 输入 3 行和 2 列。
如何更正以下代码?
#include<stdio.h>
int main()
{
int i,j,r,c,r1,c1,r2,c2,k,a[10][10],b[10][10],result[10][10];
printf("\n Enter the no. of rows and column desired for the 1st matrix (between 1 to 10):");
scanf("%d%d",&r1,&c1);
printf("\n Enter the no. of rows and columns for the 2nd matrix - between 1 to 10 (Pls note that no. of rows of 1st matrix should be equal to the no. of columns in the 2nd matrix for multiplication):");
scanf("%d%d",&r2,&c2);
printf("\n Enter the elements of the 1st Matrix:");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
scanf("%d",&a[i][j]);
}
}
printf("\n Print of 1st Matrix:\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
printf("\t%d",a[i][j]);
}
printf("\n");
}
printf("\n Enter the elements of the 2nd Matrix:");
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
scanf("%d",&b[i][j]);
}
}
printf("\n Print of 2nd Matrix:\n");
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
printf("\t%d",b[i][j]);
}
printf("\n");
}
//multiplying 1st and 2nd matrices and storing the results in results[i][j]
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
for(k=0;k<c1;k++){
result[i][j] = result[i][j] + (a[i][k] * b[k][j]);
}
}
}
//Printing the results
printf("\n After Multiplication of two matrices:\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
printf("\t%d",result[i][j]);
}
printf("\n");
}
return 0;
}
您的代码存在一些问题。
结果矩阵类型和初始化
您将结果矩阵定义为整数,但是当您执行乘法时,结果始终是浮点数。你有两个解决方案
保存乘法结果时使用显式转换为 int
将结果矩阵类型更改为 float
此外,您应该初始化结果矩阵以避免意外行为。
我建议你这样改代码
float result[10][10]; //define result matrix
...
...
//multiplying 1st and 2nd matrices and storing the results in results[i][j]
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
//init i,j element of result matrix
result[i][j] = 0.0; //add here the initialisation
for(k=0;k<c1;k++){
result[i][j] = result[i][j] + (a[i][k] * b[k][j]);
}
}
}
打印输出循环
在您的代码中,您使用了变量
r
和 c
,但没有初始化它们。这是错误输出的主要原因。
按如下方式更改代码:
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
printf("\t%f",result[i][j]);
}
printf("\n");
}
新代码
这里是您的程序的完整代码,其中包含更正和 scanf 语句中的一些改进。
#include<stdio.h>
int main()
{
int i,j,r,c,r1,c1,r2,c2,k,a[10][10],b[10][10];
float result[10][10];
printf("\n Enter the no. of rows and column desired for the 1st matrix (between 1 to 10):");
scanf("%d %d",&r1,&c1); //put a space between escape codes in order to avoid conversion issues
printf("\n Enter the no. of rows and columns for the 2nd matrix - between 1 to 10 (Pls note that no. of rows of 1st matrix should be equal to the no. of columns in the 2nd matrix for multiplication):");
scanf("%d %d",&r2,&c2); //put a space between escape codes in order to avoid conversion issues
printf("\n Enter the elements of the 1st Matrix:");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
scanf("%d",&a[i][j]);
}
}
printf("\n Print of 1st Matrix:\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
printf("\t%d",a[i][j]);
}
printf("\n");
}
printf("\n Enter the elements of the 2nd Matrix:");
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
scanf("%d",&b[i][j]);
}
}
printf("\n Print of 2nd Matrix:\n");
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
printf("\t%d",b[i][j]);
}
printf("\n");
}
//multiplying 1st and 2nd matrices and storing the results in results[i][j]
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
//init i,j element of result matrix
result[i][j] = 0.0;
for(k=0;k<c1;k++){
printf("(%d %d %d) %f - %d - %d\n",i,j,k,result[i][j],a[i][k],b[k][j]);
result[i][j] = result[i][j] + (a[i][k] * b[k][j]);
}
}
}
//Printing the results
printf("\n After Multiplication of two matrices:\n");
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
printf("\t%f",result[i][j]);
}
printf("\n");
}
return 0;
}