Question: Example input file sampleinput.txt contains: 3 10 12 7 7 7 8 7 9 8 7 9 8 So your code should display the following:

Example input file sampleinput.txt
contains:
3 10 12 7 7 7 8 7 9 8 7 9 8
So your code should display the following:
Welcome to Conway's game of Life Please enter filename 000000000000 000000000000 000000000000 000000000000 000000000000 000000000000 000000000000 000000011100 000000010000 000000001000 000000000000 000000000000 000000000000 000000000000 000000000000 000000000000 000000001000 000000011000 000000010100 000000000000 000000000000 000000000000 000000000000 000000000000 000000000000 000000000000 000000011000 000000010100 000000010000 000000000000 000000000000 000000000000 000000000000 000000000000 000000000000 000000000000 000000011000 000000110000 000000001000 000000000000
Here is the code to do the file for In C language:
#include
int neighbors(int anArray[MAXSIZE][MAXSIZE][MAXTIME+1], int i, int j, int size, int step)// returns the number of neighbors of position i,j that are alive at time step.
{
int totalCount=0; //initilaizes the total count
if(i>=0 && j>=0 && i { if(j-1>=0) { if(anArray[i][j-1][step]==1) totalCount++; if(i-1>=0) if(anArray[i-1][j-1][step]==1) totalCount++; if(i+1 if(j+1 if(i-1>=0) if(anArray[i-1][j+1][step]==1) totalCount++; if(i+1 if(i-1>=0) if(anArray[i-1][j][step]==1) totalCount++; if(i+1 } return totalCount; } void printGrid(int myGrid[MAXSIZE][MAXSIZE][MAXTIME+1],int size,int step) // prints the grid of size sizexsize at time step. { int r=0,c=0; while(r r++; } } int main() //main function { printf("Welcome to Conway's game of Life "); //prints welcome to the game message printf("Please enter the n for the n by n grid to simulate, max size for n is 50. :"); //prints a senetnce to ask the user to enter the grid demension int n=0; //initializes the n scanf("%d",&n); //scans the maximum size for n users enters int gridArr[MAXSIZE][MAXSIZE][MAXTIME+1]={0}; //3d grid array if (n > 50) // If the user enters a grid size greater than 50, the code will print error and exit. { printf("Error "); } else { printf(" Thank You,now enter the initial x y coordinate(the board is indexed starting from 0 0) for the initial live cells,enter -1 -1 when done "); int r=0,c=0; //initializes the row r and column c is 0 while(r!=-1||c!=-1) { scanf("%d %d",&r,&c); gridArr[r][c][0]=1; } int ts=0; //initializes the time step ts is 0 printf(" Please enter the number of time steps you wish to simulate, max number allowed is 50."); //asks the user to enter time step scanf("%d",&ts); if (ts>50) // If the user enters a timestep greater than 50, the code will print error and exit. { printf("Erorr. "); } else { printf("Num time steps %d ",ts);//prints the time step printf("After %d timesteps the grid is: ",ts); int i,j,k=1; k=0; while(k else if(gridArr[i][j][k]==0) { if(neighbors(gridArr,i,j,n,k)==3) gridArr[i][j][k+1]=1; } j++; } i++; } k++; } printGrid(gridArr,n,ts); printf(" "); } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
