C++将数组重置为null
C++ Reset array to null
我正在试图找到平均值,它适用于我输入的第一组数字,但第二组数字略有偏离,我认为这是因为我没有正确重置数组,或者我错过了重置数组中的一个值。
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int array [15];
int amount, step, length;
double total;
step = -1;
amount = 0;
length = 0;
total = 0;
cin >> amount;
for(int count = 0; count!=amount; count++){
while (array[step] != 0){
step++;
cin >> array[step];
}
length = step;
while (step >= 0){
total = total + array[step];
array[step] = 0;
step--;
}
total = total / length;
cout << round(total) << " ";
step = -1;
}
return 0;
}
step = -1;
代码的第一个过程
while (array[step] != 0){
产生未定义的行为。数组的第一个条目应使用0进行索引。
相关文章:
- c++使用foreach使数组为null
- 如果我在相应 char 数组的声明中为其提供额外的元素,是否会自动设置 NULL?
- 如何在 C++ 中使用 NULL(或 0)初始化静态字符数组
- 正在将对象指针数组初始化为NULL
- 为什么具有单独字符的字符数组不像字符串文字那样以 null 终止符结尾?
- STD :: Sort将数组指针设置为NULL
- 我正在尝试计算 char 数组中的内容,直到 null 终止,但每次编译时,我都会得到一个比我的数组大的数字
- 如何在默认构造函数中将 char 数组初始化为 null
- 2D 数组内存分配 (malloc) 返回 NULL
- Node-ffi 绑定到 NULL 终止的 C 字符串数组,但得到“分段错误:11”
- C++如何将结构数组初始化为null,然后在while循环中检查该数组的元素是否为null
- 初始化数组中的每个变量以'0'与将它们全部初始化为 null 相同吗?
- 尝试用C 中的空地址(而非null)删除数组指针
- C :将文本文件的内容存储在2D数组中,作为字符串(用于NULL终结器的麻烦?)
- 在std::字符串或字符数组中间包含NULL字符的UPDATE
- 将数组中的元素设置为 0 或 NULL
- 正在初始化指向null的专用指针数组
- 将对象的数组设置为c 中的null
- 在此初始值设定项方法上将整个数组设置为 NULL 是否C++标准?
- Opencv:cvGetMat中的Null指针(传递Null数组指针)