Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need some help going from here. I am not sure if my countNeighbour Function would work. And how would I start my reproduce function? #include

Need some help going from here. I am not sure if my countNeighbour Function would work. And how would I start my reproduce function?

#include

using namespace std;

// GLOBAL CONSTANTS

const int MAXGEN = 3; const int n = 3; // rows const int m = 4; // columns

void initialize(int grid[][m]){ cout << "Enter 0 for dead, 1 for alive" << endl; for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){

cin >> grid[i][j]; }

}

} void initialize2(){

}

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

//grid[x][y] would represent current cell. Need to check all round that cell. Using modular for wrap.

int counter = 0;

//(x+n)%n and (y+m)%m to get wrap around

// y> if (grid[((x+n)%n)][(((y-1)+m)%m)] == 1) //one to left // 0 0 1 2 counter++; // 1 ^x 3 4 5 if (grid[(((x-1)+n)%n)][(((y-1)+m)%m)] == 1) //up one to left // 2 6 7 8 counter++; if (grid[(((x-1)+n)%n)][((y+m)%m)] == 1) // up one counter++; if (grid[(((x-1)+n)%n)][((y+1)+m)%m]==1) //up one to right x= 1 y = 2 counter++; if (grid[((x+n)%n)][(((y+1)+m)%m)]==1) // one to right counter++; if (grid[(((x+1)+n)%n)][(((y+1)+m)%m)]==1) // down one to right counter++; if (grid[(((x+1)+n)%n)][((y+m)%m)]==1) // down one counter++; if (grid[(((x+1)+n)%n)][(((y-1)+m)%m)]==1) // down one to left counter++;

return counter; // counter represents total number of Alive cells }

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

void reproduce(int grid[][m]){ int x,y;

for (int i =0; i< countNeighbours(grid[n][m],x,y); i++)

}

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

for(int i=0; i

cout << grid[i][j]; } cout << endl;

} cout << endl; }

int main(){

int grid[n][m]; initialize(grid); cout << "Initial population = "; print(grid); int gen = 1;

while (gen <= MAXGEN && !allDead(grid)){ cout << "gen = " << gen << ": " << endl; reproduce(grid); //will call the function countNeighbours for each cell print(grid); gen++; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Distinguish between merit pay, bonus, spot bonuses, and piecework.

Answered: 1 week ago