【发布时间】:2018-11-06 10:17:31
【问题描述】:
我无法在 c 中添加两个二维数组 可能是什么问题 ?我正在尝试从用户输入添加两个多维数组,但输出不正确。 下面是我的代码
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p,q,c,d,k;
int first[10][10],second[10][10],sum[10][10];
clrscr();
printf("\nEnter the number of rows and columns of the first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the elements of the first matrix");
for (c = 0;c < m;c++)
for (d = 0;d < n;d++)
scanf("%d",&first[c][d]);
printf("\nEnter the number of rows and columns of the second matrix");
scanf("%d %d",&p,&q);
printf("\nEnter the elements of the second matrix");
for (c = 0;c < p;c++)
for (d = 0;d < p;d++)
scanf("%d",&second[c][d]);
for (c = 0;c < m;c++)
for (d = 0;d < n;d++)
for (k = 0;k < p;k++)
{
sum[c][d] = first[c][k] + second[k][d];
}
printf("\nThe sum of the two matrices is : \n\n");
for (c = 0;c < m;c++)
for (d = 0;d < n;d++)
{
printf("%d",&sum[c][d]);
if (d == n-1)
{
printf("\n\n");
}
}
getch();
}
【问题讨论】:
-
c 还是 c++ ?请格式化您的代码
-
c++。我在 TurboC++ 中使用它
-
for (d = 0;d < p;d++)似乎是错误的。d应该一直运行到q,不是吗? -
@BBKiVines
int m,n,p,q,c,d,k;-- 如果您使用了更有意义的变量名,您可能更容易发现其中一个错误。 -
为什么允许两者有不同的维度?如果
m != p或n != q你的总和是错误的