the game of life

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include<iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
//Nicolas Ningaba
//The program is called the game of life
//it simulates the life of a cell and its gegerations
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;
        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)
                grid[i][j]=1;
                alternative++;
             if  (density> 0.30*r)
               grid[i][j]=0;
                alternative++;

        }
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)
      return true;
      return false;

}
int reproduce(int grid[][3]){


      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;
} 				// produce the next generation
void print(int grid[][m]){

    	for(int i=0;i<2;i++){ //Print Generation
		for(int j=0;j<3;j++)
        cout << setw(4)<<grid[i][j]<<" ";
        cout << endl


		}
		;
}
				// print a population


  int main(){
      // Global constants
    const int MAXGEN = 3; // maximum no. of generations
    const int n=2 ;    // number of rows
    const int m=3 ;    // number of columns

    int grid[n][m];
    initialize(grid);
    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++;}
  }
Last edited on
1. Put your code between code tags please!
2. I don't think anyone will help you unless you organize your question properly and clearly.

Good luck with your program.
Last edited on
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 here

for(int i=0;i<2;i++){ //Print Generation 


1
2
3
4
5
6
7
8
9
int main(){
// Global constants
const int MAXGEN = 3;
const int n=2 ; 
const int m=3 ; 

int grid[n][m];
initialize(grid);
gen = 1;// <- gen is undefined here 
Last edited on
thanks for the insight , here are the modifications but i'm still getting a similar
at the void print


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include<iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace 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)
      return true;
      return false;

}
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
    const int MAXGEN = 3; // maximum no. of generations
    const int n=2 ;    // number of rows
    const int 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++;
  }
      }

Last edited on
You're missing closing braces in the previous function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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;
                }
            }//end if
        
        return neighbors;
        }//end inner for
    }//end outer for
    
void print(int grid[][3]){//Print Generation 
thanks man
Topic archived. No new replies allowed.