2016-06-14 154 views
0

我試圖創建一個(10×10)2維字符陣列來存儲或者「」和「T」和這樣2維陣列操縱

+-+-+-+-+-+-+-+-+-+-+ 
| |T| |T| |T| |T| |T| 
+-+-+-+-+-+-+-+-+-+-+ 
|T| |T| |T| |T| |T| | 
+-+-+-+-+-+-+-+-+-+-+ 
| |T| |T| |T| |T| |T| 
+-+-+-+-+-+-+-+-+-+-+ 
|T| |T| |T| |T| |T| | 
+-+-+-+-+-+-+-+-+-+-+ 
| |T| |T| |T| |T| |T| 
+-+-+-+-+-+-+-+-+-+-+ 
|T| |T| |T| |T| |T| | 
+-+-+-+-+-+-+-+-+-+-+ 
| |T| |T| |T| |T| |T| 
+-+-+-+-+-+-+-+-+-+-+ 
|T| |T| |T| |T| |T| | 
+-+-+-+-+-+-+-+-+-+-+ 
| |T| |T| |T| |T| |T| 
+-+-+-+-+-+-+-+-+-+-+ 
|T| |T| |T| |T| |T| | 
+-+-+-+-+-+-+-+-+-+-+ 

顯示它我寫的函數:

int plant_forest(char forest[][SIZE]) 
{ 
    int i,j; 
    forest[0][0] = ' '; 
    for(i = 0;i<SIZE;i++) 
    { 
     for(j=0;j<SIZE;j++) 
     { 
      if(forest[i][j]!= forest[0][0]) 
      { 
       if(forest[i][j-1]!='T' && forest[i-1][j]!= 'T') 
       { 
        forest[i][j] = 'T'; 
       } 
       else 
       { 
        forest[i][j] = ' '; 
       } 

      } 
     } 
    } 
    return 0; 
} 

我得到的結果稍有不同。

+-+-+-+-+-+-+-+-+-+-+ 
| |T| |T| |T| |T| |T| 
+-+-+-+-+-+-+-+-+-+-+ 
| | |T| |T| |T| |T| | 
+-+-+-+-+-+-+-+-+-+-+ 
|T| | |T| |T| |T| |T| 
+-+-+-+-+-+-+-+-+-+-+ 
| |T| | |T| |T| |T| | 
+-+-+-+-+-+-+-+-+-+-+ 
|T| |T| | |T| |T| |T| 
+-+-+-+-+-+-+-+-+-+-+ 
| |T| |T| | |T| |T| | 
+-+-+-+-+-+-+-+-+-+-+ 
|T| |T| |T| | |T| |T| 
+-+-+-+-+-+-+-+-+-+-+ 
| |T| |T| |T| | |T| | 
+-+-+-+-+-+-+-+-+-+-+ 
|T| |T| |T| |T| | |T| 
+-+-+-+-+-+-+-+-+-+-+ 
| |T| |T| |T| |T| | | 
+-+-+-+-+-+-+-+-+-+-+ 

我檢查了邏輯並找不到任何錯誤。除了i-1和j-1可能是負數。但是這將如何影響執行?

僅供參考,我將在此列入打印功能。但我已經檢查並確定此函數中沒有錯誤

void printBoard(char forest[][SIZE]) 
{ 
    int i,j; 

     printf("+-+-+-+-+-+-+-+-+-+-+\n"); 
     for(i = 0; i<SIZE;i++) 
     { 
      for(j = 0;j<SIZE;j++) 
      { 
       printf("|%c",forest[i][j]); 
      } 
      printf("|\n"); 
      printf("+-+-+-+-+-+-+-+-+-+-+\n"); 

     } 

} 
+2

'[j-1]' - 但'j'從0開始!更不用說除了[0] [0]''forest'沒有初始化。爲了獲得「棋盤格」,你看起來過於複雜(而且很麻煩)。 – John3136

+0

森林在主要功能中初始化。 –

+0

要製作棋盤格:'forest [i] [j] =(i + j)&1? 'T':'';' – user3386109

回答

1

我認爲這可能與如何存儲數組中的數據有關。在內存中,數據只存儲在sequentially。由於negative array indexes are allowed in C您的情況下的功能結果是森林[1] [ - 1]指向位於單元格森林[0] [9]中的相同數據,您會注意到這是'T'。

由於森林[0] [9](又名森林[1] [ - 1])是 'T',你失敗了該試驗

if(forest[i][j-1]!='T' && forest[i-1][j]!= 'T') 

這意味着 '' 在細胞森林註冊改爲[1] [0]。防止負面指數值,你應該是好的。

0

您可能在想,您使用的邏輯填充forest陣列plant_forest。所有你需要錯開用於填充forest'T'' '元素是檢查你的外環指數'i'奇數或偶數並開始填充無論是'T'' '基於此結果。然後,對於每個'j'索引,只需開關'T'' '之間。例如:

int plant_forest (char (*forest)[SIZE]) 
{ 
    if (!forest) return 1;    /* validate pointer */ 
    int i, j; 

    for (i = 0; i < SIZE; i++) { 
     int t = i & 1; 
     for (j = 0; j < SIZE; j++) /* simple toggle 'T' or ' ' */ 
      forest[i][j] = t ? 'T' : ' ', t ^= 1; 
    } 
    return 0; 
} 

你可以用一個簡單的代碼位測試結果:

#include <stdio.h> 

#define SIZE 10 

int plant_forest (char (*forest)[SIZE]) 
{ 
    if (!forest) return 1;    /* validate pointer */ 
    int i, j; 

    for (i = 0; i < SIZE; i++) { 
     int t = i & 1; 
     for (j = 0; j < SIZE; j++) /* simple toggle 'T' or ' ' */ 
      forest[i][j] = t ? 'T' : ' ', t ^= 1; 
    } 
    return 0; 
} 

int main (void) { 

    char arr[SIZE][SIZE] = {{0}}; 

    if (!plant_forest (arr)) { 
     int i, j; 
     for (i = 0; i < SIZE; i++) { 
      for (j = 0; j < SIZE; j++) 
       putchar (arr[i][j]); 
      putchar ('\n'); 
     } 
    } 
    return 0; 
} 

示例使用/輸出

$ ./bin/plant 
T T T T T 
T T T T T 
T T T T T 
T T T T T 
T T T T T 
T T T T T 
T T T T T 
T T T T T 
T T T T T 
T T T T T 

如果您有任何疑問,或我誤解了你的問題,只是讓我知道。 (我會給你填寫所有額外的'|'格式你可能會喜歡)。

你甚至可以保存一行代碼由安定在評論中提到ij的總和,在plant_forest分配'T'' '例如之間做出選擇

for (i = 0; i < SIZE; i++) 
     for (j = 0; j < SIZE; j++) /* simple toggle 'T' or ' ' */ 
      forest[i][j] = (i + j) & 1 ? 'T' : ' '; 

無論哪種方式都可以確保您的forest陣列正確填充。