Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 238
Cistoran Avatar asked Sep 20 '11 23:09

Cistoran


1 Answers

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
}
like image 91
Heisenbug Avatar answered Oct 12 '22 08:10

Heisenbug