我无法纠正此C代码中的错误,该代码正在接受用户输入,以便使用scanf填充2d数组
我的尝试 现在,当我打印这些值只是为了检查是否正常时,我得到的结果非常奇怪我无法纠正此C代码中的错误,该代码正在接受用户输入,以便使用scanf填充2d数组,c,arrays,multidimensional-array,C,Arrays,Multidimensional Array,我的尝试 现在,当我打印这些值只是为了检查是否正常时,我得到的结果非常奇怪 2 4 15 25 30 35 45 50 10 20 2 15 25 10 10 15 5 12 40 55 1 10 25 35 45 50 20 28 27 35 15 40 4 5 3 5 10 27 printf(“测试用例%d的编号”,ntc); 对于(i=0;i C无法预见未来。以后无法分配数组并确定其大小。您需要先读取ntc,然后使用它 您还需要启用编译器警告,并确保所有程序生成无警告的。编译器很容易
2
4
15 25 30 35 45 50 10 20
2
15
25
10
10 15 5 12 40 55 1 10 25 35 45 50 20 28 27 35 15 40 4 5
3
5
10
27
printf(“测试用例%d的编号”,ntc);
对于(i=0;i
C无法预见未来。以后无法分配数组并确定其大小。您需要先读取ntc
,然后使用它
您还需要启用编译器警告,并确保所有程序生成无警告的。编译器很容易捕获此错误
作为旁注,C中的自动是危险的,因为它们很容易导致程序超出其堆栈大小限制。避免它们。动态分配所有未知大小的数组。可能是缩进代码并检查来自scanf
的返回值,我无法重现它。问题可能是您分配了一个d总线阵列。但你没有显示。我已经编辑了代码并添加了阵列分配。请解释什么是奇怪的,不要让读者猜测。这是相关的,因为你认为的方式对调试总是重要的。你能添加你自己调试尝试的结果吗?有关提示,请参考解决问题的另一个好方法这里描述了任何类型的问题,如果您一开始觉得这些文本有点愤世嫉俗,请继续阅读,并认真尝试按照描述的方式行事。这是有经验的程序员在遇到问题时几乎总是做的。感谢它现在的工作,以及您对编译器警告的建议,gcc没有给我任何警告,否则我将你会抓到这个的error@adityajain您需要启用警告。对于gcc,请尝试使用-Wall
。
2
4
15 25 30 35 45 50 10 20
2
15
25
10
10 15 5 12 40 55 1 10 25 35 45 50 20 28 27 35 15 40 4 5
3
5
10
27
printf("no of test case %d\n",ntc);
for(i=0;i<ntc;i++)
{
printf("Case #%d\n",i+1);
printf("no of bus %d\n",nb[i]);
p=2*nb[i];
printf("bus route array");
for(j=0;j<p;j++)
{
printf(" %d ",barr[i][j]);
}
printf("\nno of city for which bus passing by count is to be determined %d \n",nc[i]);
q=nc[i];
printf("city array");
for(k=0;k<q;k++)
{
printf(" %d ",carr[i][k]);
}
printf("\n");
}
no of test case 2
Case #1
no of bus 4
bus route array 10 15 5 12 40 55 1 10
no of city for which bus passing by count is to be determined 2
city array 5 10
Case #2
no of bus 10
bus route array 10 15 5 12 40 55 1 10 25 35 45 50 20 28 27 35 15 40 4 5
no of city for which bus passing by count is to be determined 3
city array 5 10 27
int *nb=(int *)malloc(ntc*sizeof(int)); // what is ntc?
int barr[ntc][ntc]; // what is ntc?
int *nc=(int *)malloc(ntc*sizeof(int)); // what is ntc?
int carr[ntc][ntc]; // what is ntc?
scanf("%d",&ntc); // input -> test case // oh