Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rules: an organism in cell (i,j) survives to the next evolution if the number of

ID: 3630788 • Letter: R

Question

Rules: an organism in cell (i,j) survives to the next evolution if the number of occupied cells adjacent to the cell(i,j) is either 2 or 3, otherwise it dies. An organism is born in an empty cell if and only if the number of occupied cells adjacent to cell(i,j) is equal to 3, otherwise it remains empty.

Write a program that reads an initial configuration of occupied cells for a 7x7 grid, calculates the next five evolution's according to the above rules, and prints the population after each evolution.

Instructions: Use the two dimensional array grid[7][7] to represent the grid. If the (i,j) cell of the grid is occupied, then set grid[i][j]=1. If the (i,j) cell of the grid is unoccupied, then set grid[i][j]=0. Use the function printgrid to print out the grid array at every evolution.

The initial population looks like this

...0 1 2 3 4 5 6 
0| | | | | | | |
1| | |*| |*| | |
2| | |*| |*| | |
3| | |*|*|*| | |
4| | | | | | | | 
5| | | | | | | | 
6| | | | | | | | 

 

And the 1st evolution should look like this

...0 1 2 3 4 5 6 
0| | | | | | | |
1| | | | | | | |
2| |*|*| |*|*| |
3| | |*| |*| | |
4| | | |*| | | | 
5| | | | | | | | 
6| | | | | | | | 



I need an output showing the next five evolutions. The function printgrid was given by the professor. The function neighbors was used to find out how many of the cell(i,j) neighbors were occupied. Then I tried to say that if the grid(i,j) was equal to 1 and occupied_neighbors was equal to 2 then grid(i,j) was equal to 1. Also if grid(i,j) was equal to 0 or 1 and occupied_neighbors was equal to 3 then grid(i,j) was also equal to one. Anything else meant grid(i,j) was equal to 0. I don't know if I did this correctly and I am completely stuck. Please help me? I feel like I have written the bulk of it, I just need a little help. This is what I have written so far.

#include <stdio.h>

#define size 7

int grid[7][7];

void main ()
{
int i, j, n, occupied_neighbors;
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
grid[i][j] = 0;

grid[1][2] = 1;
grid[1][4] = 1;
grid[2][2] = 1;
grid[2][4] = 1;
grid[3][2] = 1;
grid[3][3] = 1;
grid[3][4] = 1;

for (i = 0; i < size; i++)
for (j = 0; j < size; j++) {
n = neighbors(i,j);
}

while (i <= 6, j <= 6) {
if (grid[i][j] == 1 && occupied_neighbors == 2)
grid[i][j] = 1;
else if (grid[i][j] == 1 || 0 && occupied_neighbors == 3)
grid[i][j] = 1;
else
grid[i][j] = 0;

}

return;
}


void printgrid ()
{
int i, j;

printf(" ");
for (i = 0; i < size; i++)
printf (" %d",i);
printf (" ");

for (i = 0; i < size; i++) {
printf (" %d",i);
for (j = 0; j < size; j++) {

if (grid[i][j] == 1)
printf ("|*");
else
printf ("| ");
}
printf ("| ");
}

printf(" ");
return;
}

int neighbors (int i, int j)
{
int row, col, row_start, row_end, col_start, col_end, occupied_neighbors;
if (i == 0)
row_start = i;
else row_start = i - 1;
if (i == size - 1)
row_end = i;
else row_end = i + 1;
if(j == 0)
col_start = j;
else col_start = j - 1;
if (j == size - 1)
col_end = j;
else col_end = j + 1;
occupied_neighbors = 0;
for(row = row_start; row <= row_end; row++)
for(col = col_start; col <= col_end; col++)
if (grid[row][col] == 1)
occupied_neighbors = occupied_neighbors + 1;
occupied_neighbors = occupied_neighbors - grid[i][j];
return(occupied_neighbors);
}




Explanation / Answer

#include <stdio.h>

#define size 7

int grid[7][7];

int neighbors (int i, int j);

void printgrid ();

void main ()

{

int i, j, n,k;

int grid2[size][size];

for (i = 0; i < size; i++)

for (j = 0; j < size; j++)

{

grid[i][j] = 0;

grid2[i][j] = 0;

}

grid[1][2] = 1;

grid[1][4] = 1;

grid[2][2] = 1;

grid[2][4] = 1;

grid[3][2] = 1;

grid[3][3] = 1;

grid[3][4] = 1;

printgrid();

for(k =0;k<5;k++)

{

for (i = 0; i < size; i++)

for (j = 0; j < size; j++) {

if (grid[i][j] == 1 && neighbors(i,j) == 2)

grid2[i][j] = 1;

else if ((grid[i][j] == 1 || grid[i][j] == 0) && neighbors(i,j) == 3)

grid2[i][j] = 1;

else

grid2[i][j] = 0;

}

for (i = 0; i < size; i++)

for (j = 0; j < size; j++)

{

grid[i][j] = grid2[i][j];

}

printgrid();

}

return ;

}

void printgrid ()

{

int i, j;

printf(" ");

for (i = 0; i < size; i++)

printf (" %d",i);

printf (" ");

for (i = 0; i < size; i++) {

printf (" %d",i);

for (j = 0; j < size; j++) {

if (grid[i][j] == 1)

printf ("|*");

else

printf ("| ");

}

printf ("| ");

}

printf(" ");

return;

}

int neighbors (int i, int j)

{

int row, col, row_start, row_end, col_start, col_end, occupied_neighbors;

if (i == 0)

row_start = i;

else row_start = i - 1;

if (i == size - 1)

row_end = i;

else row_end = i + 1;

if(j == 0)

col_start = j;

else col_start = j - 1;

if (j == size - 1)

col_end = j;

else col_end = j + 1;

occupied_neighbors = 0;

for(row = row_start; row <= row_end; row++)

for(col = col_start; col <= col_end; col++)

if (grid[row][col] == 1)

occupied_neighbors = occupied_neighbors + 1;

occupied_neighbors = occupied_neighbors - grid[i][j];

return(occupied_neighbors);

}