I have 3 errors on my game of life program:
2 of the sort "a function- definition is not allowed before '{' token "
1 error expected '}' at end of input
I would like some insights on what I should modify
You're missing a few closing } so the compiler thinks you're trying to define a function within a function.
If you can edit your post, highlight the code part, then click on the <> button in the Format palette at the right side of the post, that will format your code on the forum and add line numbers, making it easier to read.
A few other things.
1 2 3 4
if(neighbors == 3)
{
grid[i][j] == 0; // I think you mean to use assignment =, not equality == here?
}
1 2 3
void print(int grid[][m]){//<- you need a number, not variable m herefor(int i=0;i<2;i++){ //Print Generation
1 2 3 4 5 6 7 8 9
int main(){
// Global constants
constint MAXGEN = 3;
constint n=2 ;
constint m=3 ;
int grid[n][m];
initialize(grid);
gen = 1;// <- gen is undefined here
#include<iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace std;
//Nicolas Ningaba
//The program is called the game of life
//it simulates the life of a cell and its generations
int initialize(int grid[][3]){
int z,z2;
cout<<"Enter two numbers for a population ";
cin>>z>>z2;
cout<<"Enter the population as 0(for dead) and 1(live)";
for(int i=0;i<z;i++)
for(int j=0;j<z2;j++)
cin>>grid[i][j];
} // initialize the nxm population
int initialize2(int grid[][3], int density){ // initialize given a density
int alternative=0;\\number of cells initialized given a density
srand (time(NULL));
int r=rand()%100 + 1;
int r2=rand()%100 + 1;
for(int i=0;i<r;i++){
for (int j=0;j<r2;j++){
if (density<= 0.30*r)\\if the density is less than 30% the cell lives
grid[i][j]=1;
alternative++;\\increment those that live
if (density> 0.30*r)\\if the density is greater than 30% the cell dies
grid[i][j]=0;
alternative++;\\increment those that die
}
}
return alternative;
}
int countNeighbours (int grid[][3], int x, int y){// counts live neighbours of cell x,y
int num_neighbors=0;
if ((grid[x-1][y-1])==0)
num_neighbors++;
if ((grid[x][y-1])==0)
num_neighbors++;
if ((grid[x][y+1])==0)
num_neighbors++;
if ((grid[x-1][y])==0)
num_neighbors++;
if ((grid[x+1][y-1])==0)
num_neighbors++;
if ((grid[x+1][y])==0)
num_neighbors++;
if ((grid[x+1][y+1])==0)
num_neighbors++;
return num_neighbors;
}
bool allDead(int grid[][3]){// checks if the population is dead
int neighbors = 0;
bool living=false;
for(int i= 1; i<2; i++)
{
for(int j = 1; j<3; j++)
{
if(grid[i][j] == 1)
{
if(grid[i- 1][j - 1] == 1)
living=true;
neighbors++;
if(grid[i - 1][j] == 1)
living=true;
neighbors++;
if(grid[i - 1][j + 1] == 1)
living=true;
neighbors++;
if(grid[i][j - 1] == 1)
living=true;
neighbors++;
if(grid[i][j + 1] == 1)
living=true;
neighbors++;
if(grid[i+ 1][j - 1] == 1)
living=true;
neighbors++;
if(grid[i + 1][j] == 1)
living=true;
neighbors++;
if(grid[i + 1][j + 1] == 1)
living=true;
neighbors++;
if(neighbors < 2 || neighbors > 4)
{
living=false;
grid[i][j] = 0;
}
}
}
}
if(living)
returntrue;
returnfalse;
}
int reproduce(int grid[][3]){// produce the next generation
int neighbors = 0;
for(int i = 1; i<2; i++)
{
for(int j= 1; j<3; j++)
{
if(grid[i][j]== 0)
{
if(grid[i - 1][j - 1] == 1)
{
neighbors++;
}
if(grid[i - 1][j] == 1)
{
neighbors++;
}
if(grid[i - 1][j + 1] == 1)
{
neighbors++;
}
if(grid[i][j - 1] == 1)
{
neighbors++;
}
if(grid[i][j + 1] == 1)
{
neighbors++;
}
if(grid[i+ 1][j - 1] == 1)
{
neighbors++;
}
if(grid[i + 1][j] == 1)
{
neighbors++;
}
if(grid[i + 1][j + 1] == 1)
{
neighbors++;
}
if(neighbors == 3)
{
grid[i][j] = 0;
}
}
return neighbors;
}
}
void print(int grid[][3]){//Print Generation
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
cout << setw(4)<<grid[i][j]<<" ";
cout << endl;
}
}
}
int main(){
// Global constants
constint MAXGEN = 3; // maximum no. of generations
constint n=2 ; // number of rows
constint m=3 ; // number of columns
int grid[n][m];
initialize(grid);
int gen = 1;
print (grid);
while (gen <= MAXGEN && !allDead(grid)){
cout << "gen = " << gen;
reproduce(grid); // will call the function countNeighbours for each cell
print (grid);
gen++;
}
}