I am trying (unsuccessfully!) to code the game of life for an assignment, I am very new to C programming and am finding it very difficult.

I am at the part where the program needs to count how many cells are live/dead and I thought it would be a good idea to surround the initial matrix with 0's in order to count the dead/live cells at the edge.

can anyone help me do this?

My initial matrix is generated as below, after many user inputs:

printf("Initial matrix :\n");
	for (i=1; i<=n; i++)
	{
	for (j=1; j<=p; j++)
	{
/*Begins loop to fill the matrix with 0's and 1's*/
	
	v[i][j] = rand()%2;
	printf("%d\t", v[i][j]);
	}
/*Prints the matrix, 1's for live cells and 0 for dead cells, with the title initial matrix*/
	printf("\n");
	}

Thanks in advance.

Recommended Answers

All 4 Replies

I have succeeded by using

for(j=0;j<=p+1;j++)
        v[0][j]=v[p+1][j]=0;

    for(i=0;i<=n;i++)
        v[i][0]=v[i][n+1]=0;

A better idea, why not just include edge checking code when you count the neighbors? An if statement or two with a simple little condition(s). That way you don't have to start your array at index '1', therefore making it much simpler (especially to someone reading your code, ie. teacher).

I'm not sure how adding if statements for edge checking makes the code simpler. In my experience, additional code always complicates, rarely simplifies, readability.

But it is an alternative to adding a 0 border around the main grid. To me, edge checking would make more sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.