Hey can someone help me finish my code? Im in a real stump here.


What Ive done so far is read and display the code from a 2d array, then I counted the neighbours that were dead and then did the process to make a new Generation.

However, I dont think Ive done it completely right. Like, how would I tell the function how many times I need to make the new generation, then display it?

Im so lost :s

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int SIZE = 20;
bool readGrid(string fileName, char grid[SIZE][SIZE]);
void displayGrid(const char grid[SIZE][SIZE]);



int countAlive(char grid[SIZE][SIZE]);

void newGen(char grid[SIZE][SIZE]);




// declare more functions if necessary

int main()
{
    string fileName;        // the name of the grid file
    int nGenerations;    // nGenerations is the number of generations
    char grid[SIZE][SIZE];  // a char array consiting of '*' or '.'
    // declare more variables if necessary
    
    char nuevo[SIZE][SIZE];
   
    cout << "Enter the filename of the grid: " << flush;            // don't modify
    cin >> fileName;                                                // don't modify
    cout << endl;                                                   // don't modify
   
    readGrid(fileName, grid);
    displayGrid(grid);
   
    // add processing for reading and displaying the grid
 
    cout << endl << "How many generations in total? " << flush;     // don't modify
    cin >> nGenerations;                                            // don't modify

    
    // add processing for new grid generations
   
    if (nGenerations == 1)                                                                  // don't modify
      cout << "This is the grid after " << nGenerations << " generation:" << endl << endl;  // don't modify
    else                                                                                    // don't modify
      cout << "This is the grid after " << nGenerations << " generations:" << endl << endl; // don't modify
   


    // display the grid

    cout << endl;            // don't modify
    system("pause");         // don't modify
    return 0;                // don't modify
}

// implement all declared functions

 

 

bool readGrid(string fileName, char grid[SIZE][SIZE])
{
     ifstream inData;
    
     inData.open(fileName.c_str());
    
     if(inData)
     {
     for(int row = 0; row < SIZE; row++)
     {
             for(int col = 0; col < SIZE; col++)
             {
                     inData >> grid[row][col];
             }
             } 
             return true;    } 
             else
             return false;
   
}

 
 


void displayGrid(const char grid[SIZE][SIZE])
{
     for(int row = 0; row < SIZE; row++)
     {
             for(int col = 0; col < SIZE; col++)
             {
                     cout << grid[row][col] << " ";
             }
     cout << endl;
     }
}





 

int countAlive(char grid[SIZE][SIZE])
{
    int neighbour = 0;
   
    for(int row = 1; row < SIZE-1; row++)
    {
            for(int col = 1; col < SIZE-1; col++)   
            {    
                 if(grid[row+1][col+1] == '*') neighbour++;
                 if(grid[row+1][col] == '*') neighbour++;
                 if(grid[row+1][col-1] == '*') neighbour++;
                 if(grid[row][col-1] == '*') neighbour++;
                 if(grid[row][col+1] == '*') neighbour++;
                 if(grid[row-1][col-1] == '*') neighbour++;
                 if(grid[row-1][col] == '*') neighbour++;
                 if(grid[row-1][col+1] == '*') neighbour++;   
            }           
    } 
    
    return neighbour;
        
}

 




void newGen(char grid[SIZE][SIZE])
{
     int neighbour = countAlive(grid);
     char nuevo[SIZE][SIZE];
     
     grid[SIZE][SIZE] = nuevo[SIZE][SIZE];
     
     
     for(int row = 0; row < SIZE; row++)
     {
             for(int col = 0; col < SIZE; col++)
             {
                 grid[row][col] = nuevo[row][col];
                     
                 if(grid[row][col] == '*' && neighbour == 1)
                        nuevo[row][col] == '*';
                 if(grid[row][col] == '.' && neighbour == 1)
                        nuevo[row][col] == '.';
                 if(grid[row][col] == '*' && neighbour == 2)
                        nuevo[row][col] == '*';
                 if(grid[row][col] == '.' && neighbour == 2)
                        nuevo[row][col] == '*';
                 if(grid[row][col] == '*' && neighbour > 2)
                        nuevo[row][col] == '.';
                 if(grid[row][col] == '.' && neighbour > 2)
                        nuevo[row][col] == '.'; 
                 }
     }    
}

Dani AI

Generated

Several problems in the posted code explain the stalled behavior: a global neighbor count is used instead of counting per cell, array-indexing like grid[SIZE][SIZE] is out-of-bounds, == is used where = was intended when copying, and the "nuevo" buffer is used/declared incorrectly. already spotted the indexing/copy issues and added a swap attempt that still used the wrong operators. A simple, robust approach is to keep two buffers (current and next), compute the neighbor count for each cell independently, write the new state into the next buffer, then copy or swap buffers. The standard Life rules are: a live cell survives with 2 or 3 neighbors; a dead cell becomes live with exactly 3 neighbors.

Example (minimal, replace existing buggy functions with these):

int neighbors(const char g[SIZE][SIZE], int r, int c) {
    int n = 0;
    for (int dr = -1; dr <= 1; ++dr)
        for (int dc = -1; dc <= 1; ++dc) {
            if (dr == 0 && dc == 0) continue;
            int rr = r + dr, cc = c + dc;
            if (rr >= 0 && rr < SIZE && cc >= 0 && cc < SIZE)
                n += (g[rr][cc] == '*');
        }
    return n;
}

void step(const char in[SIZE][SIZE], char out[SIZE][SIZE]) {
    for (int r = 0; r < SIZE; ++r)
        for (int c = 0; c < SIZE; ++c) {
            int n = neighbors(in, r, c);
            if (in[r][c] == '*' && (n == 2 || n == 3)) out[r][c] = '*';
            else if (in[r][c] == '.' && n == 3) out[r][c] = '*';
            else out[r][c] = '.';
        }
}

Main-generation loop pattern:

for (int gen = 0; gen < nGenerations; ++gen) {
    step(grid, nuevo);
    memcpy(grid, nuevo, sizeof grid);   // safe because both are SIZE x SIZE chars
}
displayGrid(grid);

Notes: memcpy is fine for fixed-size char arrays; alternatively swap pointers to avoid copying. Ensure all array writes initialize every cell (step does). Keep bounds checks as shown or choose toroidal wrapping explicitly.

Recommended Answers

All 4 Replies

What happens when you run it? I'm guessing you are possibly getting a segmentation fault error in line 122 below due to your attempt to access element grid[SIZE][SIZE] . Arrays in C++ start at 0 and go to SIZE - 1. Or are you trying to do a deep copy of the entire array here? If so, that line isn't going to do it.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int SIZE = 20;
bool readGrid(string fileName, char grid[SIZE][SIZE]);
void displayGrid(const char grid[SIZE][SIZE]);
int countAlive(char grid[SIZE][SIZE]);
void newGen(char grid[SIZE][SIZE]);




// declare more functions if necessary

int main()
{
    string fileName; // the name of the grid file
    int nGenerations; // nGenerations is the number of generations
    char grid[SIZE][SIZE]; // a char array consiting of '*' or '.'
    // declare more variables if necessary

    char nuevo[SIZE][SIZE];

    cout << "Enter the filename of the grid: " << flush; // don't modify
    cin >> fileName; // don't modify
    cout << endl; // don't modify

    readGrid(fileName, grid);
    displayGrid(grid);

    // add processing for reading and displaying the grid

    cout << endl << "How many generations in total? " << flush; // don't modify
    cin >> nGenerations; // don't modify


    // add processing for new grid generations

    if (nGenerations == 1) // don't modify
        cout << "This is the grid after " << nGenerations << " generation:" << endl << endl; // don't modify
    else // don't modify
        cout << "This is the grid after " << nGenerations << " generations:" << endl << endl; // don't modify



    // display the grid

    cout << endl; // don't modify
    system("pause"); // don't modify
    return 0; // don't modify
}

// implement all declared functions


bool readGrid(string fileName, char grid[SIZE][SIZE])
{
    ifstream inData;

    inData.open(fileName.c_str());

    if(inData)
    {
        for(int row = 0; row < SIZE; row++)
        {
            for(int col = 0; col < SIZE; col++)
            {
                inData >> grid[row][col];
            }
        }
        return true;
    }
    else
        return false;
}


void displayGrid(const char grid[SIZE][SIZE])
{
    for(int row = 0; row < SIZE; row++)
    {
        for(int col = 0; col < SIZE; col++)
        {
            cout << grid[row][col] << " ";
        }
        cout << endl;
    }
}


int countAlive(char grid[SIZE][SIZE])
{
    int neighbour = 0;

    for(int row = 1; row < SIZE-1; row++)
    {
        for(int col = 1; col < SIZE-1; col++)
        {
            if(grid[row+1][col+1] == '*') neighbour++;
            if(grid[row+1][col] == '*') neighbour++;
            if(grid[row+1][col-1] == '*') neighbour++;
            if(grid[row][col-1] == '*') neighbour++;
            if(grid[row][col+1] == '*') neighbour++;
            if(grid[row-1][col-1] == '*') neighbour++;
            if(grid[row-1][col] == '*') neighbour++;
            if(grid[row-1][col+1] == '*') neighbour++;
        }
    }

    return neighbour;
}


void newGen(char grid[SIZE][SIZE])
{
    int neighbour = countAlive(grid);
    char nuevo[SIZE][SIZE];

    grid[SIZE][SIZE] = nuevo[SIZE][SIZE];


    for(int row = 0; row < SIZE; row++)
    {
        for(int col = 0; col < SIZE; col++)
        {
            grid[row][col] = nuevo[row][col];

            if(grid[row][col] == '*' && neighbour == 1)
                nuevo[row][col] == '*';
            if(grid[row][col] == '.' && neighbour == 1)
                nuevo[row][col] == '.';
            if(grid[row][col] == '*' && neighbour == 2)
                nuevo[row][col] == '*';
            if(grid[row][col] == '.' && neighbour == 2)
                nuevo[row][col] == '*';
            if(grid[row][col] == '*' && neighbour > 2)
                nuevo[row][col] == '.';
            if(grid[row][col] == '.' && neighbour > 2)
                nuevo[row][col] == '.';
        }
    }
}

What happens when you run it? I'm guessing you are possibly getting a segmentation fault error in line 122 below due to your attempt to access element grid[SIZE][SIZE] . Arrays in C++ start at 0 and go to SIZE - 1. Or are you trying to do a deep copy of the entire array here? If so, that line isn't going to do it.

Well in that function im making a new generation, so I tried to copy it to another array inside the function. I guess Ill have to try something else then.

I think my main problem is I dont know how to tell it how many times to make a new generation, like once, twice or up to eight times. And then how to display the new grid. Wah, lol.....

Ok well I added a new function to swap the arrays, I think that is better now :s

void swapArray(char grid[SIZE][SIZE], char nuevo[SIZE][SIZE])
{
     for(int row = 0; row < SIZE; row++)
     {
             for(int col = 0; col < SIZE; col++)
             {
                     grid[row][col] ==  nuevo[row][col];
             }   
     }     
}     







void newGen(char grid[SIZE][SIZE])
{
     int neighbour = countAlive(grid);
     char nuevo[row][col] = swapArray(grid, nuevo);
     
     for(int row = 0; row < SIZE; row++)
     {
             for(int col = 0; col < SIZE; col++)
             {                     
                 if(grid[row][col] == '*' && neighbour == 1)
                        nuevo[row][col] == '*';
                 if(grid[row][col] == '.' && neighbour == 1)
                        nuevo[row][col] == '.';
                 if(grid[row][col] == '*' && neighbour == 2)
                        nuevo[row][col] == '*';
                 if(grid[row][col] == '.' && neighbour == 2)
                        nuevo[row][col] == '*';
                 if(grid[row][col] == '*' && neighbour > 2)
                        nuevo[row][col] == '.';
                 if(grid[row][col] == '.' && neighbour > 2)
                        nuevo[row][col] == '.';    
                        
                 }   
                 
     }

Ok well I added a new function to swap the arrays, I think that is better now :s

void swapArray(char grid[SIZE][SIZE], char nuevo[SIZE][SIZE])
{
     for(int row = 0; row < SIZE; row++)
     {
             for(int col = 0; col < SIZE; col++)
             {
                     grid[row][col] ==  nuevo[row][col];
             }   
     }     
}

Close. Change the == to = . Also, which one is the source array and which one is the destination array? What are you copying into what?


void newGen(char grid[SIZE][SIZE])
{
     int neighbour = countAlive(grid);
     char nuevo[row][col] = swapArray(grid, nuevo);
     
     for(int row = 0; row < SIZE; row++)
     {
             for(int col = 0; col < SIZE; col++)
             {                     
                 if(grid[row][col] == '*' && neighbour == 1)
                        nuevo[row][col] == '*';
                 if(grid[row][col] == '.' && neighbour == 1)
                        nuevo[row][col] == '.';
                 if(grid[row][col] == '*' && neighbour == 2)
                        nuevo[row][col] == '*';
                 if(grid[row][col] == '.' && neighbour == 2)
                        nuevo[row][col] == '*';
                 if(grid[row][col] == '*' && neighbour > 2)
                        nuevo[row][col] == '.';
                 if(grid[row][col] == '.' && neighbour > 2)
                        nuevo[row][col] == '.';    
                        
                 }   
                 
     }

Line 4 above. Be careful. Are you declaring a new array called nuevo? if so, what is the size? Is it supposed to be SIZE x SIZE or row x col? Also, do it in two separate lines:

char nuevo[?][?];
swapArray(grid, nuevo);  // make sure this order matches the order
                         // in swapArray and that you are copying
                         // the correct array into the correct array

Edit : To clarify, are you trying to SWAP the arrays or COPY one array into the other?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.