用于求矩阵乘积的C程序是';行不通
矩阵“b”元素输入之后的代码行未被执行。按enter按钮时也不会发生任何情况。 我不想知道这是逻辑错误还是语义错误用于求矩阵乘积的C程序是';行不通,c,matrix,C,Matrix,矩阵“b”元素输入之后的代码行未被执行。按enter按钮时也不会发生任何情况。 我不想知道这是逻辑错误还是语义错误 #include<stdio.h> main() { int a[10][10],b[10][10],c[10][10],i,j,k,m1,m2,n1,n2; printf("Enter the order of the matrix a: "); scanf("%d%d",&m1,&n1); printf("Enter
#include<stdio.h>
main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m1,m2,n1,n2;
printf("Enter the order of the matrix a: ");
scanf("%d%d",&m1,&n1);
printf("Enter the order of the matrix b: ");
scanf("%d%d",&m2,&n2);
//test for eligibility of matrices for multiplication
if(n1==m2)
{
//input of the elements of matrix a
printf("Enter the elements of the matrix a: ");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
scanf("%d",&a[i][j]);
}
//Input of the elements of the matrix b
printf("Enter the elements of the matrix b: ");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
scanf("%d",&b[i][j]);
}
//calculating the result of multiplication of matrices 'a' and 'b'
printf("The result of multiplication of 'a' and 'b' is: ");
for(i=0;i<n1;i++)
{
for(j=0;j<m2;j++)
{
for(k=0;k<n2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
scanf("%d",&c[i][j]);
}
}
}
//print the resultant matrix
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
printf("%4d",c[i][j]);
}
}
}
else
printf("The matrices are not eligible for multiplication");
}
#包括
main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m1,m2,n1,n2 ;;
printf(“输入矩阵a的顺序:”);
scanf(“%d%d”、&m1和&n1);
printf(“输入矩阵b的顺序:”);
scanf(“%d%d”、&m2和&n2);
//矩阵乘法合格性试验
如果(n1==m2)
{
//矩阵a元素的输入
printf(“输入矩阵a的元素:”);
对于(i=0;i好的,我花了时间来修复它:
#include <stdio.h>
#include <string.h> // New, for memset()
int main(void) // "Proper" signature
{
int a[10][10],b[10][10],c[10][10],i,j,k,m1,m2,n1,n2;
memset(c, 0, 100); // New, Ug Lee way of zeroing of the array
printf("Enter the order of the matrix a: ");
scanf("%d%d",&m1,&n1);
printf("Enter the order of the matrix b: ");
scanf("%d%d",&m2,&n2);
//test for eligibility of matrices for multiplication
if(n1==m2)
{
//input of the elements of matrix a
printf("Enter the elements of the matrix a: ");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
scanf("%d",&a[i][j]);
}
//Input of the elements of the matrix b
printf("Enter the elements of the matrix b: ");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
scanf("%d",&b[i][j]);
}
//calculating the result of multiplication of matrices 'a' and 'b'
printf("The result of multiplication of 'a' and 'b' is: \n");
for(i=0;i<n1;i++)
{
for(j=0;j<m2;j++)
{
for(k=0;k<n2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
// scanf removed
}
}
}
//print the resultant matrix
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
printf("%4d ",c[i][j]);
}
printf("\n"); // New, add newline
}
}
else
printf("The matrices are not eligible for multiplication");
}
#包括
#include//New,用于memset()
int main(无效)/“正确”签名
{
int a[10][10],b[10][10],c[10][10],i,j,k,m1,m2,n1,n2 ;;
memset(c,0,100);//数组的新的归零方法
printf(“输入矩阵a的顺序:”);
scanf(“%d%d”、&m1和&n1);
printf(“输入矩阵b的顺序:”);
scanf(“%d%d”、&m2和&n2);
//矩阵乘法合格性试验
如果(n1==m2)
{
//矩阵a元素的输入
printf(“输入矩阵a的元素:”);
对于(i=0;ic[i][j]=c[i][j]+a[i][k]*b[k][j];
--在分配某项内容之前,在第一次迭代时,c[i][j]
的值是多少?scanf(“%4d”,&c[i][j];
这到底是做什么的?孩子,发布的代码中有语法错误。(#7,#35),它甚至没有编译…另外,在第35行,有一个scanf
。为什么?是的,你必须初始化你使用的所有东西。如果你这样做了,请编辑你问题中的代码,使它是最新的。scanf
?你还应该说int main()
,而不仅仅是main()
。感谢您让我知道将数组初始化为零的另一种方法。@ChetanPujar请记住,当您使用calloc
时,它专门进行清零,但另一个*alloc
-s不进行清零。注意:calloc
将所有位清零。对于整数,这也意味着将值设置为零(它们是这样定义的),但对于浮点或指针(空指针)之类的其他类型来说,这不一定是真的。哦,而且:只有代码的答案对OP没有真正的帮助,更不用说其他读者了。你说的“没有它就尝试”是什么意思这就是为什么我清楚地写下这里使用的整数是可以的。顺便说一句,memset
也有同样的问题。对于memset
问题:通过这个,我假设我们可以做到。