2013-01-05 34 views
0

我在我的代碼中有一處錯誤,我想我正在進入一個無限循環。 基本上我得到一個矩陣,兩個指標,I,J,我需要周圍算多少鄰居[i] [j]有1或2康威的生命之球,數數鄰居

的值,這是我的代碼:

int number_of_living_neighbors(matrix mat,int i, int j, int n,int m) 
{ 
    int counter=0,row_index=0,column_index=0; 
    for(row_index=i-1;row_index<=i+1;row_index++) 
    { 
     for(column_index=j-1;column_index=j+1;column_index++) 
     { 
      if((row_index>=0)&&(row_index<n)&&(column_index>=0)&&(column_index<m)) 
      { 
       if((mat[row_index][column_index]==1)||(mat[row_index][column_index]==2)) 
        counter++; 
       if((row_index==i)&&(column_index==j)&&(mat[i][j]==1)) 
        counter--; 
      } 
     } 
    } 
    printf("The number of living neighbors is %d", counter); 
    return counter; 
} 

它沒有打印任何東西。 mat是我得到的矩陣,i,j是指針,n是行數,m是列數。

回答

5
for(column_index = j-1 ; column_index = j+1; column_index++) 

您的情況包含一個作業,而不是比較。您可能要使用<=而不是=

for(column_index = j-1 ; column_index <= j+1; column_index++) 
+0

非常感謝您,那的確是問題所在 –