In: Computer Science
In C programming
Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1
[MAX_ROW][MAX_COL].
Then, loop through the 2d array and save the results in n0 and
n1:
for (i=0; i<MAX_ROW; i++)
for (j=0; j<MAX_COL;j++){
//calculate number of
zeros around entry a[i][j]
n0[i][j] =
//calculate number of
ones around entry a[i][j]
n1[i][j] =
}
Set the MAX_ROW and MAX_COL to a small number and display all three 2d arrays on the screen to verify that the calculations are correct. You may still use command-line inputs as in the example program to set the actual size of the array (must be less than or equal to MAX_ROW, MAX_COL.
I am adding the code with the output and i have commented for each condition so that it becomes easy to understand for you...
I have Checked all the possible cases for better and proper outcomes...
if you have any doubt,please ask me in the comment box... please try to understand the logic by making a 2D array on a papper and then see the condition strp by step... it will help you...
code
#include<stdio.h>
#define MAX_ROW 5
#define MAX_COL 5
int count0(int a[][MAX_COL],int i,int j){ //for counting number of 0 around position i,j
int count=0; // initialising count = 0
if(i!=0 && i!=MAX_ROW-1 && j!=0 && j!=MAX_COL-1){ //if i and j are not at the age position
if(a[i][j+1]==0) count++;
if(a[i][j-1]==0) count++;
if(a[i+1][j]==0) count++;
if(a[i-1][j]==0) count++;
if(a[i-1][j+1]==0) count++;
if(a[i+1][j+1]==0) count++;
if(a[i-1][j-1]==0) count++;
if(a[i+1][j-1]==0) count++;
}
else if(i==0 && j!=0 && j!=MAX_COL-1){ // i.e first row except first and last column
if(a[i][j+1]==0) count++;
if(a[i][j-1]==0) count++;
if(a[i+1][j+1]==0) count++;
if(a[i+1][j]==0) count++;
if(a[i+1][j-1]==0) count++;
}
else if(j==0 && i!=0 && i!=MAX_ROW-1){ //// i.e first column except first and last row
if(a[i-1][j]==0) count++;
if(a[i-1][j+1]==0) count++;
if(a[i][j+1]==0) count++;
if(a[i+1][j+1]==0) count++;
if(a[i+1][j]==0) count++;
}
else if(i==MAX_ROW-1 && j!=0 && j!=MAX_COL-1){ //// i.e last row except first and last culumn
if(a[i][j+1]==0) count++;
if(a[i][j-1]==0) count++;
if(a[i-1][j]==0) count++;
if(a[i-1][j+1]==0) count++;
if(a[i-1][j-1]==0) count++;
}
else if(j==MAX_COL-1 && i!=0 && i!=MAX_ROW-1){ //// i.e last column except first and last row
if(a[i+1][j]==0) count++;
if(a[i-1][j]==0) count++;
if(a[i-1][j-1]==0) count++;
if(a[i+1][j-1]==0) count++;
if(a[i][j-1]==0) count++;
}
else if(i==0 && j==0){ //i.e upper left corner element
if(a[i][j+1]==0) count++;
if(a[i+1][j+1]==0) count++;
if(a[i+1][j]==0) count++;
}
else if(i==0 && j==MAX_COL-1){ //i.e upper right corner element
if(a[i][j-1]==0) count++;
if(a[i+1][j-1]==0) count++;
if(a[i+1][j]==0) count++;
}
else if( i== MAX_ROW-1 && j==0){ //i.e lower left corner element
if(a[i][j+1]==0) count++;
if(a[i-1][j+1]==0) count++;
if(a[i-1][j]==0) count++;
}
else if(i== MAX_ROW-1 && j==MAX_COL-1){ //i.e lower right corner element
if(a[i][j-1]==0) count++;
if(a[i-1][j-1]==0) count++;
if(a[i-1][j]==0) count++;
}
return count; //returning count
}
int count1(int a[][MAX_COL],int i,int j){ //for counting number of 1 around position i,j
int count=0;
if(i!=0 && i!=MAX_ROW-1 && j!=0 && j!=MAX_COL-1){ //if i and j are not at the age position
if(a[i][j+1]==1) count++;
if(a[i][j-1]==1) count++;
if(a[i+1][j]==1) count++;
if(a[i-1][j]==1) count++;
if(a[i-1][j+1]==1) count++;
if(a[i+1][j+1]==1) count++;
if(a[i-1][j-1]==1) count++;
if(a[i+1][j-1]==1) count++;
}
else if(i==0 && j!=0 && j!=MAX_COL-1){ // i.e first row except first and last column
if(a[i][j+1]==1) count++;
if(a[i][j-1]==1) count++;
if(a[i+1][j+1]==1) count++;
if(a[i+1][j]==1) count++;
if(a[i+1][j-1]==1) count++;
}
else if(j==0 && i!=0 && i!=MAX_ROW-1){ //// i.e first column except first and last row
if(a[i-1][j]==1) count++;
if(a[i-1][j+1]==1) count++;
if(a[i][j+1]==1) count++;
if(a[i+1][j+1]==1) count++;
if(a[i+1][j]==1) count++;
}
else if(i==MAX_ROW-1 && j!=0 && j!=MAX_COL-1){ /// i.e last row except first and last culumn
if(a[i][j+1]==1) count++;
if(a[i][j-1]==1) count++;
if(a[i-1][j]==1) count++;
if(a[i-1][j+1]==1) count++;
if(a[i-1][j-1]==1) count++;
}
else if(j==MAX_COL-1 && i!=0 && i!=MAX_ROW-1){ // i.e last column except first and last row
if(a[i+1][j]==1) count++;
if(a[i-1][j]==1) count++;
if(a[i-1][j-1]==1) count++;
if(a[i+1][j-1]==1) count++;
if(a[i][j-1]==1) count++;
}
else if(i==0 && j==0){ //i.e upper left corner element
if(a[i][j+1]==1) count++;
if(a[i+1][j+1]==1) count++;
if(a[i+1][j]==1) count++;
}
else if(i==0 && j==MAX_COL-1){ //i.e upper right corner element
if(a[i][j-1]==1) count++;
if(a[i+1][j-1]==1) count++;
if(a[i+1][j]==1) count++;
}
else if( i== MAX_ROW-1 && j==0){ //i.e lower left corner element
if(a[i][j+1]==1) count++;
if(a[i-1][j+1]==1) count++;
if(a[i-1][j]==1) count++;
}
else if(i== MAX_ROW-1 && j==MAX_COL-1){ ////i.e lower right corner element
if(a[i][j-1]==1) count++;
if(a[i-1][j-1]==1) count++;
if(a[i-1][j]==1) count++;
}
return count; //returning count
}
int main(){
int a[MAX_ROW][MAX_COL],n0[MAX_ROW][MAX_COL],n1[MAX_ROW][MAX_COL],i,j;
printf("\nEnter the Elements in the array ' a ' \n");
for (i=0; i<MAX_ROW; i++){
for (j=0; j<MAX_COL;j++){
scanf("%d",&a[i][j]); //scanning array a
n0[i][j]=n1[i][j]=0; //initialising n1,n0 by 0
}
}
for (i=0; i<MAX_ROW; i++){
for (j=0; j<MAX_COL;j++){
n0[i][j]=count0(a,i,j); //calling function for each position of array a
n1[i][j]=count1(a,i,j);
}
}
printf("\n ARRAY 'a' ARRAY n0 ARRAY n1 \n");
for (i=0; i<MAX_ROW; i++){ //printing the arrays side by side
for (j=0; j<MAX_COL;j++)
printf("%2d",a[i][j]);
printf(" | ");
for (j=0; j<MAX_COL;j++)
printf("%2d",n0[i][j]);
printf(" | ");
for (j=0; j<MAX_COL;j++)
printf("%2d",n1[i][j]);
printf("\n");
}
return 0;
}
output....