C 二维数组输入(-1表示行结束输入,-2表示二维数组结束输入)问题
大家好,谢谢你们花时间回答 在练习过程中,我多次遇到需要输入2D数组(矩阵)的问题。在练习中,它说当我输入完一行(进入下一行)时,我应该输入-1,输入-2表示矩阵输入完成 我试着做这样的事情,我可以用在我的大多数练习中,但它不起作用,我似乎找不到原因。这是我的密码:C 二维数组输入(-1表示行结束输入,-2表示二维数组结束输入)问题,c,loops,for-loop,multidimensional-array,scanf,C,Loops,For Loop,Multidimensional Array,Scanf,大家好,谢谢你们花时间回答 在练习过程中,我多次遇到需要输入2D数组(矩阵)的问题。在练习中,它说当我输入完一行(进入下一行)时,我应该输入-1,输入-2表示矩阵输入完成 我试着做这样的事情,我可以用在我的大多数练习中,但它不起作用,我似乎找不到原因。这是我的密码: #include <stdio.h> #include <stdlib.h> int main() { int i,j,matrix[100][100]; int m,n; fo
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,matrix[100][100];
int m,n;
for(i=0; i<100; i++) {
for(j=0; j<100; j++)
{
scanf("%d", &matrix[i][j]);
if(matrix[i][j]==-2) {
m=i+1; //After I enter -2, the matrix dimensions should be
n=j-1; // "m" and "n", but for some reason once I enter this
// if statement, "i" has to be incremented by 1
// and "j" has to be decremented by 1 for it to work
i=1000; // i use this to get out of nested loop
break;
}
if(mat1[i][j]==-1)
{
i++; // once the user inputs -1, I increase "i"(next row),
j=0; //and here I set the "column" count to 0
}
}
}
for(i=0; i<m; i++) {
for(j=0; j<n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
矩阵(打印在屏幕上)如下所示:
1 2 3
0 1 2
0 1 2
有人知道为什么这不能按预期工作吗?我好像找不到了 您的代码没有意义 事实上,所有输入都在一个内部循环中处理。但是,在输入
-2
之前,将执行此内部循环。所以外环是多余的
在内部循环中,下一行的迭代从1
for(j=0; j<100; j++)
^^^
{
//...
if(mat1[i][j]==-1)
{
i++; // once the user inputs -1, I increase "i"(next row),
j=0; //and here I set the "column" count to 0
^^^^
}
如果输入是
1 2 3 -1
4 5 6 -1
7 8 9 -2
然后程序输出以下矩阵
1 2 3
4 5 6
7 8 9
或者,例如,如果输入看起来像
1 2 3 4 -1
5 6 -1
-2
然后输出为
1 2 3 4
5 6 0 0
在程序中,
m
表示行数,n
表示结果矩阵中的列数。问题中的代码试图用以下语句控制i
和j
:
for(i=0; i<100; i++) {
for(j=0; j<100; j++)
if(mat1[i][j]==-1)
{
i++; // once the user inputs -1, I increase "i"(next row),
j=0; //and here I set the "column" count to 0
}
这些冲突。当输入“-1”时,后面的语句将
j
重置为零,但随后程序控制流到for
循环的内部末尾。对于
循环,for(j=0;j至少变量i在外循环和if语句中增加了两次。否则,我应该如何跳转到下一个“行”从内部循环中获取矩阵的信息?非常感谢您的帮助,对于新手来说,这是可以理解和容易理解的。我知道我在哪里犯了错误。再次感谢+1@VladfromMoscow:当替换为问题代码中的for
循环时,生成的源代码将使用Apple LLVM 10.0.1编译,其中clang-1001.0.46.4,在macOS 10.14.6上执行,并在给定问题中所示的输入时生成所需的输出。您认为什么是无效的?@EricPostDischil编译代码并不意味着有效代码。:)代码至少没有考虑原始数组的上限。。此外,为一行输入的数据数量可能因行而异。@VladFrom莫斯科:这些投诉与OP提出的问题无关,不会使程序无效。它们限制了它的实用性——它不能用于任意或不规则大小的数组,但代码在OP打算使用它的有限情况下是有效的。请记住,堆栈溢出不是一种代码编写服务。回答的目的不是回答特定的问题,也不一定是提供一个完全重新编制的程序,解决所有潜在的问题,同时向提问者传授所有编程知识。@Vladfrommosco:这些约束是您添加的,不是OP要求的。正如我所说,目的是回答OP提出的具体问题,不是为了解决他们所有的问题,也不是教他们编程的所有知识。这个答案中的代码没有包含
。这个答案中的代码没有检查scanf
的返回值。当输入行超过分配的长度时,这个答案中的代码无法识别,并且它将以静默方式继续到下一行,而不是警告用户。此答案中的代码无法识别输入中包含的行数是否超过分配的数字,并在不警告用户的情况下继续执行−1及−2由于哨兵指示负数不是数组的正确数据,但代码是,此答案无法对负值执行任何错误处理。
for(i=0; i<100; i++) {
for(j=0; j<100; j++)
if(mat1[i][j]==-1)
{
i++; // once the user inputs -1, I increase "i"(next row),
j=0; //and here I set the "column" count to 0
}
i = 0; j = 0;
do
{
int input;
scanf("%d", &input);
if (input == -2)
{
// At the end of the matrix, record the dimensions and leave the loop.
m = i+1;
n = j;
break;
}
else if (input == -1)
{
// At the end of a row, move to the start of the next row.
++i;
j = 0;
}
else
{
// Otherwise, record a single element and move to the next element.
matrix[i][j] = input;
++j;
}
} while (1);