【发布时间】:2017-06-01 00:32:12
【问题描述】:
我正在尝试制作一个用 0 填充的 10x10 数组,然后打印它,但最后一行没有用 0 填充。
#include <iostream>
using namespace std;
int main()
{
int table[9][9];
int height = 9;
int width = 9;
for(int i=0; i<=width; i++){
for(int j=0; j<=height; j++){
table[i][j] = 0;
}
}
for(int i=0; i<=9; i++){
for(int j=0; j<=9; j++){
cout << table[i][j] << " ";
}
cout << endl;
}
}
为什么这会让我的最后一行出现:
0 9 2 9 10 9 4201024 6422336 6422420 4199045
而不是 0 像其余的行?
【问题讨论】:
-
你要打印多少行?
-
您不应该遍历无效索引。 8 是最后一个索引而不是 9
-
你的 for 循环比你在表中分配的索引多了一个索引。
<=只能是<。