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] == '.'; 
                 }
     }    
}

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.