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; }
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;
}
Step by step
Solved in 4 steps with 7 images