Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given an nxn array of integers. Let us call this array grid . All the elements of grid are either 0 or 1.

You are given an nxn array of integers. Let us call this array grid . All the elements of grid are either 0 or 1. Write a function (name the function neighbors ) that will take as arguments only two integer indices i, j , and will return the number of nonzero entries of the array grid that are adjacent to the i - th, j - th element of the array. For example, if the grid array is the 7x7 array shown below, then your function should return the following values, when called with the arguments shown below:

image text in transcribedneighbors(0,0) should return 0, since there are no non-zero neighbors to element (0,0)

neighbors(0,1) should return 1, since there is 1 non-zero neighbor to element (0,1)

neighbors(3,3) should return 4, since there are 4 non-zero neighbors to element (3,3)

neighbors(3,4) should return 2,

neighbors(5,3) should return 1,

Complete the code given below; your task is to fill in the code for the function only. Turn in a hardcopy of the code together with a snapshot of its output.

#include

/* define grid size */

#define SIZE 7

int grid[SIZE][SIZE];

/* function to find the number of occupied adjacent cells */ int neighbors (int i, int j);

void main () { int i, j, n;

/* initialize the entire grid to be zero */

for (i = 0; i

/* introduce a few ones */

grid[1][2] = 1;

grid[2][2] = 1;

grid[1][4] = 1;

grid[2][4] = 1;

grid[3][2] = 1;

grid[3][3] = 1;

grid[3][4] = 1;

grid[5][3] = 1;

grid[6][2] = 1;

for (i = 0; i

} return; } /* function to compute an element's neighbors */ int neighbors (int i, int j) {

FILL IN THE REMAINDER OF THE CODE

grid th st nd th column column column column column column column row 0 st 0 row nd row 0 row th row 0 row th row

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_2

Step: 3

blur-text-image_3

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

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions