CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Mar 2008
    Location
    USA
    Posts
    12

    [RESOLVED] 2D arrays help (involves copying elements)

    Hello:
    I am new to the forum. I am doing an assignment involving 2D arrays called the Game of Life. I have to create a 2D array that is initialized with specific values of 0 or 1. Then I have to check each element to determine if it will stay a zero or one, or if it will become a one (based on certain rules). After I determine this, I have to create a new array and store the results into it. I have already created this array. However, when I print the new array, it gives me the same configuration as the first array. I'm not sure what I'm doing wrong.

    Here's the original array's initialization:
    Code:
    int life[12][12] = { {0,0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,1,0,1,0,1,0,0,0},
    {0,0,0,1,0,1,0,1,0,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0,0},
    {0,0,0,0,1,0,1,0,1,0,0,0},
    {0,0,0,1,0,1,0,1,0,0,0,0},
    {0,1,1,1,1,1,1,1,1,1,1,0},
    {0,0,0,0,1,1,1,1,0,0,0,0},
    {0,0,0,0,1,0,1,0,1,0,0,0},
    {0,0,0,1,0,1,0,1,0,0,0,0},
    {0,0,0,0,1,0,1,0,1,0,0,0},
    {0,0,0,0,0,0,0,0,0,0,0,0} };
    Here's the code that I have so far:
    Code:
    void neighbors(const int array[][12])
    {
     
    int count = 0,
    row,
    col,
    a,
    b,
    newLife[12][12] = {};
     
    for (row = 1; row < 11; row++)
    {
    for (col = 1; col < 11; col++)
    { 
    for (a = -1; a < 2; a++)
    {
    for (b = -1; b < 2; b++)
    {
    if (array[row + a][col + b] == 1)
    count++; 
    }
    }
     
    if (array[row][col] == 1)
    count--;
     
    if (array[row][col] == 0 && count == 3)
    newLife[row][col] == 1;
    else if (count >= 4 || count == 1 || count == 0)
    newLife[row][col] == 0;
    else
    newLife[row][col] == 1;
     
    count = 0;
    }
    }
     
    for (row = 0; row < 12; row++)
    {
    for (col = 0; col < 12; col++)
    {
    cout << setw(3) << newLife[row][col];
     
    if (((col + 1) % 12) == 0)
    cout << endl; 
    } 
    }
     
    }
    Please help because I can't see what I'm obviously doing wrong.
    If you need more info, just let me know.

    Thanks in advance
    Last edited by evapisces; March 14th, 2008 at 10:08 AM. Reason: code tags added

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured