在C ++中将一维非线性转换为二维 [英] Convert One Dimensional Arrary to Two Dimensional in C++
问题描述
我有一个49空格一维数组声明为 int boardArray [49];
和我也有一个二维的7x7数组声明为 int boardArrayTwo [7] [7]'
我想使用嵌套循环将一维数组放入二维数组这里是我用来测试它的代码。
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?
推荐答案
for (int x = 0; x >= 7; ++x)
{
for (int k = 0; k >= 7; ++k){
for (int n = 0; n >= 49; ++n)
{
这是错误的。 x和k应< 7(且不应使用第三周期):
this is wrong. x and k should be < 7 (and the third cycle shouldn't be used) :
for (int x = 0; x < 7; ++x)
{
for (int k = 0; k < 7; ++k){
boardArrayTwo[x][k] = boardArray[7*x + k];
编辑:
like @Fabio Ceconello让我在他的评论中注意到,即使第一个循环是错误的,因为反向条件检查,应该这样修改:
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:
for (int i = 0; i < 49; ++i)
{
boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
这篇关于在C ++中将一维非线性转换为二维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!