开发者

Convert One Dimensional Arrary to Two Dimensional in C++

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 ar开发者_如何学编程ray 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)
    {

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];

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:

for (int i = 0; i < 49; ++i)
{
    boardArray[i] = i; //fills the array with ints 0 - 48 to test
}


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:

boardArrayTwo[x][k] = boardArray[x * 7 + k];

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:

#define arrlen(x) (sizeof(x)/sizeof((x)[0]))

for (int i = 0; i < arrlen(boardArray); ++i)
{
    boardArray[i] = i; 
}
int stride = arrlen(boardArrayTwo[0]);
for (int x = 0; x < arrlen(boardArrayTwo); ++x)
{
    for (int k = 0; k < stride; ++k)
    {
        boardArrayTwo[x][k] = boardArray[stride * x + k];
        cout << boardArrayTwo[x][k] << " " << endl;
    }
}

caveat: if the arrays aren't declared here (were passed as parameters), arrlen() won't work. But that's another long story...


It looks like your destination array is in row-major order. You could just blast the source array directly into place.

memcpy(boardArrayTwo, boardArray, 49 * sizeof(int));

or if you prefer something in more idiomatic C++:

std::copy(boardArray, boardArray + 49, reinterpret_cast<int*>(boardArrayTwo));


You used i > 50 in your for loop. It should be i < 49 and same for all the other loops.

Also, this won't work. You're setting all of the boardArrayTwo[][] values to boardArray[49] You should instead do something like this:

for (int x = 0; x < 7; ++x)
{
    for (int k = 0; k < 7; ++k)
    {
        boardArrayTwo[x][k] = boardArray[7*x + k];
        cout << boardArrayTwo[x][k] << " " << endl;
    }
}

or

int count = 0;

for (int x = 0; x < 7; ++x)
{
    for (int k = 0; k < 7; ++k)
    {
        boardArrayTwo[x][k] = boardArray[count];
        cout << boardArrayTwo[x][k] << " " << endl;
        count++;
    }
}


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 through x and k, then copy the corresponding element from boardArray to boardArrayTwo.

You could do one of these:

int n = 0;
for (int x = 0; x < 7; ++x)
    for (int k = 0; k < 7; ++k)
    {
        boardArrayTwo[x][k] = boardArray[n];
        ++n;
    }

or use a formula to calculate the proper n:

for (int x = 0; x < 7; ++x)
    for (int k = 0; k < 7; ++k)
        boardArrayTwo[x][k] = boardArray[x*7+k];

I wrote x*7+k because it seems like x is iterating over the rows of the array, each row having 7 elements, says that x*7+kth element of the boardArray represents position [x][k] of boardArrayTwo/


Note

for (int i = 0; i > 50; ++i)

if i is initialized to 0, it won't be greater than 50 and thus it will never enter the loop.


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 to boardArrayTwo[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.

#include <iostream>
using std::cout;
using std::endl;

int main () {

    int boardArray[49];
    int boardArrayTwo[7][7];

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)
    {
            boardArrayTwo[x][k] = boardArray[x*7 + k];
            cout << boardArrayTwo[x][k] << " " << endl;
    }
}

}

With any luck (unless I am embarrassing myself) this should do the trick!

EDIT: Special thanks to Fabio!


for(int i=0; i<49; i++)
b[i]=(i+1);

int p=0;
for(int i=0;i<7;i++){
        for(int j=0;j<7;j++)
        {a[i][j]=b[p];
        p++;}
        }

beside other errors, third loop is making your code wrong

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜