john 發問時間： 電腦與網際網路程式設計 · 1 0 年前

# c++的程式碼超簡單 超奇怪的問題= =

#include "stdafx.h"

#define ARRAY_SIZE 2

void main()

{

int i,j,n=ARRAY_SIZE;

int a[ARRAY_SIZE][ARRAY_SIZE];

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

{a[i][j]=0;

}

printf("%d %d \n%d %d\n", a[1][1], a[1][2], a[2][1], a[5][5]);

}

ㄜ 上面有一ㄍ打錯了 #define ARRAY_SIZE 5 <---這是5

### 3 個解答

• 1 0 年前
最佳解答

Arrays in C or C++ starts at 0 and ends with n-1 where n is the size of the array. So array a starts at a[0][0] and ends at a[4][4]. Thus, your loops assign something at a[5][5] which is not your array. You should loop from 0 to 4 and print a[4][4] instead of a[5][5].

2009-04-18 11:50:51 補充：

Arrays in C or C++ starts at 0 and ends with n-1 where n is the size ofthe array. So array a starts at a[0][0] and ends at a[4][4]. Thus, yourloops assign something at a[5][5] which is not your array. You shouldloop from 0 to 4 and print a[4][4] instead of a[5][5].

• 1 0 年前

呵~除了樓上的回答之外,有個方法蠻快的,

int a[ARRAY_SIZE][ARRAY_SIZE]={0};

這樣就全部是0了

• 1 0 年前

水啦...可以把答案寫在回答那邊ㄇ@@

這樣在過3個小時 時間到之後 就可以選你為最佳解答摟XD