从数组打印时,零未显示为0
Zero not displaying as 0 when printing from array
除了秒数等于4或4的倍数外,其他一切都按预期进行。在这种情况下,秒的0显示为零以外的值。
请向我解释为什么会发生这种情况。
当秒数=3时,输出为:
Please enter the height of the bridge (meters): 100
Please enter the time the watermelon falls: 3
Time Falling (seconds) Distance Falling (meters)
*************************************************
0 0
1 4.9
2 19.6
3 44.1
当秒=4时,输出为:
Please enter the height of the bridge (meters): 100
Please enter the time the watermelon falls: 4
Time Falling (seconds) Distance Falling (meters)
*************************************************
1117572301 0
1 4.9
2 19.6
3 44.1
4 78.4
代码:
#include <iostream>
using namespace std;
int main()
{
//declare variables
int seconds, heightBridge, count;
float heightWatermelon, maxFall;
const float gravity = 9.8;
//get variables from user
cout << "Please enter the height of the bridge (meters): ";
cin >> heightBridge;
cout << "Please enter the time the watermelon falls: ";
cin >> seconds;
//declare array's
int secondsArray[seconds];
float heightWatermelonArray[seconds];
for (count = 0; count <= seconds; count++)
{
heightWatermelon = 0.5 * gravity * count * count;
secondsArray[count] = count;
heightWatermelonArray[count] = heightWatermelon;
}
//create heading
cout << "Time Falling (seconds) Distance Falling (meters)n";
cout << "*************************************************n";
//calculate max fall distance
maxFall = 0.5 * gravity * seconds * seconds;
//display data
for (count = 0; count <= seconds; count++)
{
if (maxFall > heightBridge)
{
cout << "Warning - Bad Data: The distance fallen exceeds the "
<< "height of the bridge" << endl;
break;
}
else
cout << secondsArray[count] << "t"
<< heightWatermelonArray[count] << endl;
}
return 0;
}
您使用的是从0
到seconds
(包括)的索引,因此您的数组必须声明为
int secondsArray[seconds+1];
float heightWatermelonArray[seconds+1];
(似乎只有才能使用较小的值,您的代码实际上是在调用未定义的行为)。
您声明您的数组如下:
//declare array's
int secondsArray[seconds];
float heightWatermelonArray[seconds];
然后在以下循环中访问:
for (count = 0; count <= seconds; count++)
当您声明一个长度为seconds
的数组时,这会分配一个元素数为seconds
的数组。因为数组是零索引的,这意味着索引0..(seconds-1)
是有效的。您的循环将从0..seconds
开始,因此将导致溢出。要修复此问题,只需将<=
更改为<
即可。
另一个解决方案是按照Zeta下面所说的调整数组的大小。在这种情况下,只需将阵列规范更改为以下内容:
//declare array's
int secondsArray[seconds+1];
float heightWatermelonArray[seconds+1];
然后你可能会想,为什么你会得到一个奇怪的数字,而不是零。因为在x86上(我假设您在x86上),堆栈向下增长,所以heightWatermelonArray
将直接在内存中的secondsArray
之前。当你写入heightWatermelonArray[seconds]
时(即在heightWatermelonArray
的末尾,你会溢出数组,然后进入下一个内存位。在这种情况下,该内存将是secondsArray
的第一个元素,因此你会损坏内存。
相关文章:
- 显示数组中的学生 ID 和最高分
- 我编写了一个程序来显示数组的内容,但它显示的是它的地址
- 为什么断点显示数组的第二个值是一个大数字?额外学分工作
- 使用指针显示数组的值
- 我无法显示包含整数最大值的索引. 但我可以显示数组中整数的最高值
- 数组:如何在指定索引之前显示数组的第一个和最后一个元素以及数组元素的差异?
- 使用增量/减少操作员在C 中显示数组的奇怪结果
- 将25个甚至整数存储到一个名为intlist的整数阵列中,然后在屏幕上显示数组,并在屏幕上显示名为fivintegers
- 显示数组的问题.一切都放在一个单独的行中,但不放入单独的列中
- strlen 显示数组元素的数量错误
- 我的函数不会用" "替换".",也不会按预期显示数组
- 以C++为单位乘以并显示数组的值
- 找不到标识符,尝试在 C++ 中显示数组值
- 如何显示数组名称中的名称
- 在列表小部件 Qt C++中显示数组内容
- c++将.txt文件中的字符串添加到字符串数组中,并显示数组的值
- C++如何将数组传递给函数,然后调用它来显示数组的值
- 从函数更新(覆盖)和显示数组
- 在单独的数据中显示数组数据
- 仅显示数组文本文件中的第一个字母