Question
Implementing Conways Game Of Life in the language C https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life Your program will take as input from standard in: (1) The name of the input
Implementing Conways Game Of Life in the language C
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Your program will take as input from standard in: (1) The name of the input pgm to act as the seed (2) The number of additional frames to compute. You will write the sequence of frames to standard out.
1 PGM Files
http://netpbm.sourceforge.net/doc/pgm.html
Format:
P2 MN W #1 #2 #3 ...
#MxN -1 #MxN
At the top of the file is the special keyword P2, read it in as a string and make sure it matches. M and N are the integer dimensions of the image. M is the number of columns, N is the number of rows. W is the integer for the value of white on a grey scale. 0 is black. If you want a black and white image then W should be 1. Every M numbers after W populate a complete row.
2 Internal Representation Of The Map
After reading in the dimensions of the board, You will allocate a 2D array (M+2)x(N+2). The outer most rows and columns (index 0 and M+1/N+1)
are fixed at 0. You will read the board in from the PGM file into the indeces [1..M][1..N] in memory.
3 Rules Of Conways Game Of Life
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Rules
Any live cell with fewer than two live neighbours dies, as if caused by under-population.
Any live cell with two or three live neighbours lives on to the next gener- ation.
Any live cell with more than three live neighbours dies, as if by over- population.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
For a given cell, its neighborhood is every cell that is +/- 1 in all directions.
4 Computing The Next Frame
Starting from the seed, you will compute the indicated number of additional frames. Each frames border is fixed at 0, indicating it is permanently empty. To create a new frame you will allocate the space for the new frame, initializing the border to zero, and for every internal cell in the new frame you will look at the neighborhood in the old frame to determine if the cell should be occupied or empty.
The simplest organization is to have a function which takes as input a frame and returns as output a frame.
5 Output To Standard Out
First you will output the seed frame:
Seed: M N
OXXOOOOOOXOX
OXOOOXXOOXOX
OXOXOOOXXXOX
...
You will print Frame: followed by the dimensions.
The following N lines will contain the contents for the rows where O (capital O) is 0 or empty and X (capital X) is 1 or occupied, each rows content separated by a single space.
After computing each from you should output the following information:
Frame: #
OXXOOOOOOXOX
OXOOOXXOOXOX
OXOXOOOXXXOX
...
You will print Frame: followed by the number of the frames (starting from 1).
The following N lines will contain the contents for the rows where O (capital O) is 0 or empty and X (capital X) is 1 or occupied, each rows content separated by a single space.
Example:
Seed: 3 3
OOOOO
OOXOO
OOXOO
OOXOO
OOOOO
Frame: 1
OOOOO
OOOOO
OXXXO
OOOOO
OOOOO
Frame: 2
OOOOO
OOXOO
OOXOO
OOXOO
OOOOO
Frame: 3
OOOOO
OOOOO
OXXXO
OOOOO
OOOOO
DOES THIS CODE SATISFY THIS QUESTION? ALSO in the main(), int i and j are set to 1, but xcodes says, unused variables i and j, as well as in the main(), life(m,n); says "variable 'm' may be uninitialized when used here. are these okay?
#include
#include
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 { for(int j=0;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 { for(int j=0;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 { printf(" Frame %d",i+1); life(m,n); print(m+2,n+2); printf(" "); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started