## Trending News

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

#include "stdafx.h"

#define ARRAY_SIZE 2

void read_array(int x[][ARRAY_SIZE], int n);

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]);

}

以上 我想製造出一個5*5的矩陣

而且把裡面全都清為0

但是出來之後 很神奇的是 矩陣裡面 竟然有1 有3 有6...

只有大約一半的數字 是0....

請問 有人能告訴我這是怎麼回事ㄇ ˊˋ

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

貼上來之前 忘記修改~

### 3 Answers

- bean_bottom_2Lv 51 decade agoFavorite Answer
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].