在 C++ 中将一维数组转换为二维数组
我有一个 49 空间的一维数组,声明为 int boardArray [49];
,还有一个二维 7x7 数组,声明为 int boardArrayTwo [7][7]'
我试图使用嵌套的 for 循环将一维数组放入二维数组中,这是我用来测试它的代码。
for (int i = 0; i > 50; ++i)
{
boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
for (int x = 0; x >= 7; ++x)
{
for (int k = 0; k >= 7; ++k)
{
for (int n = 0; n >= 49; ++n)
{
boardArrayTwo[x][k] = boardArray[n];
cout << boardArrayTwo[x][k] << " " << endl;
}
}
}
我尝试运行这个但没有任何反应。我做错了吗?
I have a 49 space one dimensional array declared as int boardArray [49];
and I also have a two dimensional 7x7 array declared as int boardArrayTwo [7][7]'
I am trying to use nested for loops to throw the one dimensional array into the two dimensional array here is the code I am using to test it.
for (int i = 0; i > 50; ++i)
{
boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
for (int x = 0; x >= 7; ++x)
{
for (int k = 0; k >= 7; ++k)
{
for (int n = 0; n >= 49; ++n)
{
boardArrayTwo[x][k] = boardArray[n];
cout << boardArrayTwo[x][k] << " " << endl;
}
}
}
I tried running this but nothing happens. Am I doing it wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是错误的。 x 和 k 应该 < 7(并且不应该使用第三个循环):
编辑:
就像@Fabio Ceconello让我在他的评论中注意到,即使第一个循环由于反向条件检查而错误,也应该这样修改:
this is wrong. x and k should be < 7 (and the third cycle shouldn't be used) :
EDIT:
like @Fabio Ceconello make me notice in his comment, even the first loop is wrong because of the inverted condition checks, it should be modified this way:
除了循环中的反转逻辑(其他人提到的)之外,不需要第三个内部循环。只需将归因放在第二个内部循环中即可:
编辑:
我还应该提到,所有这些文字都不是好的做法,我在上面又添加了一个 (7)。我将重写代码如下:
警告:如果此处未声明数组(作为参数传递),则 arrlen() 将不起作用。但这是另一个很长的故事了......
Apart from the inverted logic in the loops (which the others mentioned), there's no need for the third inner loop. Just put the attribution in the second inner loop:
EDIT:
I should also mention that all these literals aren't good practice, and I added one more (7) above. I'd rewrite the code as follows:
caveat: if the arrays aren't declared here (were passed as parameters), arrlen() won't work. But that's another long story...
看起来您的目标数组是按行优先顺序排列的。您可以直接将源阵列爆炸到位。
或者如果您更喜欢更惯用的 C++ 语言:
It looks like your destination array is in row-major order. You could just blast the source array directly into place.
or if you prefer something in more idiomatic C++:
您使用了
i >; 50
在你的 for 循环中。应该是i < 49
和所有其他循环相同。另外,这行不通。您将所有
boardArrayTwo[][]
值设置为boardArray[49]
您应该执行如下操作:或
You used
i > 50
in your for loop. It should bei < 49
and same for all the other loops.Also, this won't work. You're setting all of the
boardArrayTwo[][]
values toboardArray[49]
You should instead do something like this:or
首先,for 循环中的第二项表示当条件为 true 时 for 循环将运行。因此,对于所有循环,您应该使用
<
而不是>=
。其次,
n
上的循环是额外的,不应该存在。您需要的是遍历x
和k
,然后将相应的元素从boardArray
复制到boardArrayTwo
。您可以执行以下操作之一:
或使用公式来计算正确的
n
:我写了
x*7+k
因为它看起来像x
迭代数组的行,每行有 7 个元素,表示 boardArray 的第x*7+k
元素表示的位置[x][k]
boardArrayTwo/First of all, the second term in the
for
loop says the for loop would run while that condition is true. So you should use<
instead of>=
for all your loops.Second, the loop over
n
is extra and shouldn't be there. What you need is to go throughx
andk
, then copy the corresponding element fromboardArray
toboardArrayTwo
.You could do one of these:
or use a formula to calculate the proper
n
:I wrote
x*7+k
because it seems likex
is iterating over the rows of the array, each row having 7 elements, says thatx*7+k
th element of the boardArray represents position[x][k]
of boardArrayTwo/请注意,
如果
i
初始化为0,它不会大于50
,因此它永远不会进入循环。Note
if
i
is initialized to 0, it won't be greater than50
and thus it will never enter the loop.在每个循环中,您使用大于或等于 (>),而不是小于 (<) 或等于。您还应该注意到,正如 Fabio 上面指出的那样,第三个嵌套循环将 boardArrayTwo[x][k] 一遍又一遍地设置为 0-49,共 49 次。您需要使用算术来操作 x 和 k,以便它们成为 boardArray 的索引,然后将该索引分配给
boardArrayTwo[x][k]
。使用 0..7(含)也很重要,这实际上是 8 个位置。你的数组的长度只有 7,所以你实际上最终会得到一些垃圾值。
运气好的话(除非我让自己难堪)这应该可以解决问题!
编辑:特别感谢法比奥!
In each of your loops you used greater than or equal (>) to rather than less than (<) or equal to. You should also notice that, as Fabio points out above, the third nested loop is setting
boardArrayTwo[x][k]
to 0-49 over and over again, 49 times. You will need to use arithmetic to manipulate x and k so that they will be an index into boardArray, and then assign that index toboardArrayTwo[x][k]
.It's also important that you are using 0..7 inclusive, which is actually 8 positions. Your array are only of length 7 so you are actually ending up with some garbage values in there.
With any luck (unless I am embarrassing myself) this should do the trick!
EDIT: Special thanks to Fabio!