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

*NOTE MUST BE DONE IN C* Explain code. Program Conway\'s Game of Life using a N

ID: 3783056 • Letter: #

Question

*NOTE MUST BE DONE IN C* Explain code.

Program Conway's Game of Life using a N times N array. The starting state of the board will be determined by one of the initialization subroutines. Leave the borders of the board empty and do not bother to update the borders of the board. A #define for N. Two arrays, one for the current board state and the other for the next board state. Store the state of each cell as an int8, dead = 0 and alive = 1. A subroutine that takes as input the current board array, a row array index, a column array index, and return the dead or alive status of that cell in the next generation. Initialize the next board array to 0. A subroutine that initializes an array with a Block (first still life) pattern near the middle of the board. A subroutine that initializes an array with a Period-2 Blinker near the middle of the board. A subroutine that initializes an array with a glider near the middle of the board. No global variables. Show that the current and next board arrays are the same at the end of the board update when the array is initialized to a Block. Do this by examining the array values for the two rows occupied by the Block. Show that the current and next board arrays show proper operation of a period-2 Blinker at the end of the board update when the array is initialized with a period-2 Blinker.

Explanation / Answer

Here is the C code for you:

//Conway's game of life.
#include <stdio.h>
#include <stdlib.h>
int frame[100][100];
void life(int n,int m) {
//Copies the main array to a frame array so changes can be entered into a grid
//without effecting the other cells and the calculations being performed on them.
int count;
int a[100][100];
for(int i = 0; i < 100; i++)
for(int j = 0; j < 100; j++)
a[i][j] = frame[i][j];
  
for(int i = 1 ; i <=n ; i++) {
for(int j = 1; j <= m; j++) {
count = 0;
count = a[i-1][j] + a[i][j-1] + a[i+1][j] + a[i][j+1] + a[i-1][j+1]
+ a[i+1][j-1] + a[i-1][j-1] + a[i+1][j+1];
//The cell dies.
if(count < 2 || count > 3)
frame[i][j] = 0;
//The cell stays the same.
if(count == 2)
frame[i][j] = a[i][j];
//The cell either stays alive, or is "born".
if(count == 3)
frame[i][j] = 1;
}
}
  
}
void print(int m,int n)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
printf("%d",frame[i][j]);
printf(" ");
}
}
int main()
{
char *filename;
int frames;
printf("Enter the file name:");
scanf("%s",filename);
printf("Enter no. of additional frames:");
scanf("%d",&frames);
FILE *file = fopen(filename, "r");
char c=(fgetc(file));
int m,n,w;
if(c=='P')
{
if(fgetc(file)=='N')
{
m = (int)fgetc(file);
n = (int)fgetc(file);
w = (int)fgetc(file);
int i=1,j=1;
  
for(int i=0;i<n+2;i++)
{
for(int j=0;j<m+2;j++)
{
if(i==0 || i==n+1) frame[i][j] = 0;
else fscanf(file, "%1d", &frame[i][j]);
}
}
  
}
} fclose(file);
for(int i=0;i<frames;i++)
{
printf(" Frame %d",i+1);
life(m,n);
print(m+2,n+2);
printf(" ");
}
return 0;
}