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

Please show as many comments as you can that describe what is going on. No matte

ID: 3632272 • Letter: P

Question

Please show as many comments as you can that describe what is going on. No matter how basic. I'm new to programming and struggling with these programs. Also, this program has to be written in C code not C++. Here is the question and some trial runs showing what the program must input and output exactly. Thank you all for your help.

Write a program zombie.c that prompts the user for a 5 X 5 array of zeros and ones (entered consecutively row by row) and run the following game for three times:

1.)Each element of the array is either a zombie (if it is 0) or a human (if it is 1).

2.)The neighbors of each elements are those horizontally, vertically, or diagonally adjacent to it, hence corner elements have three neighbors, elements on the side have ve neighbors, and elements in the middle have eight neighbors.

3.)At each iteration of the game the following events occur:
-Any human with less than two (namely 0 or 1) human neighbors is attacked by the zombies and becomes a zombie.
-Any zombie with more than two (namely 3 up to 8) human neighbors is cured by the humans and becomes a human.

Denoting zombies by - and humans by *, the program prints the initial state of the game (provided by the user), and then runs the above iterations three times, printing the new state each time.
Sample run:
(~)$ a.out
Enter 5*5 array: 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0
State 0:
- - - - -
- - - - -
- * * * -
- - - - -
- - - - -
State 1:
- - - - -
- - * - -
- - * - -
- - * - -
- - - - -
State 2:
- - - - -
- - - - -
- * * * -
- - - - -
- - - - -
State 3:
- - - - -
- - * - -
- - * - -
- - * - -
- - - - -
(~)$
3

Explanation / Answer

#include <stdio.h>

//number of rows, columns

//constants throughout the program

#define ROWS 5

#define COLS 5

//displays the array to the console

void display(int a[ROWS][COLS])

{

int i, j;

printf(" ");

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

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

if(a[i][j] == 0)

printf("%5s", "-");

else

printf("%5s","*");

}

printf(" ");

}

printf(" ");

}

//calculates number of neighbours for an element in the 5*5 matrix

//specified by its row and column

int neighboursof(int a[ROWS][COLS], int row, int col)

{

int tleft = 0, top =0 , tright = 0;

int bleft = 0, bottom = 0, bright =0;

int left = 0, right = 0;

int neighs = 0; //number of neighbours

if(row > 0)

{

top = a[row-1][col]; //top element

if(col > 0)

tleft = a[row-1][col-1]; //leftside top element

if(col < COLS-1)

tright = a[row-1][col+1];//rightside top

}

if(row < ROWS-1)

{

bottom = a[row+1][col]; //bottom

if(col > 0)

bleft = a[row+1][col-1]; //left side bottom

if(col < COLS-1)

bright = a[row+1][col+1]; //right bottom

}

if(col > 0)

left = a[row][col-1]; //left

if(col < COLS-1)

right = a[row][col+1]; //right element

//total num of neighbours

neighs = tleft + top + tright + bleft + bottom + bright + left + right;

return neighs;

}

//main function

int main()

{

int a[ROWS][COLS]; // 5*5 array

int i, j, k; //loop variables

int tmp[ROWS][COLS]; //temperory array to store intermediate values

//reads 5*5 array from the console

printf("enter 5*5 array: ");

//stores the input values into 5*5 array

//and also in tmp array

for( i = 0; i < ROWS; i++)

{

for(j = 0; j < COLS; j++)

{

scanf("%d", & a[i][j]);

tmp[i][j] = a[i][j];

}

}

//loop 4 times for 4 states

for( k =0; k< 4;k++)

{

//displays the array state

display(a);

//for each element in the 5*5 array

//find the number of neighbours

//and changes the corresponding elements in the tmp

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

for(j = 0;j < COLS; j++)

{

if(a[i][j] == 1)

{

if(neighboursof(a, i, j) < 2)

tmp[i][j] = 0;

}

else

if(neighboursof(a, i ,j) >= 3)

tmp[i][j] = 1;

} //j for

} //i for

//copies the tmp values to a

for(i =0; i<ROWS; i++)

for(j=0; j<COLS; j++)

a[i][j] = tmp[i][j];

}// k for

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote