我無法確定如何在考慮角落&陣列邊緣的情況下識別下面我的代碼中列出的陣列中的1。陣列中的鄰居
#include <stdio.h>
/* 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 < SIZE; i++)
for (j = 0; j < SIZE; j++)
grid[i][j] = 0;
/* 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 < SIZE; i++)
for (j = 0; j < SIZE; j++) {
n = neighbors(i,j);
printf ("Number of neighbors to element %d,%d =%d\n",i,j,n);
}
return;
}
/* function to compute the neighbors */
int neighbors (int i, int j)
我想,我可以使用if語句來修改密碼將如何運行,如果I = 0或I = 6,以及如果j = 0或j = 6,但我不知道如何繼續。任何幫助將不勝感激
請縮進代碼在你的問題。 – jdarthenay
另外7個已經超出了你的矩陣的範圍,我想在最後一句話中你的意思是「i == 1或i == 6,以及如果j == 1或者j == 6」 – jdarthenay
@jdarthenay你是正確的,已經做出了相應的編輯 –