Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C LANGUAGE PLEASE ! Here is The code I used for Project 2: #include #define MAXSIZE 50 //define Maximum size #define MAXTIME 50 //define

IN C LANGUAGE PLEASE !

image text in transcribedimage text in transcribed

Here is The code I used for Project 2:

#include

#define MAXSIZE 50 //define Maximum size

#define MAXTIME 50 //define maximum time step size

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(anArray[i+1][j-1][step]==1)

totalCount++;

}

if(j+1

{

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(anArray[i+1][j+1][step]==1)

totalCount++;

}

if(i-1>=0)

if(anArray[i-1][j][step]==1)

totalCount++;

if(i+1

if(anArray[i+1][j][step]==1)

totalCount++;

}

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

{

printf(" ");

c=0;

while(c

{

printf(" %d",myGrid[r][c][step]);

c++;

}

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

{

i=0;

while(i

{

j=0;

while(j

{

if(gridArr[i][j][k]==1)

{

if(neighbors(gridArr,i,j,n,k)==2||neighbors(gridArr,i,j,n,k)==3)//call neighbors() function

gridArr[i][j][k+1]=1;

else

gridArr[i][j][k+1]=0;

}

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;

}

The initial data poi ts ea out of the of alues wil be we wil be using he concepts rom project 2?test he more recent concepts o linked ists, pointers, and le dynamically allocated and then referenced via a pointer in a linked list. More details are: be store na e specit ca on ebelo each gn input file .first line will list the number of time steps to simulate . second line will include number of rows followed by the number of columns in the grid to be simulated . remaining lines in the file will have the positions of the initial cells that are 'alive' dynamically create nodes that use the following struct struct node t int rows; int columns; int grid; // a pointer to the grid that should be of size rows*columns struct node "next step a pointer to the node that holds the grid for the next time step 1: you will create a linked list of nodes where each node is a single time step the begin pointer should point to the initial grid, and each time step should follow. no array notation is to be used, so no D's should appear in your code. You MUST create the following functions struct node "createInitialNode(FILE input, int "numsteps); I/ read data from file pointer input and return a dynamically created node with the data and the number of time st eps to simulate in numsteps void nextStep(struct node *begin); the new timestep add a struct node to the end of the list pointed to by begin for the next time step, so use data in the last node in the list to create void printList(struct node begin); // print out all the grids contained in the list pointed to by begin The initial data poi ts ea out of the of alues wil be we wil be using he concepts rom project 2?test he more recent concepts o linked ists, pointers, and le dynamically allocated and then referenced via a pointer in a linked list. More details are: be store na e specit ca on ebelo each gn input file .first line will list the number of time steps to simulate . second line will include number of rows followed by the number of columns in the grid to be simulated . remaining lines in the file will have the positions of the initial cells that are 'alive' dynamically create nodes that use the following struct struct node t int rows; int columns; int grid; // a pointer to the grid that should be of size rows*columns struct node "next step a pointer to the node that holds the grid for the next time step 1: you will create a linked list of nodes where each node is a single time step the begin pointer should point to the initial grid, and each time step should follow. no array notation is to be used, so no D's should appear in your code. You MUST create the following functions struct node "createInitialNode(FILE input, int "numsteps); I/ read data from file pointer input and return a dynamically created node with the data and the number of time st eps to simulate in numsteps void nextStep(struct node *begin); the new timestep add a struct node to the end of the list pointed to by begin for the next time step, so use data in the last node in the list to create void printList(struct node begin); // print out all the grids contained in the list pointed to by begin<>

<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

Students also viewed these Databases questions

Question

Write down the Limitation of Beer - Lamberts law?

Answered: 1 week ago

Question

Discuss the Hawthorne experiments in detail

Answered: 1 week ago

Question

Explain the characteristics of a good system of control

Answered: 1 week ago

Question

State the importance of control

Answered: 1 week ago