I don't know what the issue is in my code. In genereation doesn't change anything. Can you please fix this. Thank you    #include #include #include using namespace std;   // Global constants   const int MAXGEN = 5; // maxi nb of generations const int n = 10;     // nb of rows const int m = 10;     // nb of colums     // function prototypes   void initialize ( int grid [][m]); void initialize ( int grid [][m], int density); int countNeighbours ( int grid [][m], int x, int y); bool allDead ( int grid[][m]); void reproduce ( int grid[][m]); void print (int grid [][m]);   // function to initialize the grid with user input void initialize( int grid [][m]){     cout<< "Enter the initial population ( 0 for dead , 1 for alive ): \n";     for (int i = 0; i < n; i++){         for( int j = 0; j < m; j++){             cin >> grid [i][j];         }     } } // function to initialiize the grid with random values based on density void initialize( int grid [][m], int density){     for (int i = 0; i < n; i++){         for( int j = 0; j 3) ? 0 : 1;             }else {                 nextGen [i][j] = ( neighbors == 3) ? 1: 0;             }         }     }     //update the original grid with the next generation     for ( int i = 0; i> density;          int grid [n][m];        cout << "Initial population: \n";     print(grid);          int gen = 1;     while ( gen <= MAXGEN && !allDead(grid)){         cout<< "gen = "<< gen << ":\n";         reproduce(grid);         print(grid);         gen++;     }     return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I don't know what the issue is in my code. In genereation doesn't change anything. Can you please fix this. Thank you 

 

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

 

// Global constants

 

const int MAXGEN = 5; // maxi nb of generations

const int n = 10;     // nb of rows

const int m = 10;     // nb of colums

 

 

// function prototypes

 

void initialize ( int grid [][m]);

void initialize ( int grid [][m], int density);

int countNeighbours ( int grid [][m], int x, int y);

bool allDead ( int grid[][m]);

void reproduce ( int grid[][m]);

void print (int grid [][m]);

 

// function to initialize the grid with user input

void initialize( int grid [][m]){

    cout<< "Enter the initial population ( 0 for dead , 1 for alive ): \n";

    for (int i = 0; i < n; i++){

        for( int j = 0; j < m; j++){

            cin >> grid [i][j];

        }

    }

}

// function to initialiize the grid with random values based on density

void initialize( int grid [][m], int density){

    for (int i = 0; i < n; i++){

        for( int j = 0; j<m; j++){

            int randomNum = rand() % 100 + 1 ;  // generate a random nb between 1 and  100

            grid[i][j] =  ( randomNum <= density) ? 1: 0; // cell is alive if randomNum is less thabn or equal to density

        }

    }

}

//function to count the number of live neighbors for a given cell

int countNeighbours(int grid[][m], int x, int y){

    int count = 0;

    for (int i = x - 1; i <= x + 1; i++){

        for (int j = y - 1; j <= y + 1; j++){

            if ( i == x && j == y) continue; // skip the current cell

            int row = (i + n) % n; // handle wrappy around for rows

            int col = ( j + m ) % m; // handle wrapping around for columns

            count += grid[row][col];

            

        }

    }

    return count;

}

//function to check if all cells are dead

bool allDead ( int grid [][m]){

    for (int i = 0; i<n ; i++){

        for (int j = 0; j<m; j++){

            if ( grid [i][j] == 1){

                return false; // if any cell is alive , return false

            }

        }

    }

    return true; // all cells are dead

}

 

void reproduce ( int grid [][m]){

    int nextGen[n][m];

    

    for (int i = 0; i < n ; i++){

        for ( int j = 0; j<m ; j++){

            int neighbors = countNeighbours(grid, i, j);

           

            if ( grid [i][j] == 1){

                nextGen[i][j] = ( neighbors < 2 || neighbors > 3) ? 0 : 1;

            }else {

                nextGen [i][j] = ( neighbors == 3) ? 1: 0;

            }

        }

    }

    //update the original grid with the next generation

    for ( int i = 0; i<n; i++){

        for (int j = 0; j< m; j++){

            grid[i][j] = nextGen[i][j];

            

        }

    }

}

//function to print the current state of the grid

void print (int grid [][m]){

    for ( int i = 0; i<n; i++){

        for (int j = 0; j < m ; j++){

            cout<< (grid[i][j] == 1 ? '1' : '0')<< " ";

        }

        cout << '\n';

    }

    cout << '\n';

}

 

 

int main(){

    srand(time(0)); // for random number generation

    

    int density;

    cout << "Enter the density out of 100% : ";

    cin >> density;

    

    int grid [n][m];

  

    cout << "Initial population: \n";

    print(grid);

    

    int gen = 1;

    while ( gen <= MAXGEN && !allDead(grid)){

        cout<< "gen = "<< gen << ":\n";

        reproduce(grid);

        print(grid);

        gen++;

    }

    return 0;

}

 

 

 

 

 

 

 

Expert Solution
steps

Step by step

Solved in 4 steps with 7 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education