【发布时间】:2017-02-24 13:07:52
【问题描述】:
我有一个二维数组,当我第一次打印数组的数据时,日期打印正确,但其他时候 array[last][i] 的数据从 i = 0 到最后 - 1.
显然是逻辑错误,但我不明白原因,因为我复制并粘贴了for语句。所以... ¿C 更改数据?
我使用 gcc -std=c99,但在此之前我尝试使用 C++ 和 cout 语句。
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned int numero_jugaderes = 11;
unsigned int numero = numero_jugaderes - 1;
unsigned int p_a[numero];
float p_aya[numero][numero];
for (unsigned int i = 0; i <= numero; i++) {
p_a[i] = i;
}
for (unsigned int i = 0; i <= numero; i++) {
for (unsigned int j = 0; j <= numero; j++) {
p_aya[i][j] = (float) (p_a[i] * p_a[j]) / 100;
printf("%f\t", p_aya[i][j]);
}
puts("");
}
puts("\n");
for (unsigned int i = 0; i <= numero; i++) {
for (unsigned int j = 0; j <= numero; j++) {
printf("%f\t", p_aya[i][j]);
}
puts("");
}
return 0;
}
【问题讨论】:
-
这在技术上不是一个有效的 C++ 程序,因为 C++ 没有variable-length arrays。
-
@user 我猜是相反的
-
至于您的问题,请记住 X 元素数组的有效索引范围为
0到X - 1(含)。现在仔细看看你的循环。 -
请勿发文字图片。