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

In C produce change the following code same output. Use better techniques thats

ID: 3726689 • Letter: I

Question

In C produce change the following code same output. Use better techniques thats all.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include<stdio.h>
#define MAXNUM 60 //defines the Maximum NUM
#define MAXTIM 60//define maximum time step size
/*function definition for returns the number of neighbors of position i,j that are alive at time step*/

int neighbors(int anArray[MAXNUM][MAXNUM][MAXTIM+1], int i, int j, int size, int step)
{
//initilaizes the total count

int totalCount=0;
if(i>=0&&j>=0&&i<size&&j<size) //
{
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<size)
if(anArray[i+1][j-1][step]==1)
totalCount++;
}
if(j+1<size)
{
if(anArray[i][j+1][step]==1)
totalCount++;
if(i-1>=0)
if(anArray[i-1][j+1][step]==1)
totalCount++;
if(i+1<size)
if(anArray[i+1][j+1][step]==1)
totalCount++;
}
if(i-1>=0)
if(anArray[i-1][j][step]==1)
totalCount++;
if(i+1<size)
if(anArray[i+1][j][step]==1)
totalCount++;
}
return totalCount;
}
//function definition for print the grid of size sizexsize at time step
void printGrid(int myGrid[MAXNUM][MAXNUM][MAXTIM+1],int size,int step)
{
int r=0,c=0;
while(r<size)
{
printf(" ");
c=0;
while(c<size)
{
printf(" %d",myGrid[r][c][step]);
c++;
}
r++;
}
}
//main function
int main()
{
//print the given message
printf("Conway's game of Life ");

printf("Please enter the n for the n x n grid to simulate, max size for n is 60. :");
//initializes the n
int n=0;
//gets the maximum size for n
scanf("%d",&n);
//3d grid array

int gridArr[MAXNUM][MAXNUM][MAXTIM+1]={0};
printf(" Enter the initial x y coordinates (the board is indexed starting from 0 0) for the initial live cells, enter -1 -1 when done ");
//initializes the row r and column c is 0
int r=0,c=0;
//get the r and c upto done r = -1 and c = -1
while(r!=-1||c!=-1)
{
scanf("%d %d",&r,&c);
gridArr[r][c][0]=1;
}
//initializes the time step ts is 0
int ts=0;
//gets the time step from user
printf(" Please enter the number of time steps to simulate, max number allowed is 60.");
scanf("%d",&ts);
//print the time step
printf("Num time steps %d ",ts);
printf("After %d timesteps the grid is: ",ts);
//compute the grid after the given time step
int i,j,k=1;
k=0;
while(k<=ts)
{
i=0;
while(i<n)
{
j=0;
while(j<n)
{
if(gridArr[i][j][k]==1)
{
//call neighbors() function
if(neighbors(gridArr,i,j,n,k)==2||neighbors(gridArr,i,j,n,k)==3)
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++;
}
//call printGrid() function to display the grid after given ts
printGrid(gridArr,n,ts);
printf(" ");
return 0;
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------output:

Conway's game of Life                                                                                    

Please enter the n for the n x n grid to simulate, max size for n is 60. :10   

                                                                                                         

Enter the initial x y coordinates (the board is indexed starting from 0 0) enter -1 -1 when done   

7 7                                                                                                      

7 8                                                                                                      

7 9                                                                                                      

8 7                                                                                                      

9 8                                                                                                      

-1 -1                                                                                                    

                                                                                                         

Please enter the number of time steps to simulate, max numbe^[[                                          

12                                                                                                       

Num time steps 12                                                                                        

After 12 timesteps the grid is:                                                                          

0 0 0 0 0 0 0 0 0 0                                                                                     

0 0 0 0 0 0 0 0 0 0                                                                                     

0 0 0 0 0 0 0 0 0 0                                                                                     

0 0 0 0 0 0 0 0 0 0                                                                                     

0 0 0 0 1 1 1 0 0 0                                                                                     

0 0 0 0 1 0 0 0 0 0                                                                                     

0 0 0 0 0 1 0 0 0 0                                                                                     

0 0 0 0 0 0 0 0 0 0                                                                                     

0 0 0 0 0 0 0 0 0 0                                                                                     

0 0 0 0 0 0 0 0 0 0

Explanation / Answer

#include <stdio.h>

/* Board Width and Hgight */

#define WIDTH 10

#define HEIGHT 10

/* initialize to zero */

void initiboard (int board[][HEIGHT]) {

int i, j;

int size;

printf(" Enter GRId size");

scanf("%d", &size);

for (i=0; i<WIDTH; i++) for (j=0; j<HEIGHT; j++)

board[i][j] = 0;

}

int XAddition (int i, int a) {

i += a;

while (i < 0) i += WIDTH;

while (i >= WIDTH) i -= WIDTH;

return i;

}

/* add to a height index, wrapping around */

int YAddition (int i, int a) {

i += a;

while (i < 0) i += HEIGHT;

while (i >= HEIGHT) i -= HEIGHT;

return i;

}

/* return the number of on cells neighbor to the i,j cell */

int neighbour (int board[][HEIGHT], int i, int j) {

int k, l, count;

count = 0;

for (k=-1; k<=1; k++) for (l=-1; l<=1; l++)

if (k || l)

if (board[XAddition(i,k)][YAddition(j,l)]) count++;

return count;

}

void PlayGame (int board[][HEIGHT]) {

int i, j, a, newboard[WIDTH][HEIGHT];

/* for each cell, apply the rule of Life */

for (i=0; i<WIDTH; i++) for (j=0; j<HEIGHT; j++) {

a = neighbour (board, i, j);

if (a == 2) newboard[i][j] = board[i][j];

if (a == 3) newboard[i][j] = 1;

if (a < 2) newboard[i][j] = 0;

if (a > 3) newboard[i][j] = 0;

}

/* copy new board back into old board */

for (i=0; i<WIDTH; i++) for (j=0; j<HEIGHT; j++) {

board[i][j] = newboard[i][j];

}

}

/* print board */

void DisPlayGameBoard (int board[][HEIGHT]) {

int i, j;

/* for each row */

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

/* DisPlayGameBoard each column position... */

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

printf ("%c", board[i][j] ? 'x' : ' ');

}

/* followed by a carriage return */

printf (" ");

}

}

/* read a file into the life board */

void ReadUserInput (int board[WIDTH][HEIGHT]) {

int i, j;

printf(" enter initail(-1, -1 to finish) ");

while(i!=-1 ||j!=-1)

{

scanf("%d %d", &i, &j);

board[i][j]=1;

}

}

/* main program */

int main (int argc, char *argv[]) {

int board[WIDTH][HEIGHT], i, j;

int ts;

initiboard (board);

ReadUserInput (board);

printf(" How many times?");

scanf("%d", &ts);

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

DisPlayGameBoard (board);

PlayGame(board);

}

}